一些知识点的说明:
1、DataSet 是抽象类,不能实例化对象,我们需要自己写一个数据集的类去继承。继承DataSet 要重写init,getitem,len魔法函数。分别是为了加载数据集,获取数据索引,获取数据总量。
2、DataLoader 需要获取DataSet提供的索引[i]和len;用来帮助我们加载数据,比如说做shuffle(打乱数据,提高数据集的随机性),batch_size,能拿出Mini-Batch进行训练。它帮我们自动完成这些工作。DataLoader可实例化对象。
3、__ getitem __函数目的是为支持下标(索引)操作
我在其代码的基础上做了一些修改,首先是将训练数据拆分,分为0.8的训练集和0.2的测试集,将训练过程封装成train()函数,将测试过程封装成test()函数。
import numpy as np
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from sklearn.model_selection import train_test_split
import pdb
#定义一个cpu
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 读取原始数据集,并且划分为训练集和测试集
xy = np.loadtxt('diabetes.csv', delimiter = ',', dtype = np.float32)
x = xy[:, :-1]
y = xy[:, [-1]]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3)
x_test = torch.from_numpy(x_test).to(device)
y_test = torch.from_numpy(y_test).to(device)
# prepare dataset
class DiabetesDataset(Dataset):
def __init__(self, data, labels):
#xy = np.loadtxt(filepath, delimiter = ',', dtype = np.float32)
self.len = data.shape[0] # shape(行,列)
self.x_data = torch.from_numpy(data)
self.y_data = torch.from_numpy(labels)
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
train_dataset = DiabetesDataset(x_train, y_train)
train_loader = DataLoader(dataset = train_dataset, batch_size = 32, shuffle = True, num_workers = 0) #num_workers 多线程
# design model using class
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.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
x = self.sigmoid(self.linear3(x))
return x
model = Model()
model.to(device)
# construct loss and optimizer
criterion = torch.nn.BCELoss(reduction = 'mean')
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
# optimizer = torch.optim.Adam(model.parameters(),
# lr=0.001,
# betas=(0.9, 0.999),
# eps=1e-08,
# weight_decay=0,
# amsgrad=False)
# training cycle forward, backward, update
def train(epoch):
train_loss = 0.0
count = 0.0
for i, data in enumerate(train_loader, 0): # start = 0,train_loader 是先shuffle后mini_batch
#inputs, labels = data
inputs, labels = data[0].to(device), data[1].to(device) # 使用gpu训练
y_pred = model(inputs)
loss = criterion(y_pred, labels)
#print(epoch, i, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss.item()
count = i
if epoch % 1000 == 999:
#pdb.set_trace()
print('epoch:', epoch+1, 'train loss:', train_loss/count, end = ',')
def test():
# 在with torch.no_grad()下对变量的操作,均不会让求梯度为真
with torch.no_grad():
y_pred = model(x_test)
y_pred_label = torch.where(y_pred >= 0.5, torch.tensor([1.0]).to(device), torch.tensor([0.0]).to(device))
acc = torch.eq(y_pred_label, y_test).sum().item() / y_test.size(0)
print('test acc:', acc)
if __name__ == '__main__':
for epoch in range(50000):
train(epoch)
if epoch % 1000 == 999:
test()