当前位置:网站首页>Pytorch每日一练——预测泰坦尼克号船上的生存乘客
Pytorch每日一练——预测泰坦尼克号船上的生存乘客
2022-07-31 05:16:00 【qq_50749521】
训练数据:
Survived是输出标签,其他年龄、性别、名字等等都当做输入。当然会有数据缺失的情况,需要提前进行清洗。
测试的目的就是输入样本特征,输出是否能生存下来(0或1)
import torch
import pandas as pd
import numpy as np
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
class DiabetesDataset(Dataset):
def __init__(self, filepath):
xy = pd.read_csv(filepath)
self.len = xy.shape[0]
features = ["Pclass", "Sex", "SibSp", "Parch", "Fare"]
self.x_data = torch.from_numpy(np.array(pd.get_dummies(xy[features])))
self.y_data = torch.from_numpy(np.array(xy['Survived']))
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
dataset = DiabetesDataset('Dataset\\titanic\\train.csv')
train_loader = DataLoader(dataset = dataset,
batch_size = 32,
shuffle = True,
num_workers = 0)
batch_size = 32
batch = np.round(dataset.__len__() / batch_size)
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(6, 4)
self.linear2 = torch.nn.Linear(4, 2)
self.linear3 = torch.nn.Linear(2, 1)
self.relu = torch.nn.ReLU()
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.relu(self.linear1(x))
x = self.relu(self.linear2(x))
x = self.sigmoid(self.linear3(x))#注意最后一步不能使用relu,避免无法计算梯度
return x
mymodel = Model()
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(mymodel.parameters(), lr = 0.01)
epoch_list = []
loss_list = []
sum_loss = 0
if __name__ == '__main__':
for epoch in range(500):
for index, data in enumerate(train_loader, 0): #train_loader存的是分割组合后的小批量训练样本和对应的标签
inputs, labels = data #inputs labels都是张量
inputs = inputs.float()
labels = labels.float()
y_pred = mymodel(inputs)
y_pred = y_pred.squeeze(-1)
loss = criterion(y_pred, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
sum_loss += loss.item()
print('epoch = ', epoch + 1,'index = ', index+1, 'loss = ', loss.item())
epoch_list.append(epoch)
loss_list.append(sum_loss/batch)
print(sum_loss/batch)
sum_loss = 0
test_x = pd.read_csv('Dataset\\titanic\\test.csv')
features = ["Pclass", "Sex", "SibSp", "Parch", "Fare"]
test_x_data = torch.from_numpy(np.array(pd.get_dummies(test_x[features])))
test_x_data = test_x_data.float()
y_test_pred = mymodel(test_x_data)
len_y = y_test_pred.shape[0]
y = []
for i in range(len_y):
if(y_test_pred[i].item()<0.5):
y.append(0)
else:
y.append(1)
for i in range(len(y)):
print(y[i])
最后把输出的y保存到gender_submission.csv中,提交kaggle即可。
刚开始练习基础,后面再慢慢改进…
边栏推荐
- MySql to create data tables
- TransactionTemplate transaction programmatic way
- 纯shell实现文本替换
- 为数学而歌之伯努利家族
- 一个简单的bash转powershell案例
- Podspec verification dependency error problem pod lib lint , need to specify the source
- QT VS中双击ui文件无法打开的问题
- SSH自动重连脚本
- 多元线性回归方程原理及其推导
- quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
猜你喜欢

自定dialog 布局没有居中解决方案
![[Cloud native] Simple introduction and use of microservice Nacos](/img/06/b0594208d5b0cbf3ae8edd80ec12c4.png)
[Cloud native] Simple introduction and use of microservice Nacos

For penetration testing methods where the output point is a timestamp (take Oracle database as an example)

CNN的一点理解

npm WARN config global `--global`, `--local` are deprecated. Use `--location solution

为数学而歌之伯努利家族

Artifact SSMwar exploded Error deploying artifact.See server log for details

Tencent Cloud GPU Desktop Server Driver Installation

活体检测PatchNet学习笔记

Understanding of js arrays
随机推荐
UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装
quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
Podspec automatic upgrade script
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
sql 外键约束【表关系绑定】
cocos2d-x-3.2图片灰化效果
Gradle sync failed: Uninitialized object exists on backward branch 142
人脸识别AdaFace学习笔记
一文速学-玩转MySQL获取时间、格式转换各类操作方法详解
npm WARN config global `--global`, `--local` are deprecated. Use `--location solution
ERROR Error: No module factory availabl at Object.PROJECT_CONFIG_JSON_NOT_VALID_OR_NOT_EXIST ‘Error
Several solutions for mysql startup error The server quit without updating PID file
为数学而歌之伯努利家族
The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
Tencent Cloud GPU Desktop Server Driver Installation
Android软件安全与逆向分析阅读笔记
Filter out egrep itself when using ps | egrep
活体检测FaceBagNet阅读笔记
jenkins +miniprogram-ci 一键上传微信小程序
Sourcery插件(自动提升代码质量)