Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
!pip install autogluon --user
clear_output()
token = ["[CLS]"]+tokenizer.tokenize(text)[:max_lengths-2]+["[SEP]"]
input_id = tokenizer.convert_tokens_to_ids(token)
 segment_id = [0]*max_lengths
attention_mask = [1]*len(input_id)+[0]*(max_lengths - len(input_id))
input_id = input_id+[pad]*(max_lengths-len(input_id))<define_variables>
import numpy as np
import pandas as pd
import os
import random
from autogluon.tabular import TabularDataset, TabularPredictor
class ReviewDataset(torch.utils.data.Dataset):
def __init__(self, texts, labels=[]):
self.input_ids, self.segment_ids, self.attention_masks = [],[],[]
for text in tqdm(texts):
token = ["[CLS]"]+tokenizer.tokenize(text)[:max_lengths-2]+["[SEP]"]
input_id = tokenizer.convert_tokens_to_ids(token)
 segment_id = [0]*max_lengths
attention_mask = [1]*len(input_id)+[0]*(max_lengths - len(input_id))
input_id = input_id+[pad]*(max_lengths-len(input_id))
self.input_ids.append(input_id)
 self.segment_ids.append(segment_id)
 self.attention_masks.append(attention_mask)
 self.input_ids = np.array(self.input_ids)
 self.segment_ids = np.array(self.segment_ids)
 self.attention_masks = np.array(self.attention_masks)
 self.labels = labels
def __len__(self):
return len(self.input_ids)
 def __getitem__(self, idx):
if len(self.labels):
return self.input_ids[idx], self.segment_ids[idx], self.attention_masks[idx], self.labels[idx]
else:
return self.input_ids[idx], self.segment_ids[idx], self.attention_masks[idx]<split>
TRAIN_PATH = ".. /input/titanic/train.csv"
TEST_PATH = ".. /input/titanic/test.csv"
SAMPLE_SUBMISSION_PATH = ".. /input/titanic/gender_submission.csv"
SUMISSION_PATH = "submission.csv"
TARGET = 'Survived'
EVAL_METRIC = "roc_auc"
SAVE_PATH = 'agModels-predictClass'
DEFAULT_RANDOM_SEED = 2021
def seedBasic(seed=DEFAULT_RANDOM_SEED):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 seedBasic()
batch_size=8
X,y = train_df["Body"].values, train_df["Label"].values
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=100,stratify=y)
 train_ds = ReviewDataset(texts=X_train, labels=y_train)
 train_dl = torch.utils.data.DataLoader(
 train_ds, batch_size=batch_size, shuffle=True)
 val_ds = ReviewDataset(texts=X_val, labels=y_val)
 val_dl = torch.utils.data.DataLoader(
 val_ds, batch_size=batch_size, shuffle=False )<choose_model_class>
train = TabularDataset(TRAIN_PATH)
 test = TabularDataset(TEST_PATH )
class BertClassification(nn.Module):
def __init__(self, bert):
super(BertClassification, self ).__init__()
self.bert = bert
self.cls = nn.Linear(in_features=768, out_features=2)
 nn.init.normal_(self.cls.weight, std=0.02)
 nn.init.normal_(self.cls.bias, 0)
 def forward(self, input_ids, token_type_ids=None, attention_mask=None):
pooled_output = self.bert(
 input_ids, token_type_ids, attention_mask)[1]
pooled_output = pooled_output.view(-1, 768)
 out = self.cls(pooled_output)
 return out<choose_model_class>
predictor = TabularPredictor(label=TARGET,eval_metric=EVAL_METRIC, path=SAVE_PATH ).fit(train )
cuda = torch.cuda.is_available()
bert = BertModel.from_pretrained("cl-tohoku/bert-base-japanese-whole-word-masking")
 model = BertClassification(bert)
 torch.backends.cudnn.benchmark = True
if cuda:
model.cuda()
optimizer = optim.Adam(model.parameters() ,lr = 4e-4 ,betas=(0.9, 0.999))
criterion = nn.CrossEntropyLoss()<categorify>
y_pred = predictor.predict(test )
for param in model.bert.encoder.layer[:-1].parameters() :
param.requires_grad = False<train_model>
submission = pd.read_csv(SAMPLE_SUBMISSION_PATH)
 submission[TARGET] = y_pred
submission.to_csv(SUMISSION_PATH,index=False)
 submission.head()