application_vnd.jupyter.stderr_output_1.png
from sklearn.model_selection import train_test_split

import math

import matplotlib.pyplot as plt

import pandas as pd        # For loading and processing the dataset

import torch

import torch.nn.functional as F

df_train = pd.read_csv('../input/train.csv')

df_train = df_train.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1)

df_train = pd.concat([df_train, pd.get_dummies(df_train['Embarked'], prefix='Embarked')], axis=1)
df_train = df_train.drop('Embarked', axis=1)

X_train = df_train.drop('Survived', axis=1).as_matrix()
y_train = df_train['Survived'].as_matrix()
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.2)

X_train, y_train, X_test, y_test = map(torch.tensor, (X_train, y_train, X_test, y_test))

def model(x):
    a1 = torch.matmul(x, weights1) + bias1
    h1 = a1.sigmoid()
    a2 = torch.matmul(h1, weights2) + bias2
    h2 = a2.sigmoid()
    a3 = torch.matmul(h2, weights3) + bias3
    h3 = a3.exp() / a3.exp().sum(-1).unsqueeze(-1)
    return h3

def loss_fn(y_hat, y):
    return -y_hat[range(y.shape[0]), y].log().mean()

def accuracy(y_hat, y):
    pred = torch.argmax(y_hat, dim=1)
    return (pred == y).float().mean()

torch.manual_seed(0)
weights1 = torch.randn(9, 128) / math.sqrt(2)
weights1.requires_grad_()
bias1 = torch.zeros(128, requires_grad=True)
weights2 = torch.randn(128, 256) / math.sqrt(2)
weights2.requires_grad_()
bias2 = torch.zeros(256, requires_grad=True)
weights3 = torch.randn(256, 2) / math.sqrt(2)
weights3.requires_grad_()
bias3 = torch.zeros(2, requires_grad=True)
learning_rate = 0.2
epochs = 10000
X_train = X_train.float()
y_train = y_train.long()
loss_arr = []
acc_arr = []
for epoch in range(epochs):
    y_hat = model(X_train)
    loss = F.cross_entropy(y_hat, y_train)
    loss.backward()
    loss_arr.append(loss.item())
    acc_arr.append(accuracy(y_hat, y_train))
    with torch.no_grad():
        weights1 -= weights1.grad * learning_rate
        bias1 -= bias1.grad * learning_rate
        weights2 -= weights2.grad * learning_rate
        bias2 -= bias2.grad * learning_rate
        weights3 -= weights3.grad * learning_rate
        bias3 -= bias3.grad * learning_rate
        weights1.grad.zero_()
        bias1.grad.zero_()
        weights2.grad.zero_()
        bias2.grad.zero_()
        weights3.grad.zero_()
        bias3.grad.zero_()

weights3.grad