用pytorch实现预测是否会患糖尿病
知识点补充
- train_loader = DataLoader(dataset=dataset,
batch_size=32, shuffle=True, num_workers=8)
- dataset:数据集
- batch_size:一个batch里包含的样本数
- shuffle:是否需要打乱顺序,一般训练集为保证随机性要打乱顺序,而测试集为了保证输出结果的直观性不打乱
- num_worker:多线程使用的线程个数,一般设置为4或者8,注意在windows环境下,多线程会报错,需要加入在程序的执行部分加入if name == ‘main’::,如果是Linux则不需要。
准备数据集
class DiabetesDataset(Dataset):
def __init__(self,filepath):
xy = np.loadtxt(filepath, delimiter=" " , dtype=np.float32)
self.len = xy.shape[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
dataset = DiabetesDataset("diabetes_data.csv.gz")
train_loader = DataLoader(dataset=dataset,
batch_size=32,
shuffle=True,
num_workers=8)
创建神经网络
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(9,6)
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 1)
self.sigmoid = torch.nn.Sigmoid()
self.relu = torch.nn.ReLU()
def forward(self, x):
x = self.relu(self.linear1(x))
x = self.relu(self.linear2(x))
x = self.sigmoid(self.linear3(x))
return x
开始训练
if __name__ == '__main__':
model = Model()
criterion =torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for epoch in range(100):
for i, data in enumerate(train_loader, 0):
input, label = data
y_pred = model(input)
loss = criterion(y_pred, label)
print(epoch, i, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
|