Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<categorify>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import tensorflow as tf
from transformers import AutoTokenizer, TFAutoModel
from tqdm.notebook import tqdm
titanic_df['age_class'] = titanic_df['Age'] * titanic_df['Pclass']
test_df['age_class'] = test_df['Age'] * test_df['Pclass']<prepare_x_and_y>
!pip install nlp
X_train = titanic_df.drop("Survived",axis=1)
 Y_train = titanic_df["Survived"]
X_test = test_df.drop("PassengerId",axis=1 ).copy()<compute_train_metric>
MODEL_NAME = 'jplu/tf-xlm-roberta-large'
EPOCHS = 10
MAX_LEN = 80
RATE = 1e-5
BATCH_SIZE = 64 * strategy.num_replicas_in_sync
<train_on_grid>
train = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/train.csv')
 test = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/test.csv')
 submission = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/sample_submission.csv' )
<train_model>
train = train[['premise', 'hypothesis', 'label']]
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, Y_train)
 Y_pred_1 = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train )<train_on_grid>
multigenre_data = nlp.load_dataset(path='glue', name='mnli' )
<train_on_grid>
index = []
premise = []
hypothesis = []
label = []
for example in multigenre_data['train']:
premise.append(example['premise'])
 hypothesis.append(example['hypothesis'])
 label.append(example['label'] )
<data_type_conversions>
multigenre_df = pd.DataFrame(data={
'premise': premise,
'hypothesis': hypothesis,
'label': label
} )
Y_pred = Y_pred_1<save_to_csv>
adversarial_data = nlp.load_dataset(path='anli' )
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('titanic.csv', index=False )<load_from_csv>
index = []
premise = []
hypothesis = []
label = []
for example in adversarial_data['train_r1']:
premise.append(example['premise'])
 hypothesis.append(example['hypothesis'])
 label.append(example['label'])
 for example in adversarial_data['train_r2']:
premise.append(example['premise'])
 hypothesis.append(example['hypothesis'])
 label.append(example['label'])
 for example in adversarial_data['train_r3']:
premise.append(example['premise'])
 hypothesis.append(example['hypothesis'])
 label.append(example['label'] )
train_df = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_df = pd.read_csv("/kaggle/input/titanic/test.csv" )<count_missing_values>
adversarial_df = pd.DataFrame(data={
'premise': premise,
'hypothesis': hypothesis,
'label': label
} )
train_df.isnull().sum()<count_missing_values>
train = pd.concat([train, multigenre_df, adversarial_df] )
test_df.isnull().sum()<count_missing_values>
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME )
train_df.isnull().sum()<count_missing_values>
train_text = train[['premise', 'hypothesis']].values.tolist()
test_text = test[['premise', 'hypothesis']].values.tolist()
train_df.drop('Cabin', axis=1, inplace=True)
 train_df.isnull().sum()<count_missing_values>
train_encoded = tokenizer.batch_encode_plus(
 train_text,
pad_to_max_length=True,
max_length=MAX_LEN
)
train_df.dropna(subset=['Embarked'], inplace=True)
 train_df.isnull().sum()<data_type_conversions>
test_encoded = tokenizer.batch_encode_plus(
 test_text,
pad_to_max_length=True,
max_length=MAX_LEN
)
train_df['Age'].fillna(train_df['Age'].mean() , inplace=True)
 train_df.isnull().sum()<count_missing_values>
vocab = tokenizer.get_vocab()
print(vocab['
'])
 print(vocab['▁and'])
 print(vocab['▁these'])
 print(vocab['▁comments'])
 print(vocab['▁were'])
 print(vocab['▁considered'])
 print(vocab['▁in'])
 print(vocab['▁formula'])
 print(vocab['ting'])
 print(vocab['▁the'])
 print(vocab['▁inter'])
 print(vocab['im'])
 print(vocab['▁rules'])
 print(vocab['.'] )
test_df.isnull().sum()<count_missing_values>
x_train, x_valid, y_train, y_valid = train_test_split(
 train_encoded['input_ids'],
train.label.values,
test_size=0.2,
random_state=2020
)
test_df.drop('Cabin', axis=1, inplace=True)
 test_df.isnull().sum()<feature_engineering>
x_test = test_encoded['input_ids']
test_df['Fare'] = test_df['Fare'].fillna(train_df['Fare'].median())
 test_df.isnull().sum()<count_missing_values>
auto = tf.data.experimental.AUTOTUNE
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_train, y_train)) 
.repeat() 
.shuffle(2048)
.batch(BATCH_SIZE)
.prefetch(auto)
 )
test_df['Age'].fillna(train_df['Age'].mean() , inplace=True)
 test_df.isnull().sum()<prepare_x_and_y>
valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_valid, y_valid)) 
.batch(BATCH_SIZE)
.cache() 
.prefetch(auto)
 )
X = train_df.drop(['PassengerId', 'Survived', 'Name', 'Ticket', 'Fare'], axis=1)
 y = train_df['Survived']
n_test_df = test_df.drop(['PassengerId', 'Name', 'Ticket', 'Fare'], axis=1 )<categorify>
test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test)
.batch(BATCH_SIZE)
 )
X = pd.get_dummies(X, drop_first=True )<categorify>
with strategy.scope() :
backbone = TFAutoModel.from_pretrained(MODEL_NAME )
n_test_df = pd.get_dummies(n_test_df, drop_first=True )<split>
with strategy.scope() :
x_input = tf.keras.Input(shape=(MAX_LEN,), dtype=tf.int32, name="input_ids")
 x = backbone(x_input)[0]
x = x[:, 0, :]
x = tf.keras.layers.Dense(3, activation='softmax' )(x)
 model = tf.keras.models.Model(inputs=x_input, outputs=x )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42 )<define_search_space>
model.compile(
 tf.keras.optimizers.Adam(lr=RATE),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
parameters = {"learning_rate": [0.1, 0.01, 0.001],
"gamma" : [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2],
"max_depth": [2, 4, 7, 10],
"colsample_bytree": [0.3, 0.6, 0.8, 1.0],
"subsample": [0.2, 0.4, 0.5, 0.6, 0.7],
"reg_alpha": [0, 0.5, 1],
"reg_lambda": [1, 1.5, 2, 3, 4.5],
"min_child_weight": [1, 3, 5, 7],
"n_estimators": [100, 250, 500, 1000]}
xgb_rscv = RandomizedSearchCV(XGBClassifier() , param_distributions = parameters, cv = 7, verbose = 3, random_state = 40)
 model_rscv = xgb_rscv.fit(X_train, y_train)
 model_rscv.best_params_<train_model>
steps = len(x_train)// BATCH_SIZE
history = model.fit(
 train_dataset,
validation_data=valid_dataset,
epochs=EPOCHS,
steps_per_epoch=steps,
)
tuned_model = XGBClassifier(booster='gbtree', subsample= 0.7,
reg_lambda= 3,
reg_alpha= 1,
n_estimators= 100,
min_child_weight= 3,
max_depth= 10,
learning_rate = 0.001,
gamma= 0.01,
colsample_bytree= 0.6)
 tuned_model.fit(X_train, y_train )<compute_test_metric>
test_preds = model.predict(test_dataset, verbose=1)
 submission['prediction'] = test_preds.argmax(axis=1 )
tuned_model.score(X_test, y_test )<save_to_csv>
submission.to_csv('submission.csv', index=False )
predictions = tuned_model.predict(n_test_df)
 output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<install_modules>
submission.to_csv('submission.csv', index=False )