Kaggle competition: Titanic - Machine Learning from Disaster
if not torch.cuda.is_available() :
print("WARNING: CUDA is not available.Select 'GPU On' on kernel settings")
 device = torch.device("cuda")
 torch.cuda.manual_seed(seed )<choose_model_class>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
criterion = torch.nn.CrossEntropyLoss(reduction='sum' )<train_model>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
def train(model, optimizer, data, batch_size, token_size, log=False):
model.train()
total_loss = 0
ncorrect = 0
nsentences = 0
ntokens = 0
niterations = 0
for batch in pool_generator(data, batch_size, token_size, shuffle=True):
X = [torch.from_numpy(d[0])for d in batch]
X_lengths = [x.numel() for x in X]
ntokens += sum(X_lengths)
 X_lengths = torch.tensor(X_lengths, dtype=torch.long, device=device)
 y = torch.tensor([d[1] for d in batch], dtype=torch.long, device=device)
 X = torch.nn.utils.rnn.pad_sequence(X ).to(device)
 model.zero_grad()
output = model(X, X_lengths)
 loss = criterion(output, y)
 loss.backward()
optimizer.step()
total_loss += loss.item()
ncorrect +=(torch.max(output, 1)[1] == y ).sum().item()
nsentences += y.numel()
niterations += 1
total_loss = total_loss / nsentences
accuracy = 100 * ncorrect / nsentences
if log:
print(f'Train: wpb={ntokens//niterations}, bsz={nsentences//niterations}, num_updates={niterations}')
 return accuracy<prepare_x_and_y>
train_data['titles'] = train_data.Name.str.split().str[1]
test_data['titles'] = test_data.Name.str.split().str[1]
def validate(model, data, batch_size, token_size):
model.eval()
ncorrect = 0
nsentences = 0
with torch.no_grad() :
for batch in pool_generator(data, batch_size, token_size):
X = [torch.from_numpy(d[0])for d in batch]
X_lengths = torch.tensor([x.numel() for x in X], dtype=torch.long, device=device)
 y = torch.tensor([d[1] for d in batch], dtype=torch.long, device=device)
 X = torch.nn.utils.rnn.pad_sequence(X ).to(device)
 answer = model(X, X_lengths)
 ncorrect +=(torch.max(answer, 1)[1] == y ).sum().item()
nsentences += y.numel()
dev_acc = 100 * ncorrect / nsentences
return dev_acc<define_variables>
train_data[train_data.Age >12][:3]
hidden_size = 256
embedding_size = 64
bidirectional = False
ntokens = len(char_vocab)
 nlabels = len(lang_vocab )<train_model>
train_data['age_fill'] = train_data['Age']
test_data['age_fill'] = test_data['Age']
def fill_age(df):
for i, title in enumerate(df.titles):
if df.loc[i,'age_fill'] != df.loc[i,'age_fill']:
if df.loc[i,'titles'] == 'Master.':
df.loc[i,'age_fill']= round(random.uniform(0,12),1)
 if df.loc[i,'titles'] == 'Miss.':
df.loc[i,'age_fill']= round(random.uniform(0.0,df.Age.max()),1)
 else:
df.loc[i,'age_fill'] = round(random.uniform(13.0,df.Age.max()),1)
 fill_age(train_data)
 fill_age(test_data)
def testParameters(model, optimizer, epochs, batch_size, token_size):
train_accuracy = []
valid_accuracy = []
for epoch in range(1, epochs + 1):
acc = train(model, optimizer, train_data, batch_size, token_size, log=epoch==1)
 train_accuracy.append(acc)
 acc = validate(model, val_data, batch_size, token_size)
 valid_accuracy.append(acc)
 return train_accuracy, valid_accuracy<train_model>
train_data['relatives'] = train_data['SibSp'] + train_data['Parch']
test_data['relatives'] = train_data['SibSp'] + train_data['Parch']
train_data.relatives.value_counts()
Hyperparameter optimisation:
Trains and vlidates the model, then returns the accuracy to persist over iterations .<choose_model_class>
def combine_fam(df):
for i, r in enumerate(df['relatives']):
if df.loc[i,'relatives'] == 0:
df.loc[i,'relatives_code'] = 0
if df.loc[i,'relatives'] > 0 & df.loc[i,'relatives'] <= 3:
df.loc[i,'relatives_code'] = 1
if df.loc[i,'relatives'] > 3 & df.loc[i,'relatives'] <= 5:
df.loc[i,'relatives_code'] = 2
if df.loc[i,'relatives'] > 5:
df.loc[i,'relatives_code'] = 3
combine_fam(train_data)
 train_data.relatives_code = train_data.relatives_code.astype(int)
 combine_fam(test_data)
 test_data.relatives_code = test_data.relatives_code.astype(int )
model = CharRNNClassifier(ntokens, embedding_size, hidden_size, nlabels, bidirectional=bidirectional, pad_idx=pad_index ).to(device)
 optimizer = torch.optim.Adam(model.parameters() )<choose_model_class>
train_data['Fare_new'] = train_data['Fare']
test_data['Fare_new'] = test_data['Fare']
def find_price(df):
freqs = {}
for ticket in df.Ticket:
freqs[ticket]= freqs.get(ticket, 0)+ 1
combined_ticket = {k:v for k,v in freqs.items() if v > 1}
for i, ticket in enumerate(df['Ticket']):
if ticket in combined_ticket:
df.loc[i,'Fare_new'] = df.loc[i,'Fare']/combined_ticket[ticket]
train_data['Fare_new'] = train_data['Fare'].astype(int)
 test_data['Fare_new'] = test_data['Fare_new'].fillna(test_data.Fare_new.mean() ).astype(int )
batch_sizes = [64, 128, 256, 512, 1024]
token_sizes = np.arange(50000, 350000, 50000)
 hidden_sizes = np.arange(128, 768, 64)
 embedding_sizes = np.arange(64, 256*4, 64)
 embedding_size = 128
hidden_size = 512
hidden_models = []
hidden_optimizers = []
figure = 0
batch_size = 256
token_size = 150000
epochs = 20
max_acc = 0
std_epochs = 10
bidirectional = False
results = testParameters(model,optimizer, epochs, batch_size, token_size)
 plt.figure(figure)
 plt.plot(range(1, len(results[0])+1), results[0])
 plt.plot(range(1, len(results[1])+1), results[1])
 plt.title('Acuracy')
 plt.xlabel('epoch')
 plt.ylabel('Accuracy'); 
<choose_model_class>
train_data["Sex"] = train_data["Sex"].astype("category")
 test_data["Sex"] = test_data["Sex"].astype("category" )
<choose_model_class>
embark = train_data.Embarked.unique() [:3]
train_data['Embarked_fill'] = train_data['Embarked'].fillna(random.choice(embark))
test_data['Embarked_fill'] = test_data['Embarked'].fillna(random.choice(embark))
train_data["Embarked_fill"] = train_data["Embarked_fill"].astype("category")
 test_data["Embarked_fill"] = test_data["Embarked_fill"].astype("category" )
<choose_model_class>
decks = train_data["Cabin"].str[0].unique() [1:]
train_data["Deck"] = train_data["Cabin"].str[0].fillna(random.choice(decks))
test_data["Deck"] = test_data["Cabin"].str[0].fillna(random.choice(decks))
train_data["Deck"] = train_data["Deck"].astype("category")
 test_data["Deck"] = test_data["Deck"].astype("category")
model = CharRNNClassifier(ntokens, embedding_size, hidden_size, nlabels, bidirectional=bidirectional, pad_idx=pad_index ).to(device)
 optimizer = torch.optim.Adam(model.parameters() )<choose_model_class>
y_train = train_data["Survived"]
features = ['age_fill','Pclass', 'Sex','Deck','Fare_new','Embarked_fill','SibSp','Parch', 'relatives_code']
X_test = test_data[features].copy()
X_train = train_data[features].copy()
model = CharRNNClassifier(ntokens, embedding_size, hidden_size, nlabels, bidirectional=bidirectional ).to(device)
 optimizer = torch.optim.Adam(model.parameters() )<train_model>
X_train["Sex"] = X_train["Sex"].cat.codes
X_test["Sex"] = X_test["Sex"].cat.codes
X_train["Embarked_fill"] = X_train["Embarked_fill"].cat.codes
X_test["Embarked_fill"] = X_test["Embarked_fill"].cat.codes
X_train["Deck"] = X_train["Deck"].cat.codes
X_test["Deck"] = X_test["Deck"].cat.codes
print(f'Training final model for {epochs} epochs')
 for epoch in range(1, epochs + 1):
print(f'| epoch {epoch:03d} | train accuracy={train(model, optimizer, train_data + val_data, batch_size, token_size, log=epoch==1):.3f}' )<train_model>
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, y_train)
 predictions = model.predict(X_test)
def test(model, data, batch_size, token_size):
model.eval()
sindex = []
labels = []
with torch.no_grad() :
for batch in pool_generator(data, batch_size, token_size):
X = [torch.from_numpy(d[0])for d in batch]
X_lengths = torch.tensor([x.numel() for x in X], dtype=torch.long, device=device)
 X = torch.nn.utils.rnn.pad_sequence(X ).to(device)
 answer = model(X, X_lengths)
 label = torch.max(answer, 1)[1].cpu().numpy()
labels.append(label)
 sindex += [d[1] for d in batch]
return np.array(sindex), np.concatenate(labels )<string_transform>
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )