Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<predict_on_test>
!pip install googletrans textAugment
classifier = SVC(kernel='rbf',random_state=0)
 classifier.fit(X_train,y_train)
 y_pred = classifier.predict(X_test)
 cm = confusion_matrix(y_test,y_pred)
 print(cm )<save_to_csv>
os.environ["WANDB_API_KEY"] = "0"
y_pred_test = classifier.predict(X_real_test)
 pid = testdata[['PassengerId']].values
res = np.expand_dims(y_pred_test,axis=1)
 f = np.hstack(( pid,res))
df = pd.DataFrame(f, columns = ['PassengerId', 'Survived'])
 df.to_csv('gender_submission.csv', index=False )<set_options>
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('Number of replicas:', strategy.num_replicas_in_sync )
pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.3f}'.format 
<load_from_csv>
df_train = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/train.csv')
 df_test = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/test.csv' )
dataframe = pd.read_csv(".. /input/train.csv", sep=",")
 dataframe.head(10)
<create_dataframe>
def clean_word(value):
language = value[0]
word = value[1]
if language != 'English':
word = word.lower()
return word
word = word.lower()
word = re.sub(r'\?\?', 'e', word)
 word = re.sub('\.\.\.', '.', word)
 word = re.sub('\/', ' ', word)
 word = re.sub('--', ' ', word)
 word = re.sub('/\xad', '', word)
 word = word.strip(' ')
 return word
df_train['premise'] = df_train[['language', 'premise']].apply(lambda v: clean_word(v), axis=1)
 df_train['hypothesis'] = df_train[['language', 'hypothesis']].apply(lambda v: clean_word(v), axis=1)
 df_test['premise'] = df_test[['language', 'premise']].apply(lambda v: clean_word(v), axis=1)
 df_test['hypothesis'] = df_test[['language', 'hypothesis']].apply(lambda v: clean_word(v), axis=1)
 languages = [ 'zh-cn' if lang == 'zh' else lang for lang in df_train['lang_abv'].unique() ]
print('null value summary:')
 dataframe.isnull().sum().sort_values(ascending=False)
<create_dataframe>
seed = 42
tf.random.set_seed(seed)
 model_name = 'jplu/tf-xlm-roberta-large'
tokenizer = XLMRobertaTokenizer.from_pretrained(model_name)
 def build_model() :
with strategy.scope() :
bert_encoder = TFXLMRobertaModel.from_pretrained(model_name)
 input_word_ids = tf.keras.Input(shape=(None,), dtype=tf.int32, name="input_word_ids")
 input_mask = tf.keras.Input(shape=(None,), dtype=tf.int32, name="input_mask")
 embedding = bert_encoder([input_word_ids, input_mask])[0]
output_layer = tf.keras.layers.Dropout(0.25 )(embedding)
 output_layer = tf.keras.layers.GlobalAveragePooling1D()(output_layer)
 output_dense_layer = tf.keras.layers.Dense(64, activation='relu' )(output_layer)
 output_dense_layer = tf.keras.layers.Dense(32, activation='relu' )(output_dense_layer)
 output = tf.keras.layers.Dense(3, activation='softmax' )(output_dense_layer)
 model = tf.keras.Model(inputs=[input_word_ids, input_mask], outputs=output)
 model.compile(tf.keras.optimizers.Adam(lr=1e-5), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 return model
with strategy.scope() :
model = build_model()
model.summary()
dataframe = dataframe.drop(columns=['PassengerId', 'Name', 'Ticket', 'Cabin', 'Fare'])
<categorify>
batch_size = 8 * strategy.num_replicas_in_sync
num_splits = 5
test_input = None
def transform_sex(dataframe):
column_name = 'Sex'
genders = {"female": 0, "male": 1}
return dataframe[column_name].map(genders)
<categorify>
auto = tf.data.experimental.AUTOTUNE
def make_dataset(train_input, train_label):
dataset = tf.data.Dataset.from_tensor_slices(
(
 train_input,
train_label 
)
 ).repeat().shuffle(batch_size ).batch(batch_size ).prefetch(auto)
 return dataset
def xlm_roberta_encode(hypotheses, premises, src_langs, augmentation=False):
num_examples = len(hypotheses)
 sentence_1 = [tokenizer.encode(s)for s in premises]
sentence_2 = [tokenizer.encode(s)for s in hypotheses]
input_word_ids = list(map(lambda x: x[0]+x[1], list(zip(sentence_1,sentence_2))))
input_mask = [np.ones_like(x)for x in input_word_ids]
inputs = {
'input_word_ids': tf.keras.preprocessing.sequence.pad_sequences(input_word_ids, padding='post'),
'input_mask': tf.keras.preprocessing.sequence.pad_sequences(input_mask, padding='post')
 }
return inputs
train_df, validation_df = train_test_split(df_train, test_size=0.1)
 df_train['prediction'] = 0
num_augmentation = 1
train_input = xlm_roberta_encode(train_df.hypothesis.values,train_df.premise.values, train_df.lang_abv.values, augmentation=False)
 train_label = train_df.label.values
train_sequence = make_dataset(train_input, train_label)
 validation_input = xlm_roberta_encode(validation_df.hypothesis.values, validation_df.premise.values,validation_df.lang_abv.values, augmentation=False)
 validation_label = validation_df.label.values
tf.keras.backend.clear_session()
column_name = 'Sex'
dataframe[column_name] = transform_sex(dataframe)
 dataframe.head(10)
<create_dataframe>
train_df, validation_df = train_test_split(df_train, test_size=0.1)
 df_train['prediction'] = 0
num_augmentation = 1
train_input = xlm_roberta_encode(train_df.hypothesis.values,train_df.premise.values, train_df.lang_abv.values, augmentation=False)
 train_label = train_df.label.values
train_sequence = make_dataset(train_input, train_label)
 validation_input = xlm_roberta_encode(validation_df.hypothesis.values, validation_df.premise.values,validation_df.lang_abv.values, augmentation=False)
 validation_label = validation_df.label.values
tf.keras.backend.clear_session()
def transform_embarked(dataframe):
column_name = 'Embarked'
dataframe[column_name].fillna('S')
 dataframe = pd.concat(
 [
dataframe,
pd.get_dummies(dataframe[column_name], prefix=column_name)
 ],
axis=1)
 dataframe = dataframe.drop(columns=[column_name, 'Embarked_Q'])
 return dataframe 
<create_dataframe>
n_steps =(len(train_label)) // batch_size
with strategy.scope() :
history = model.fit(
 train_sequence, shuffle=True, steps_per_epoch=n_steps,
validation_data =(validation_input, validation_label), epochs=50, verbose=1,
callbacks=[
tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=10),
tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', factor=0.1, patience=5),
tf.keras.callbacks.ModelCheckpoint(
 'model.h5', monitor='val_accuracy', save_best_only=True,save_weights_only=True)
 ] 
)
 model.load_weights('model.h5')
 validation_predictions = model.predict(validation_input)
 validation_predictions = np.argmax(validation_predictions, axis=-1)
 validation_df['predictions'] = validation_predictions
acc = accuracy_score(validation_label, validation_predictions)
 print('Accuracy: {}'.format(acc))
<categorify><EOS> 
if test_input is None:
test_input = xlm_roberta_encode(df_test.hypothesis.values, df_test.premise.values, df_test.lang_abv.values,augmentation=False)
 test_split_predictions = model.predict(test_input)
 predictions = np.argmax(test_split_predictions, axis=-1)
 submission = df_test.id.copy().to_frame()
submission['prediction'] = predictions
submission.head()
submission.to_csv("submission.csv", index = False )