Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<choose_model_class>
import os
import numpy as np
import pandas as pd
from kaggle_datasets import KaggleDatasets
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
from tensorflow.keras.callbacks import ModelCheckpoint
import transformers
from transformers import TFAutoModel, AutoTokenizer
from tqdm.notebook import tqdm
import plotly.express as px
classifier = DecisionTreeClassifier()
classifier.fit(x_train, y_train)
 y_pred = classifier.predict(test )<predict_on_test>
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 )
y_pred = classifier.predict(test)
 acc_Tree = cross_val_score(classifier, x_train, y_train, cv=10, scoring='accuracy' ).mean()
acc_Tree<train_model>
n_epochs = 250
max_len = 80
batch_size = 16 * strategy.num_replicas_in_sync
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
 classifier.fit(x_train, y_train )<predict_on_test>
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' )
y_pred = classifier.predict(test)
 acc_Tree = cross_val_score(classifier, x_train, y_train, cv=10, scoring='accuracy' ).mean()
acc_Tree<predict_on_test>
tokenizer = AutoTokenizer.from_pretrained('jplu/tf-xlm-roberta-large' )
classifier = SVC()
classifier.fit(x_train, y_train)
 y_pred = classifier.predict(test )<predict_on_test>
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
)
y_pred = classifier.predict(test)
 acc_Tree = cross_val_score(classifier, x_train, y_train, cv=10, scoring='accuracy' ).mean()
acc_Tree<prepare_output>
x_train, x_valid, y_train, y_valid = train_test_split(
 train_encoded['input_ids'], train.label.values,
test_size=0.20, random_state=2020 
)
 x_test = test_encoded['input_ids']
accuracy = {'Model' : ['Logistic Regression', 'K- Nearest Neighbor', 'SVC', 'Decision Tree', 'Random Forest'],
'Accuracy' : [0.7890, 0.8047, 0.8226, 0.7935, 0.8037]
}
all_cross_val_scores = pd.DataFrame(accuracy, columns = ['Model', 'Accuracy'])
 all_cross_val_scores.head()<save_to_csv>
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)
 )
test_df = pd.read_csv('.. /input/titanic/test.csv')
 submission = pd.DataFrame({
'PassengerId': test_df['PassengerId'],
'Survived': y_pred
})
 submission.to_csv('titanic_prediction.csv', index=False)
 print('File Saved' )<import_modules>
with strategy.scope() :
transformer_encoder = TFAutoModel.from_pretrained('jplu/tf-xlm-roberta-large')
 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-3),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] 
)
 model.summary()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
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
)
train = pd.read_csv('.. /input/titanic/train.csv')
 train.head()<define_variables>
test_preds = model.predict(test_dataset, verbose=1)
 submission['prediction'] = test_preds.argmax(axis=1 )
<feature_engineering><EOS> 
submission.to_csv('submission.csv', index=False)