Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<choose_model_class>
import numpy as np
import pandas as pd
class ParaphraseClassifier(nn.Module):
def __init__(self,hidden_dim,embedding_dim):
super(ParaphraseClassifier, self ).__init__()
self.hidden_dim = hidden_dim
self.embedding_dim = embedding_dim
self.embedding = nn.Embedding(len(TEXT.vocab), embedding_dim)
 self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=1,bidirectional=False)
 self.Wadd = nn.Linear(hidden_dim,hidden_dim)
 self.Wtimes = nn.Linear(hidden_dim,hidden_dim)
 self.Wout = nn.Linear(hidden_dim,1)
 def forward(self,xinputA,xinputB):

 xembeddedA = self.embedding(xinputA)
 lstm_outA,(hiddenA,cellA)= self.lstm(xembeddedA.view(len(xinputA), -1, self.embedding_dim), None)
 xembeddedB = self.embedding(xinputB)
 lstm_outB,(hiddenB,cellB)= self.lstm(xembeddedB.view(len(xinputB), -1, self.embedding_dim), None)
 hiddenT = hiddenA * hiddenB
hiddenD = torch.abs(hiddenA - hiddenB)
 hidden = torch.tanh(self.Wtimes(hiddenT)+ self.Wadd(hiddenD))
return torch.sigmoid(self.Wout(hidden))
def run_test(self,test_set,pred_filename,useGPU=True):
device_code = int(useGPU-1)
 test_iterator = Iterator(test_set,batch_size=64,device=device_code,train=False,sort=False)
 idxList = []
predList = []
with torch.no_grad() :
for batch in test_iterator:
idx,xvecA,xvecB = batch.idx,batch.sentA,batch.sentB
prob = 10 * self.forward(xvecA,xvecB ).squeeze() / 2.0
idxList.extend(idx.tolist())
 predList.extend(prob.tolist())
 df_submission = pa.DataFrame({'pairID':idxList,'Relatedness':predList})
 df_submission.to_csv(pred_filename , index=False)
 print(df_submission.head(n=10))
print("done.")
 def run_train(self,train_set,dev_set,epochs,learning_rate=0.001,useGPU=True):
loss_func = nn.BCELoss()
optimizer = optim.Adam(self.parameters() , lr=learning_rate,weight_decay=0.00001)
 self.embedding = nn.Embedding(len(TEXT.vocab), self.embedding_dim)
 device_code = int(useGPU-1)
 train_iterator = BucketIterator(train_set,batch_size=64, device=device_code, sort=True,sort_key=lambda x:x.sentA,train=True)
 dev_iterator = BucketIterator(dev_set, batch_size=64, device=device_code, sort=True,sort_key=lambda x:x.sentA,train=False)
 max_corr = 0.0
for e in range(epochs):
train_loss = 0
N = 0
train_predictions = []
train_reference = []
for batch in train_iterator:
xvecA,xvecB,yRelness = batch.sentA,batch.sentB,batch.Relatedness
self.zero_grad()
prob = self.forward(xvecA,xvecB ).squeeze()
loss = loss_func(prob,yRelness)
 loss.backward()
optimizer.step()
train_loss += loss.item()
train_predictions.extend(prob.tolist())
 train_reference.extend(yRelness.tolist())
 N += 1
R = np.corrcoef(np.array(train_predictions),np.array(train_reference)) [0][1]
print('Epoch',e,'loss =',train_loss/N,'train correlation(pearson r)=',R)
 with torch.no_grad() :
dev_predictions = []
dev_reference = []
for batch in dev_iterator:
xvecA,xvecB,yRelness = batch.sentA,batch.sentB,batch.Relatedness
prob = self.forward(xvecA,xvecB ).squeeze()
dev_predictions.extend(prob.tolist())
 dev_reference.extend(yRelness.tolist())
 R = np.corrcoef(np.array(dev_predictions),np.array(dev_reference)) [0][1]
print('Epoch',e,'dev correlation(pearson r)=',R)
 if R > max_corr:
torch.save(self.state_dict() , 'para_model.wt')
 max_corr = R
print('model saved.')
 self.load_state_dict(torch.load('para_model.wt'))<train_model>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
pc = ParaphraseClassifier(150,30)
 pc.run_train(df_train,df_dev,50 )<load_from_csv>
train_data.isnull().sum()
df_test = TabularDataset(".. /input/SICK_test.txt","tsv",skip_header=True,\
fields=[('idx',INTEGER),('sentA',TEXT),('sentB',TEXT)])
 pc.run_test(df_test,'submission.csv',useGPU=True )<define_variables>
train_data[train_data['Age'].isnull() ]
device = torch.device('cuda:0')
 train_path = ".. /input/train/train/"
test_path = ".. /input/test/test/"
print(torch.cuda.get_device_name(0))<categorify>
test_data.isnull().sum()
class HindiDataset(Dataset):
def __init__(self, train_img_path, test_img_path, transform = None, train = True):
self.train_img_path = train_img_path
self.test_img_path = test_img_path
self.train_img_files = os.listdir(train_img_path)
 self.test_img_files = os.listdir(test_img_path)
 self.transform = transform
self.train = train
def __len__(self):
return len(self.train_img_files)
 def __getitem__(self, indx):
if self.train:
if indx >= len(self.train_img_files):
raise Exception("Index should be less than {}".format(len(self.img_files)))
 image = Image.open(self.train_img_path + self.train_img_files[indx] ).convert('RGB')
 labels = self.train_img_files[indx].split('_')
 V = int(labels[0][1])
 C = int(labels[1][1])
 label = {'Vowel' : V, 'Consonant' : C}
if self.transform:
image = self.transform(image)
 return image, label
if self.train == False:
image = Image.open(self.test_img_path + self.test_img_files[indx] ).convert('RGB')
 if self.transform:
image = self.transform(image)
 return image, self.test_img_files[indx]<choose_model_class>
train_data[train_data['Embarked'].isnull() ]
class BasicBlock(nn.Module):
def __init__(self, channels = 256, stride = 1, padding = 1):
super(BasicBlock, self ).__init__()
self.channels = channels
self.stride = stride
self.padding = padding
self.conv_1 = nn.Conv2d(in_channels = self.channels, out_channels = self.channels,
kernel_size = 3, stride = self.stride, padding = self.padding)
 self.bn_1 = nn.BatchNorm2d(self.channels)
 self.prelu_1 = nn.PReLU()
self.conv_2 = nn.Conv2d(in_channels = self.channels, out_channels = self.channels,
kernel_size = 3, stride = self.stride, padding = self.padding)
 self.bn_2 = nn.BatchNorm2d(self.channels)
 self.prelu_2 = nn.PReLU()
self.conv_3 = nn.Conv2d(in_channels = self.channels, out_channels = self.channels,
kernel_size = 5, stride = self.stride, padding = self.padding + 1)
 self.bn_3 = nn.BatchNorm2d(self.channels)
 def forward(self, x):
identity = x
x = self.prelu_1(self.bn_1(self.conv_1(x)))
 x = self.bn_2(self.conv_2(x)) + self.bn_3(self.conv_3(identity))
x = self.prelu_2(x)
 return x<choose_model_class>
train_data[train_data['Ticket'] == '113572']
class ModInception(nn.Module):
def __init__(self, channels = 256, stride = 1, padding = 1):
super(ModInception, self ).__init__()
self.channels = channels
self.stride = stride
self.padding = padding
self.conv_1 = nn.Conv2d(in_channels = self.channels, out_channels = 70, kernel_size = 1,
stride = self.stride, padding = 0)
 self.conv_2 = nn.Conv2d(in_channels = self.channels, out_channels = 60, kernel_size = 3,
stride = self.stride, padding = 1)
 self.conv_3 = nn.Conv2d(in_channels = self.channels, out_channels = 126,kernel_size = 5,
stride = self.stride, padding = 2)
 self.bn = nn.BatchNorm2d(self.channels)
 self.prelu = nn.PReLU()
def forward(self, x):
x = torch.cat([self.conv_1(x), self.conv_2(x), self.conv_3(x)], dim=1)
 x = self.prelu(self.bn(x))
return x<choose_model_class>
class ResNet(nn.Module):
def __init__(self, block, incp_block):
super(ResNet, self ).__init__()
self.block = block
self.incp_block = incp_block
self.input_conv = nn.Sequential(
 nn.Conv2d(in_channels = 3, out_channels = 64, kernel_size = 3, padding = 1),
nn.BatchNorm2d(64),
nn.PReLU() ,
nn.Conv2d(in_channels = 64, out_channels = 128, kernel_size = 3, padding = 1),
nn.BatchNorm2d(128),
nn.PReLU() ,
nn.Conv2d(in_channels = 128, out_channels = 256, kernel_size = 3, padding = 1),
nn.BatchNorm2d(256),
nn.PReLU() , 
)
 self.layer_64x64 = self.make_layers(3)
 self.layer_32x32 = self.make_layers(2)
 self.layer_16x16 = self.make_layers(2)
 self.layer_8x8 = self.make_layers(2)
 self.layer_4x4 = self.make_layers(2)
 self.downsample_conv_1 = nn.Sequential(
 nn.Conv2d(in_channels = 256, out_channels = 256, kernel_size = 2, stride = 2),
nn.BatchNorm2d(256),
nn.PReLU() 
)
 self.downsample_conv_2 = nn.Sequential(
 nn.Conv2d(in_channels = 256, out_channels = 256, kernel_size = 2, stride = 2),
nn.BatchNorm2d(256),
nn.PReLU() 
)
 self.downsample_conv_3 = nn.Sequential(
 nn.Conv2d(in_channels = 256, out_channels = 256, kernel_size = 2, stride = 2),
nn.BatchNorm2d(256),
nn.PReLU() 
)
 self.downsample_conv_4 = nn.Sequential(
 nn.Conv2d(in_channels = 256, out_channels = 256, kernel_size = 2, stride = 2),
nn.BatchNorm2d(256),
nn.PReLU() 
)
 self.downsample_conv_5 = nn.Sequential(
 nn.Conv2d(in_channels = 256, out_channels = 128, kernel_size = 2, stride = 2),
nn.BatchNorm2d(128),
nn.PReLU() 
)
 self.V_classifier = nn.Sequential(
 nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.PReLU() ,
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.PReLU() ,
nn.Linear(128, 64),
nn.BatchNorm1d(64),
nn.PReLU() ,
nn.Linear(64, 32),
nn.BatchNorm1d(32),
nn.PReLU() ,
nn.Linear(32, 16),
nn.BatchNorm1d(16),
nn.PReLU() ,
nn.Linear(16, 10)
)
 self.C_classifier = nn.Sequential(
 nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.PReLU() ,
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.PReLU() ,
nn.Linear(128, 64),
nn.BatchNorm1d(64),
nn.PReLU() ,
nn.Linear(64, 32),
nn.BatchNorm1d(32),
nn.PReLU() ,
nn.Linear(32, 16),
nn.BatchNorm1d(16),
nn.PReLU() ,
nn.Linear(16, 10)
)
 def make_layers(self, layers):
res_layers = []
for i in range(layers):
res_layers.append(self.block())
 res_layers.append(self.incp_block())
 return nn.Sequential(*res_layers)
 def forward(self, x):
x = self.input_conv(x)
 x = self.layer_64x64(x)
 x = self.downsample_conv_1(x)
 x = self.layer_32x32(x)
 x = self.downsample_conv_2(x)
 x = self.layer_16x16(x)
 x = self.downsample_conv_3(x)
 x = self.layer_8x8(x)
 x = self.downsample_conv_4(x)
 x = self.layer_4x4(x)
 x = self.downsample_conv_5(x)
 x = x.view(x.shape[0], -1)
 out_1 = self.V_classifier(x)
 out_2 = self.C_classifier(x)
 return out_1, out_2<create_dataframe>
train_data['Age'] = train_data['Age'].fillna(train_data.groupby(['Pclass','Sex','Survived'])['Age'].transform('median'))
test_data['Age'] = test_data['Age'].fillna(test_data.groupby(['Pclass','Sex'])['Age'].transform('median'))
data = HindiDataset(train_path, test_path, transform = transforms.Compose([transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]),
train = True)
 train_size = int(0.9 * len(data))
test_size = len(data)- train_size
train_data, validation_data = random_split(data, [train_size, test_size])
 train_loader = torch.utils.data.DataLoader(data, batch_size=32, shuffle=True)
 validation_loader = torch.utils.data.DataLoader(validation_data, batch_size=32, shuffle=False )<choose_model_class>
train_data['IsChild'] = np.where(train_data['Age'] <= 10, 'Yes', 'No')
 test_data['IsChild'] = np.where(test_data['Age'] <= 10, 'Yes', 'No' )
deepNet = ResNet(BasicBlock, ModInception ).to(device)
 cnn_ce_loss = nn.CrossEntropyLoss()
cnnet_optim = optim.Adam(deepNet.parameters() , lr = 0.0002, weight_decay=0 )<feature_engineering>
train_data = train_data[train_data['Ticket'] != '113572']
def get_accuracy(output, label, batch_size):
label = label.detach().cpu().numpy().squeeze()
output = output.detach().cpu()
_, indices = torch.max(output, dim=1)
 output = torch.zeros_like(output)
 itr = iter(indices)
 for i in range(output.shape[0]):
output[i, int(next(itr)) ] = 1
label = torch.tensor(np.eye(10)[label] ).float()
diff = torch.sum(torch.abs(output - label)) /(2*output.shape[0])
 acc = 100 -(100 * diff)
 return acc<train_on_grid>
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Age"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )