Pytorch中DataLoader && TensorDataset 的基本用法

DataLoader:

DataLoader(dataset=torch_dataset,batch_size=BATCH_SIZE,shuffle=True,num_workers=2)

dataset:

  • DataLoader支持的两种数据集
  • Iterator格式:例如数组,迭代器等

Shuffle :是否重新整理数据。
num_workers :加载数据所需的子进程数。

import torch
from torch.utils.data import DataLoader
from torch.utils.data import TensorDataset
BATCH_SIZE = 5
# 生成从 (start,end, steps) start=1开始,end=10结束的,等距的steps个数据点
# tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
x = torch.linspace(1,10,10)
# tensor([10.,  9.,  8.,  7.,  6.,  5.,  4.,  3.,  2.,  1.])
y = torch.linspace(10,1,10)
# print(x,y)

torch_dataset = TensorDataset(x,y)
loader = DataLoader(dataset=torch_dataset,batch_size=BATCH_SIZE,shuffle=True,num_workers=2)
def show_batch():
    for epoch in range(3):
        for step, (batch_x, batch_y) in enumerate(loader):
            print("step:{}, batch_x:{}, batch_y:{}".format(step,batch_x,batch_y))
#  这样取数据也可           
# def show_batch():
#     for epoch in range(3):
#         for step, data in enumerate(loader):
#             batch_x, batch_y = data
#             print("step:{}, batch_x:{}, batch_y:{}".format(step,batch_x,batch_y))

if __name__ == '__main__':
    show_batch()

TensorDataset

import torch
from torch.utils.data import DataLoader
from torch.utils.data import TensorDataset
BATCH_SIZE = 5
# 生成从 (start,end, steps) start=1开始,end=10结束的,等距的steps个数据点
# tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
x = torch.linspace(1,10,10)
# tensor([10.,  9.,  8.,  7.,  6.,  5.,  4.,  3.,  2.,  1.])
y = torch.linspace(10,1,10)
z = torch.linspace(20,30,10)
if __name__ == '__main__':
    # show_batch()
    torch_dataset = TensorDataset(x,y,z)
    # 0:7取index 为0-6的x,y,z
    print(torch_dataset[0:7])

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2022年5月24日
下一篇 2022年5月24日

相关推荐