b站刘二视频,地址:
《PyTorch深度学习实践》完结合集_哔哩哔哩_bilibili
代码 (数据可私信或评论获取)
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
#-------------------------------------------step1 prepare data----------------------------------------
class Data(Dataset):
def __init__(self, filepath):
xy = pd.read_csv(filepath)
self.len = xy.shape[0]
feature = ["Pclass", "Sex", "SibSp", "Parch", "Fare"] #选取用的特征
self.x_data = torch.from_numpy(np.array(pd.get_dummies(xy[feature])))
self.y_data = torch.from_numpy(np.array(xy["Survived"]))
def __getitem__(self, item):
return self.x_data[item], self.y_data[item]
def __len__(self):
return self.len
dataset = Data('train.csv')
dataloder = DataLoader(dataset=dataset, batch_size=32, shuffle=True)
#------------------------------------------setp2 design model--------------------------------------------
class Modle(torch.nn.Module):
def __init__(self):
super(Modle, self).__init__()
self.linear1 = torch.nn.Linear(6, 3)
self.linear2 = torch.nn.Linear(3, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
return x
def predict(self, x):#对test进行预测
with torch.no_grad():
x = self.forward(x)
y = []
for i in x:
if i > 0.5:
y.append(1)
else:
y.append(0)
return y
modle = Modle()
#---------------------------------------step3 constuct loss and optimizer-----------------------------
criteration = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(modle.parameters(), lr=0.01)
#---------------------------------------step4 traning cycle------------------------------------------
if __name__ == '__main__':
loss_lst = []
for epoch in range(1000):
sum = 0
for i, data in enumerate(dataloder, 0):
inputs, labels = data
inputs = inputs.float()
labels = labels.float()
y_pred = modle(inputs)
y_pred = y_pred.squeeze(-1)#前向输出结果是[[12],[34],[35],[23],[11]]这种,需要将这个二维矩阵转换成一行[12,34,35,23,11]
#print(y_pred.size(), labels.size())
loss = criteration(y_pred, labels)
sum += loss.item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_lst.append(sum / dataloder.batch_size)
#可视化
num_lst = [i for i in range(len(loss_lst))]
plt.plot(num_lst, loss_lst)
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
#测试集
test_data = pd.read_csv('test.csv')
feature = ["Pclass", "Sex", "SibSp", "Parch", "Fare"] # 选取用的特征
test = torch.from_numpy(np.array(pd.get_dummies(test_data[feature])))
y = modle.predict(test.float())
outputs = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y})
outputs.to_csv('predict_titanic', index=False) # index=False 代表不保存索引
outputs.head()
输出结果