《B站Pytorch深度学习实践》关于TItanic二分类问题
本文关于B站刘二大人加载数据集那一讲的实验采用线性模型和逻辑斯蒂回归算法来做
- 载入matplotlib包以及numpy包
我做可视化程序分析时他总挂掉所以载入该数据集
#
DataLoader:batch_szie=2,shuffle=True
#batch_size 为几个一组
#shuffle为是否打乱数据
%pylab inline
import matplotlib.pyplot as plt
import numpy as np
plt.plot(np.sin(np.linspace(0,2*np.pi, 100)))
载入各种程序包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import torch.nn as nn
import torch
from torch.utils.data import Dataset,DataLoader
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import torch
import numpy as np
from torch.utils.data import Dataset#dataset is an abstract class.We can define our class inherited from this class
from torch.utils.data import DataLoader#data
import csv
from sklearn.model_selection import train_test_split#载入这个代码是为了方便数据切片,划分数据集和测试集
因为train数据包采用刘二老师的方法无法进行读取,故而采用pandas直接读取
xy = pd.read_csv('train2.csv')
torch.set_default_tensor_type(torch.FloatTensor)
‘‘‘这个方法的意思是设置pytorch中默认的浮点类型,
一般使用pytorch进行运算时候使用的都是浮点数来进行计算,所以设置默认浮点数有时候也很重要。
显示数据集每一列的类型,数据个数’’’
print(xy.info()
print(xy.head())
x = xy.iloc[:, :-1]
y = xy.iloc[:, [-1]]
Xtrain, Xtest, Ytrain, Ytest = train_test_split(x,y,test_size=0.3)
Xtest = torch.from_numpy(Xtest)#数据的切片
Ytest = torch.from_numpy(Ytest)#数据的切片
如果这段报错的话
↓
\downarrow
↓
Xtest = torch.from_numpy(Xtest)#数据的切片
Ytest = torch.from_numpy(Ytest)#数据的切片
就改为
Xtest = torch.from_numpy(Xtest.values)
Ytest = torch.from_numpy(Ytest.values)
将可以用数字量化的模块该量化就量化,不能量化的就删
我是这么处理的
class Dataset(Dataset):
def __init__(self, data,label):
self.len = data.shape[0] # shape(多少行,多少列)
self.x_data = torch.from_numpy(data)
self.y_data = torch.from_numpy(label)
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
train_dataset = Dataset(Xtrain,Ytrain)
train_loader = DataLoader(dataset=train_dataset, batch_size=32, shuffle=True, num_workers=1)
载入数据集,老规矩如果有bug就改成
self.x_data = torch.from_numpy(data.values)
self.y_data = torch.from_numpy(label.values)
建立模型
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8, 6)#我删减后只有九个数据项了所以就采用Linear来算
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 2)
self.linear4 = torch.nn.Linear(2, 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))
x = self.sigmoid(self.linear4(x))
return x
model = Model()
# construct loss and optimizer
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for epoch in range(100):
for i, data in enumerate(train_loader, 0): # train_loader 是先shuffle后mini_batch
inputs, labels = data
inputs=inputs.type(torch.float)
lables=labels.type(torch.float)
y_pred = model(inputs)
y_pred=y_pred.type(torch.float)
labels=lables=labels.type(torch.float)
loss = criterion(y_pred, labels)
print(epoch, i, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
训练结束
可是我的Loss值太高了
所以我就感觉我的模型有问题,如果需要得出结论并上传的话,就可以直接输出
print(y_pred)
保存即可
有什么问题可以直接私信联系