代码
import numpy as np
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
class DiabetesDataset(Dataset):
def __init__(self, filepath):
xy = np.loadtxt(filepath, delimiter = ',',dtype = np.float32)
self.len = xy.shape[0]#Nx9,拿出第0个元素可知有多少样本数
self.x_data = torch.from_numpy(xy[:,:-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
#能够使用len()计算长度
dataset123 = DiabetesDataset('diabetes.csv.gz')#数据集在老师给的课件包里
train_loader = DataLoader(dataset = dataset123, batch_size=32, shuffle=True, num_workers=0)#一个小批量的容量,打乱,不使用多进程
class Model(torch.nn.Module):
def __init__(self):
super(Model,self).__init__()
self.linear1 = torch.nn.Linear(8,6)
self.linear2 = torch.nn.Linear(6,4)
self.linear3 = torch.nn.Linear(4,1)
self.activate = torch.nn.Sigmoid()
def forward(self, x):
x = self.activate(self.linear1(x))
x = self.activate(self.linear2(x))
x = self.activate(self.linear3(x))
return x
model = Model()
criterion = torch.nn.BCELoss(size_average = True)#返回loss均值
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
for epoch in range(100):
for i, data in enumerate(train_loader, 0):#train_loader中的x y 放到data中 32个样本为一组放入
#1 data Pre
inputs, labels = data# x y 都是张量
#2 foward
y_pred = model(inputs)
loss = criterion(y_pred, labels)
print(epoch, i, loss.item())
#3 backward
optimizer.zero_grad()
loss.backward()
#4 update
optimizer.step()
问题
将num_workers设置为2时会出现报错
runtimeerror: dataloader worker (pid(s) 14128) exited unexpectedly
所以最后将num_workers设置为0,不使用多线程进行
如果后续要使用多进程进行,该如何解决这个问题?
结果
loss在0.6~0.75之间