Kaggle competition: Contradictory, My Dear Watson
%%time
!cp.. /input/rapids/rapids.0.13.0 /opt/conda/envs/rapids.tar.gz
!cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz
sys.path = ["/opt/conda/envs/rapids/lib"] + ["/opt/conda/envs/rapids/lib/python3.6"] + ["/opt/conda/envs/rapids/lib/python3.6/site-packages"] + sys.path
!cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/<import_modules>
os.environ["WANDB_API_KEY"] = "0"
import cupy, cudf, cuml
from cuml.linear_model import LogisticRegression
from cuml.ensemble import RandomForestClassifier
from cuml.svm import SVC
import os<load_from_csv>
SEED=42
max_len = 48
EPOCHS = 3
BATCH_SIZE = 64
LR = 1e-5
train = cudf.read_csv('/kaggle/input/titanic/train.csv')
 test = cudf.read_csv('/kaggle/input/titanic/test.csv' )<drop_column>
from transformers import BertTokenizer, TFBertModel,TFAutoModel,TFXLMRobertaModel, AutoTokenizer
import matplotlib.pyplot as plt
import tensorflow as tf
train = train.drop(columns= ['Name','Ticket','Cabin'])
 test = test.drop(columns= ['Name','Ticket','Cabin'] )<data_type_conversions>
train = pd.read_csv(".. /input/contradictory-my-dear-watson/train.csv" )
train['Embarked_S'] =(train['Embarked'] == 'S' ).astype(int)
 train['Embarked_C'] =(train['Embarked'] == 'C' ).astype(int)
 train['Embarked_Q'] =(train['Embarked'] == 'Q' ).astype(int)
 train['Gender'] =(train['Sex'] == 'male' ).astype(int )<data_type_conversions>
model_name = 'jplu/tf-xlm-roberta-large'
tokenizer = AutoTokenizer.from_pretrained(model_name)
test['Embarked_S'] =(test['Embarked'] == 'S' ).astype(int)
 test['Embarked_C'] =(test['Embarked'] == 'C' ).astype(int)
 test['Embarked_Q'] =(test['Embarked'] == 'Q' ).astype(int)
 test['Gender'] =(test['Sex'] == 'male' ).astype(int )<drop_column>
def encode_sentence(s):
tokens = list(tokenizer.tokenize(s))
tokens.append('[SEP]')
 return tokenizer.convert_tokens_to_ids(tokens )
train = train.drop(columns= ['Embarked','Sex',])
 test = test.drop(columns= ['Embarked','Sex',])
<train_model>
encode_sentence("I love machine learning" )
train.fillna(0,inplace=True)
 test.fillna(0,inplace=True )<prepare_x_and_y>
def bert_encode(hypotheses, premises, tokenizer):
num_examples = len(hypotheses)
 sentence1 = tf.ragged.constant([
encode_sentence(s)
 for s in np.array(hypotheses)])
 sentence2 = tf.ragged.constant([
encode_sentence(s)
 for s in np.array(premises)])
 cls = [tokenizer.convert_tokens_to_ids(['[CLS]'])]*sentence1.shape[0]
input_word_ids = tf.concat([cls, sentence1, sentence2], axis=-1)
 input_mask = tf.ones_like(input_word_ids ).to_tensor()
type_cls = tf.zeros_like(cls)
 type_s1 = tf.zeros_like(sentence1)
 type_s2 = tf.ones_like(sentence2)
 input_type_ids = tf.concat(
 [type_cls, type_s1, type_s2], axis=-1 ).to_tensor()
inputs = {
'input_word_ids': input_word_ids.to_tensor() ,
'input_mask': input_mask,
'input_type_ids': input_type_ids}
return inputs
X = train.drop(columns = ['Survived'])
 y = train['Survived'].astype('int32' )<train_model>
train_input = bert_encode(train.premise.values, train.hypothesis.values, tokenizer )
model = RandomForestClassifier(n_estimators = 100, max_depth = 6)
 model.fit(X, y )<predict_on_test>
def build_model() :
bert_encoder = TFXLMRobertaModel.from_pretrained(model_name)
 input_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 input_type_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_type_ids")
 embedding = bert_encoder([input_word_ids, input_mask, input_type_ids])[0]
output = tf.keras.layers.Dense(3, activation='softmax' )(embedding[:,0,:])
 model = tf.keras.Model(inputs=[input_word_ids, input_mask, input_type_ids], outputs=output)
 model.compile(tf.keras.optimizers.Adam(lr=LR), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 return model
yhat_train = model.predict(X, predict_model = 'CPU')
 yhat_test = model.predict(test, predict_model = 'CPU' )<count_values>
eas = EarlyStopping(monitor='val_loss', min_delta=0.001, patience=3,
verbose=1, mode='min', baseline=None, restore_best_weights=True )
print(sum(y == yhat_train)/ len(y))<save_to_csv>
model.fit(train_input, train.label.values, epochs = EPOCHS, verbose = 1, batch_size = BATCH_SIZE, validation_split = 0.2, callbacks = [eas])
submission = cudf.DataFrame({'PassengerId': test.PassengerId, 'Survived': yhat_test})
 submission.to_csv('submission.csv', index = False )<load_from_csv>
test = pd.read_csv(".. /input/contradictory-my-dear-watson/test.csv")
 test_input = bert_encode(test.premise.values, test.hypothesis.values, tokenizer )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head() 
<load_from_csv>
predictions = [np.argmax(i)for i in model.predict(test_input)]
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head(20 )<feature_engineering>
submission = test.id.copy().to_frame()
submission['prediction'] = predictions
<feature_engineering><EOS> 
submission.to_csv("submission.csv", index = False )