Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_test_metric>
df_train = pd.read_csv('.. /input/titanic/train.csv')
 df_test = pd.read_csv('.. /input/titanic/test.csv')
 df_sub = pd.read_csv('.. /input/titanic/gender_submission.csv' )
activation = lambda y: y
def cont_kappa(input, targets, activation=None):

 n = len(targets)
 y = targets.float().unsqueeze(0)
 pred = input.float().squeeze(-1 ).unsqueeze(0)
 if activation is not None:
pred = activation(pred)
 wo =(pred - y)**2
we =(pred - y.t())**2
return 1 -(n * wo.sum() / we.sum())
<compute_test_metric>
df_train.drop(['Name','Ticket','Cabin'],axis=1,inplace=True)
 df_test.drop(['Name','Ticket','Cabin'],axis=1,inplace=True)
 sex = pd.get_dummies(df_train['Sex'],drop_first=True)
 embark = pd.get_dummies(df_train['Embarked'],drop_first=True)
 df_train = pd.concat([df_train,sex,embark],axis=1)
 df_train.drop(['Sex','Embarked'],axis=1,inplace=True)
 sex = pd.get_dummies(df_test['Sex'],drop_first=True)
 embark = pd.get_dummies(df_test['Embarked'],drop_first=True)
 df_test = pd.concat([df_test,sex,embark],axis=1)
 df_test.drop(['Sex','Embarked'],axis=1,inplace=True)
 df_train.fillna(df_train.mean() ,inplace=True)
 df_test.fillna(df_test.mean() ,inplace=True)
 Scaler1 = StandardScaler()
Scaler2 = StandardScaler()
train_columns = df_train.columns
test_columns = df_test.columns
df_train = pd.DataFrame(Scaler1.fit_transform(df_train))
df_test = pd.DataFrame(Scaler2.fit_transform(df_test))
df_train.columns = train_columns
df_test.columns = test_columns
features = df_train.iloc[:,2:].columns.tolist()
target = df_train.loc[:, 'Survived'].name
X_train = df_train.iloc[:,2:].values
y_train = df_train.loc[:, 'Survived'].values
kappa_loss = lambda pred, y: 1 - cont_kappa(pred, y )<compute_test_metric>
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.autograd import Variable
y = torch.from_numpy(np.random.randint(5, size=6))
preds = torch.ones_like(y.float())* 2
print(y.tolist())
 print(preds.tolist())
 print(activation(preds ).tolist())
 cont_kappa(preds, y), kappa_loss(preds, y), F.mse_loss(preds, y.float() )<compute_train_metric>
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.fc1 = nn.Linear(8, 512)
 self.fc2 = nn.Linear(512, 512)
 self.fc3 = nn.Linear(512, 2)
 self.dropout = nn.Dropout(0.2)
 def forward(self, x):
x = F.relu(self.fc1(x))
x = self.dropout(x)
 x = F.relu(self.fc2(x))
x = self.dropout(x)
 x = self.fc3(x)
 return x
model = Net()
print(model )
class MultiTaskLoss(FocalLoss):
def __init__(self, alpha=None, gamma=2.0, second_loss=F.mse_loss, second_mult=0.1):
super().__init__(alpha, gamma)
 self.second_loss = second_loss
self.second_mult = second_mult
def forward(self, inputs, targets):
loss = super().forward(inputs[...,:-1], targets)
 loss += self.second_mult * self.second_loss(inputs[...,-1], targets.float())
 return loss<choose_model_class>
criterion = nn.CrossEntropyLoss()
criterion = MultiTaskLoss(gamma=2., alpha=class_weights, second_loss=kappa_loss, second_mult=0.5)
 lr = 1e-2
optimizer = optim.SGD([
{
"params": chain(model.stem.parameters() , model.blocks.parameters()),
"lr": lr * 0.1,
},
{
"params": model.head[:6].parameters() ,
"lr": lr * 0.2,
},
{
"params": model.head[6].parameters() ,
"lr": lr
}],
momentum=0.99, weight_decay=1e-4, nesterov=True )<choose_model_class>
optimizer = torch.optim.SGD(model.parameters() , lr=0.01 )
if use_amp:
model, optimizer = amp.initialize(model, optimizer, opt_level="O2", num_losses=1 )<train_model>
batch_size = 64
n_epochs = 500
batch_no = len(X_train)// batch_size
train_loss = 0
train_loss_min = np.Inf
for epoch in range(n_epochs):
for i in range(batch_no):
start = i*batch_size
end = start+batch_size
x_var = Variable(torch.FloatTensor(X_train[start:end]))
y_var = Variable(torch.LongTensor(y_train[start:end]))
optimizer.zero_grad()
output = model(x_var)
 loss = criterion(output,y_var)
 loss.backward()
optimizer.step()
values, labels = torch.max(output, 1)
 num_right = np.sum(labels.data.numpy() == y_train[start:end])
 train_loss += loss.item() *batch_size
train_loss = train_loss / len(X_train)
 if train_loss <= train_loss_min:
print("Validation loss decreased({:6f} ===> {:6f} ).Saving the model...".format(train_loss_min,train_loss))
torch.save(model.state_dict() , "model.pt")
 train_loss_min = train_loss
if epoch % 200 == 0:
print('')
 print("Epoch: {} \tTrain Loss: {} \tTrain Accuracy: {}".format(epoch+1, train_loss,num_right / len(y_train[start:end])))
 print('Training Ended! ' )
def update_fn(engine, batch):
x = convert_tensor(batch[0], device=device, non_blocking=True)
 y = convert_tensor(batch[1], device=device, non_blocking=True)
 model.train()
y_pred = model(x)
 loss = criterion(y_pred, y)
 optimizer.zero_grad()
if use_amp:
with amp.scale_loss(loss, optimizer, loss_id=0)as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
optimizer.step()
return {
"batchloss": loss.item() ,
}<set_options>
X_test = df_test.iloc[:,1:].values
X_test_var = Variable(torch.FloatTensor(X_test), requires_grad=False)
 with torch.no_grad() :
test_result = model(X_test_var)
 values, labels = torch.max(test_result, 1)
 survived = labels.data.numpy()
torch.cuda.empty_cache()
gc.collect()
try:
batch = next(iter(train_loader))
res = update_fn(engine=None, batch=batch)
 print(res)
 finally:
print('max_memory_allocated:', torch.cuda.max_memory_allocated())
 del batch
torch.cuda.empty_cache()
_ = gc.collect()<import_modules>
submission = pd.DataFrame({'PassengerId': df_sub['PassengerId'], 'Survived': survived})
 submission.to_csv('submission.csv', index=False )