当前位置:网站首页>Pytorch Daily Practice - Predicting Surviving Passengers on the Titanic
Pytorch Daily Practice - Predicting Surviving Passengers on the Titanic
2022-07-31 06:32:00 【qq_50749521】
训练数据:
Survived是输出标签,other age、性别、Names, etc. are treated as input.Of course there will be missing data,It needs to be cleaned in advance.
The purpose of the test is to input the sample features,Whether the output can survive(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_loaderWhat is stored is the split and combined mini-batch training samples and the corresponding labels
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])
Finally put the outputy保存到gender_submission.csv中,提交kaggle即可.
Just started practicing the basics,Improve slowly later…
边栏推荐
- Navicat从本地文件中导入sql文件
- DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
- DSPE-PEG-Azide DSPE-PED-N3 Phospholipid-Polyethylene Glycol-Azide Lipid PFG
- 解决background-size:cover时图片铺满但显示不完整?
- 拒绝采样小记
- The array technique, my love
- Podspec automatic upgrade script
- DingTalk Enterprise Internal-H5 Micro Application Development
- About iframe
- 科研试剂Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺
猜你喜欢

MW: 3400 4-Arm PEG-DSPE four-arm-polyethylene glycol-phospholipid a saturated 18-carbon phospholipid

浏览器中的画中画(Picture-in-Picture)API

JS写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数JS

Pytorch每日一练——预测泰坦尼克号船上的生存乘客

CAS:1403744-37-5 DSPE-PEG-FA 科研实验用磷脂-聚乙二醇-叶酸

2021-09-30

Numpy常用函数

Navicat从本地文件中导入sql文件

mPEG-DSPE 178744-28-0 甲氧基-聚乙二醇-磷脂酰乙醇胺线性PEG磷脂

使用 OpenCV 提取图像的 HOG、SURF 及 LBP 特征 (含代码)
随机推荐
Fluorescein-PEG-DSPE Phospholipid-Polyethylene Glycol-Fluorescein Fluorescent Phospholipid PEG Derivatives
虚拟机查看端口号进程
jenkins +miniprogram-ci upload WeChat applet with one click
Navicat从本地文件中导入sql文件
MW:3400 4-Arm PEG-DSPE 四臂-聚乙二醇-磷脂一种饱和的18碳磷脂
PyTorch学习笔记08——加载数据集
JS写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数JS
如何修改数据库密码
ROS 之订阅多个topic时间同步问题
四种常见的POST提交数据方式
Word vector - demo
DSPE-PEG-Thiol DSPE-PEG-SH phospholipid-polyethylene glycol-thiol liposome for later use
IDEA概述和安装及调试
Fluorescein-PEG-DSPE 磷脂-聚乙二醇-荧光素荧光磷脂PEG衍生物
Picture-in-Picture API in the browser
2021-09-30
Phospholipids-Polyethylene Glycol-Active Esters for Scientific Research DSPE-PEG-NHS CAS: 1445723-73-8
VTK:Could not locate vtkTextRenderer object.
Pytorch每日一练——预测泰坦尼克号船上的生存乘客
DSPE-PEG-Biotin, CAS: 385437-57-0, phospholipid-polyethylene glycol-biotin prolongs circulating half-life