数据加载
import torch
import numpy as np
from torch.autograd import Variable
from torch.utils.data import DataLoader, Dataset
class DiabetesDataset(Dataset):
def __init__(self):
super(DiabetesDataset, self).__init__()
xy = np.loadtxt('.//data//diabetes.csv.gz', delimiter=',', dtype=np.float32)
self.len = xy.shape[0]
self.x_data = torch.from_numpy(xy[:, 0 : -1])
self.y_data = torch.from_numpy(xy[:, [-1]])
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
def run(self):
dataset = DiabetesDataset()
train_loader = DataLoader(dataset=dataset, batch_size=32, shuffle=True, num_workers=2)
for epoch in range(2):
for i, data in enumerate(train_loader, 0):
inputs, labels = data
inputs, labels = Variable(inputs), Variable(labels)
print(epoch, i, 'inputs', inputs.data, 'labels', labels.data)
if __name__ == "__main__":
print("Life is short, You need Python!")
d = DiabetesDataset()
d.run()