Kaggle competition: Ghouls, Goblins, and Ghosts... Boo!
<SOS> metric: categorizationaccuracy Kaggle data source: ghouls,-goblins,-and-ghosts...-boo!<create_dataframe>
import pandas as pd
import tensorflow as tf
from tensorflow.contrib import learn
def model_test(test_inputs, x_test, label_columns, weight_files):
batch_size = 6
test_dataset = QuestDataset(test_inputs, x_test, None)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
 num_features = x_test.shape[1]
num_labels = len(label_columns)
 for fname in weight_files:
model = NeuralNet3(num_features, num_labels, pretrained_bert)
 model.cuda()
model.load_state_dict(torch.load(fname))
model.eval()
test_preds_fold = []
with torch.no_grad() :
for ids, masks, segments, features in test_loader:
ids = ids.cuda()
masks = masks.cuda()
segments = segments.cuda()
features = torch.tensor(features,dtype=torch.float32 ).cuda()
y_pred = model(ids, masks, segments, features)
 test_preds_fold.extend(torch.sigmoid(y_pred ).cpu().data.numpy().tolist())
 test_preds_fold = np.array(test_preds_fold)
 add_test_pred_data(test_preds_fold, label_columns, 1.0)
 del model
torch.cuda.empty_cache()
gc.collect()
del test_dataset, test_loader<create_dataframe>
df = pd.read_csv(".. /input/train.csv", index_col='id' )
def model_validation(train_inputs, x_train, label_columns, weight_files):
if len(kfold_split)!= len(weight_files):
return
batch_size = 6
num_features = x_test.shape[1]
num_labels = len(label_columns)
 for k,(train_idx, valid_idx)in enumerate(kfold_split):
x_train_valid = x_train[valid_idx]
train_inputs_valid = [x[valid_idx] for x in train_inputs]
valid_dataset = QuestDataset(train_inputs_valid, x_train_valid, None)
 valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False, drop_last=False)
 model = NeuralNet3(num_features, num_labels, pretrained_bert)
 model.cuda()
fname = weight_files[k]
model.load_state_dict(torch.load(fname))
model.eval()
valid_preds_fold = []
with torch.no_grad() :
for ids, masks, segments, features in valid_loader:
ids = ids.cuda()
masks = masks.cuda()
segments = segments.cuda()
features = torch.tensor(features,dtype=torch.float32 ).cuda()
y_pred = model(ids, masks, segments, features)
 valid_preds_fold.extend(torch.sigmoid(y_pred ).cpu().data.numpy().tolist())
 valid_preds_fold = np.array(valid_preds_fold)
 add_validation_data(valid_preds_fold, label_columns, valid_idx)
 del model, valid_dataset, valid_loader
torch.cuda.empty_cache()
gc.collect()<create_dataframe>
features = ["bone_length","rotting_flesh","hair_length","has_soul"]
X = df[features]
y = df["type"]
X['hair_soul'] = X['hair_length'] * X['has_soul']
X['hair_bone'] = X['hair_length'] * X['bone_length']
X['hair_soul_bone'] = X['hair_length'] * X['has_soul'] * X['bone_length']
features+=['hair_soul','hair_bone','hair_soul_bone']
X.head()
def model_test_validation(label_columns, train_inputs, test_inputs, x_train, x_test, weight_files):
if len(kfold_split)!= len(weight_files):
return
batch_size = 6
num_features = x_test.shape[1]
num_labels = len(label_columns)
 test_dataset = QuestDataset(test_inputs, x_test, None)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
 for k,(train_idx, valid_idx)in enumerate(kfold_split):
fname = weight_files[k]
print(k+1, fname)
 x_train_valid = x_train[valid_idx]
train_inputs_valid = [x[valid_idx] for x in train_inputs]
valid_dataset = QuestDataset(train_inputs_valid, x_train_valid, None)
 valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False, drop_last=False)
 model = NeuralNet3(num_features, num_labels, pretrained_bert)
 model.cuda()
model.load_state_dict(torch.load(fname))
model.eval()
valid_preds_fold = []
with torch.no_grad() :
for ids, masks, segments, features in valid_loader:
ids = ids.cuda()
masks = masks.cuda()
segments = segments.cuda()
features = torch.tensor(features,dtype=torch.float32 ).cuda()
y_pred = model(ids, masks, segments, features)
 valid_preds_fold.extend(torch.sigmoid(y_pred ).cpu().data.numpy().tolist())
 valid_preds_fold = np.array(valid_preds_fold)
 add_validation_data(valid_preds_fold, label_columns, valid_idx)
 test_preds_fold = []
with torch.no_grad() :
for ids, masks, segments, features in test_loader:
ids = ids.cuda()
masks = masks.cuda()
segments = segments.cuda()
features = torch.tensor(features,dtype=torch.float32 ).cuda()
y_pred = model(ids, masks, segments, features)
 test_preds_fold.extend(torch.sigmoid(y_pred ).cpu().data.numpy().tolist())
 test_preds_fold = np.array(test_preds_fold)
 add_test_pred_data(test_preds_fold, label_columns, 1.0)
 del model, valid_dataset, valid_loader
torch.cuda.empty_cache()
gc.collect()
del test_dataset, test_loader<load_pretrained>
letype = LE()
y = letype.fit_transform(y )
tokenizer = transformers.GPT2Tokenizer.from_pretrained(pretrained_bert )<train_model>
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.0 )
print('time: {}'.format(time_to_str(( time.time() -start_time_all), 'min')) )<categorify>
x=tf.contrib.learn.infer_real_valued_columns_from_input(X_train)
 tf_clf_dnn = learn.DNNClassifier(hidden_units=[16], n_classes=3, feature_columns=x, activation_fn=tf.sigmoid)
 tf_clf_dnn.fit(X_train, y_train,max_steps=5000)
 print(acc_s(y_train,tf_clf_dnn.predict(X_train)) )
<train_model><EOS> 
test_df = pd.read_csv(".. /input/test.csv",index_col='id')
 features = ["bone_length","rotting_flesh","hair_length","has_soul"]
X = test_df[features]
X['hair_soul'] = X['hair_length'] * X['has_soul']
X['hair_bone'] = X['hair_length'] * X['bone_length']
X['hair_soul_bone'] = X['hair_length'] * X['has_soul'] * X['bone_length']
features+=['hair_soul','hair_bone','hair_soul_bone']
pred = tf_clf_dnn.predict(X)
 pred = letype.inverse_transform(pred)
 submission_df = pd.DataFrame({'type':pred})
 submission_df['id'] = test_df.index
submission_df = submission_df[['id', 'type']].set_index('id')
 submission_df.to_csv('tf_pred.csv')
 submission_df.head()