Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<feature_engineering>
!pip install git+https://github.com/ssut/py-googletrans.git
df_test.loc[(df_test.Sex == 'male')&(df_test.Fare_bin == 11), 'Survived'] = 1<save_to_csv>
import numpy as np
import pandas as pd
from googletrans import Translator
from dask import bag, diagnostics
<drop_column>
train = pd.read_csv('.. /input/contradictory-my-dear-watson/train.csv', index_col=['id'])
 display(train, train.lang_abv.value_counts() )
df_test.drop(['Survived'],axis=1,inplace=True )<create_dataframe>
def translate(words, dest):
dest_choices = ['zh-cn',
'ar',
'fr',
'sw',
'ur',
'vi',
'ru',
'hi',
'el',
'th',
'es',
'de',
'tr',
'bg'
]
if not dest:
dest = np.random.choice(dest_choices)
 translator = Translator()
decoded = translator.translate(words, dest=dest ).text
return decoded
def trans_parallel(df, dest):
premise_bag = bag.from_sequence(df.premise.tolist() ).map(translate, dest)
 hypo_bag = bag.from_sequence(df.hypothesis.tolist() ).map(translate, dest)
 with diagnostics.ProgressBar() :
premises = premise_bag.compute()
hypos = hypo_bag.compute()
df[['premise', 'hypothesis']] = list(zip(premises, hypos))
return df
eng = train.loc[train.lang_abv == "en"].copy() \ 
.pipe(trans_parallel, dest=None)
 non_eng = train.loc[train.lang_abv != "en"].copy() \ 
.pipe(trans_parallel, dest='en')
 train = train.append([eng, non_eng])
 train.shape
df_train_ml = df_train.copy()
df_test_ml = df_test.copy()<categorify>
test = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/test.csv', index_col=['id'])
 submission = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/sample_submission.csv',
index_col=['id'] )
df_train_ml = pd.get_dummies(df_train_ml, columns=['Sex', 'Embarked', 'Pclass'], drop_first=True)
 df_train_ml.drop(['PassengerId','Name','Ticket', 'Cabin', 'Age_bin', 'Fare_bin'],axis=1,inplace=True)
 df_train_ml.dropna(inplace=True )<categorify>
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
import transformers
from transformers import TFAutoModel, AutoTokenizer
passenger_id = df_test_ml['PassengerId']
df_test_ml = pd.get_dummies(df_test_ml, columns=['Sex', 'Embarked', 'Pclass'], drop_first=True)
 df_test_ml.drop(['PassengerId','Name','Ticket', 'Cabin', 'Age_bin', 'Fare_bin'],axis=1,inplace=True )<normalization>
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print('Running on TPU ', tpu.master())
 except ValueError:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )
scaler = StandardScaler()
scaler.fit(df_train_ml.drop('Survived',axis=1))
scaled_features = scaler.transform(df_train_ml.drop('Survived',axis=1))
df_train_ml_sc = pd.DataFrame(scaled_features, columns=df_train_ml.columns[:-1])
 df_test_ml.fillna(df_test_ml.mean() , inplace=True)
 scaled_features = scaler.transform(df_test_ml)
 df_test_ml_sc = pd.DataFrame(scaled_features, columns=df_test_ml.columns )<split>
model_name = 'jplu/tf-xlm-roberta-large'
n_epochs = 8
max_len = 80
batch_size = 16 * strategy.num_replicas_in_sync
X_train, X_test, y_train, y_test = train_test_split(df_train_ml.drop('Survived',axis=1), df_train_ml['Survived'], test_size=0.30, random_state=101)
 X_train_sc, X_test_sc, y_train_sc, y_test_sc = train_test_split(df_train_ml_sc, df_train_ml['Survived'], test_size=0.30, random_state=101 )<prepare_x_and_y>
tokenizer = AutoTokenizer.from_pretrained(model_name)
 train_text = train[['premise', 'hypothesis']].values.tolist()
test_text = test[['premise', 'hypothesis']].values.tolist()
train_encoded = tokenizer.batch_encode_plus(
 train_text,
pad_to_max_length=True,
max_length=max_len 
)
 test_encoded = tokenizer.batch_encode_plus(
 test_text,
pad_to_max_length=True,
max_length=max_len 
)
 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 
)
 x_test = test_encoded['input_ids']
X_train_all = df_train_ml.drop('Survived',axis=1)
 y_train_all = df_train_ml['Survived']
X_test_all = df_test_ml
X_train_all_sc = df_train_ml_sc
y_train_all_sc = df_train_ml['Survived']
X_test_all_sc = df_test_ml_sc<correct_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)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_valid, y_valid)) 
.batch(batch_size)
.cache() 
.prefetch(auto)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test)
.batch(batch_size)
 )
X_test_all.fillna(X_test_all.mean() , inplace=True)
 print("*" )<import_modules>
with strategy.scope() :
transformer_encoder = TFAutoModel.from_pretrained(model_name)
 input_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_ids")
 sequence_output = transformer_encoder(input_ids)[0]
cls_token = sequence_output[:, 0, :]
out = Dense(3, activation='softmax' )(cls_token)
 model = Model(inputs=input_ids, outputs=out)
 model.compile(
 Adam(lr=1e-5),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix<compute_train_metric>
n_steps = len(x_train)// batch_size
train_history = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
epochs=n_epochs
)
logreg = LogisticRegression()
logreg.fit(X_train,y_train)
 pred_logreg = logreg.predict(X_test)
 print(confusion_matrix(y_test, pred_logreg))
print(classification_report(y_test, pred_logreg))
print(accuracy_score(y_test, pred_logreg))<predict_on_test>
train_history = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
epochs=1
)
logreg.fit(X_train_all, y_train_all)
 pred_all_logreg = logreg.predict(X_test_all )<create_dataframe>
test_preds = model.predict(test_dataset, verbose=1)
 submission['prediction'] = test_preds.argmax(axis=1)
 submission.to_csv("submission.csv" )