数据格式
from torch.utils.data import Dataset,DataLoader
import numpy as np
import torch
重载Dataset
class mydata(Dataset):
def __init__(self, path):
self.xy = np.loadtxt(path,delimiter=',',dtype=np.float32)
self.x_data = torch.from_numpy(self.xy[:,:-1])
self.y_data = torch.from_numpy(self.xy[:,[-1]])
def __getitem__(self, idx):
return self.x_data[idx],self.y_data[idx]
def __len__(self):
return self.xy.shape[0]
data = mydata('txt_data\data.txt')
data[0] # 索引
train = DataLoader(data,batch_size=2,shuffle=True,num_workers=2)
# shuffle 随机打乱
# num_workers 线程数