使用Titanic数据集,label是 ‘Survived’
import torch
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
import pandas as pd
class TitanicData(Dataset):
def __init__(self, filepath):
xy = pd.read_csv(filepath, sep=',')
self.len = xy.shape[0]
self.x_data = torch.tensor(xy[['Pclass', 'SibSp', 'Parch', 'Fare']].values, dtype=torch.float32)
self.y_data = torch.tensor(xy[['Survived']].values, dtype=torch.float32)
def __getitem__(self, item):
return self.x_data[item], self.y_data[item]
def __len__(self):
return self.len
titanic = TitanicData("./datasets/titanic/train.csv")
dataloader = DataLoader(dataset=titanic, batch_size=32, shuffle=True)
class TianicModel(torch.nn.Module):
def __init__(self):
super(TianicModel, self).__init__()
self.linear1 = torch.nn.Linear(4, 2)
self.linear2 = 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.sigmoid(self.linear2(x))
return x
model = TianicModel()
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
loss = []
for epoch in range(100):
for i, (inputs, label) in enumerate(dataloader):
y_pred = model(inputs)
l = criterion(y_pred, label)
optimizer.zero_grad()
l.backward()
optimizer.step()
print(f"epoch: {epoch},\tloss: {l.item()}")
loss.append(l.item())
loss = np.array(loss)
plt.plot(range(100), loss)
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
plt.close()