Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<choose_model_class>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import re
import plotly.express as px
import plotly.figure_factory as ff
import plotly.graph_objects as go
from tqdm import tqdm
import tensorflow as tf
from tensorflow import keras
from keras import backend as k
from keras.utils import to_categorical
import transformers
lr = LinearRegression(
 n_jobs = -1 
)
 rd = Ridge(
 alpha = 4.84 
)
 rf = RandomForestRegressor(
 n_estimators = 12,
max_depth = 3,
n_jobs = -1 
)
 gb = GradientBoostingRegressor(
 n_estimators = 40,
max_depth = 2 
)
 nn = MLPRegressor(
 hidden_layer_sizes =(90, 90),
alpha = 2.75
)<train_model>
DEVICE = 'TPU'
model = StackingRegressor(
 regressors=[rf, gb, nn, rd],
meta_regressor=lr 
)
 model.fit(X_train, y )<predict_on_test>
if DEVICE == 'TPU':
print('Connecting to TPU...')
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU :',tpu.master())
 except ValueError:
print('Could not connect to TPU')
 tpu = None
if tpu:
try:
print('Initializing TPU...')
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print('TPU initialized!')
 except _:
print('Failed to initialized TPU')
 else:
DEVICE='GPU'
if DEVICE != 'TPU':
print('Using default strategy for CPU and single GPU')
 strategy = tf.distribute.get_strategy()
if DEVICE == 'GPU':
print('Num GPUs available : ',len(tf.config.experimental.list_physical_devices('GPU')))
 AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print('REPLICAS : ',REPLICAS )
y_pred = model.predict(X_train)
 print(sqrt(mean_squared_error(y, y_pred)) )<predict_on_test>
Batch_size = 16 * strategy.num_replicas_in_sync
epochs = 13
AUTO = tf.data.experimental.AUTOTUNE
MODEL = 'jplu/tf-xlm-roberta-large'
Y_pred = model.predict(X_test )<save_to_csv>
train = pd.read_csv(r'.. /input/contradictory-my-dear-watson/train.csv')
 test = pd.read_csv(r'.. /input/contradictory-my-dear-watson/test.csv')
 submission = pd.read_csv(r'.. /input/contradictory-my-dear-watson/sample_submission.csv' )
sub = pd.DataFrame()
sub['Id'] = df_test['Id']
sub['SalePrice'] = np.expm1(Y_pred)
 sub.to_csv('submission.csv',index=False )<load_from_csv>
test_num_lang = test.groupby('language')['id'].count().sort_values(ascending=False ).reset_index()
test_num_lang = pd.DataFrame(test_num_lang)
 test_num_lang['count'] = test_num_lang['id']
test_num_lang = test_num_lang.drop('id',axis=1)
 test_num_lang_data = test_num_lang.style.background_gradient(cmap='Oranges')
 test_num_lang_data
print(check_output(["ls", ".. /input"] ).decode("utf8"))
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
<create_dataframe>
num_words_train_h = [None] * len(train)
 for i in range(len(train)) :
num_words_train_h[i] = len(train['hypothesis'][i])
 num_words_train_p = [None] * len(train)
 for i in range(len(train)) :
num_words_train_p[i] = len(train['premise'][i])
 num_words_test_h = [None] * len(test)
 for i in range(len(test)) :
num_words_test_h[i] = len(test['hypothesis'][i])
 num_words_test_p = [None] * len(test)
 for i in range(len(test)) :
num_words_test_p[i] = len(test['premise'][i] )
new_age = pd.DataFrame()
new_age['Age'] = train_df.Age.fillna(train_df.Age.mean())
 new_age<import_modules>
train['num_words_hypothesis'] = num_words_train_h
train['num_words_premise'] = num_words_train_p
test['num_words_hypothesis'] = num_words_test_h
test['num_words_premise'] = num_words_test_p
import seaborn as sns 
<groupby>
labels = train['label'].sort_values().value_counts().reset_index()
labels = pd.DataFrame(labels)
 labels.columns = ['label','count']
labels_ = labels.style.background_gradient(cmap='Blues')
 labels_
train_df.groupby(['Sex'] ).Survived.sum() 
<count_values>
target = train['label']
train = train.drop('label',axis=1)
 train_text = [None] * len(train)
 test_text = [None] * len(test)
 for i in range(len(train)) :
train_text[i] = train['premise'][i] + ' ' + train['hypothesis'][i]
for i in range(len(test)) :
test_text[i] = test['premise'][i] + ' ' + test['hypothesis'][i]
print(train_df[train_df.Sex == 'female'].Survived.sum() /train_df[train_df.Sex == 'female'].Survived.count())
 print(train_df[train_df.Sex == 'male'].Survived.sum() /train_df[train_df.Sex == 'male'].Survived.count() )<feature_engineering>
tokenizer = transformers.AutoTokenizer.from_pretrained(MODEL )
data['Age'] = data.Age.fillna(data.Age.median())
 data['Fare'] = data.Fare.fillna(data.Fare.median())
 data.info()<categorify>
def roberta_encode(texts, maxlen=512):
enc_di = tokenizer.batch_encode_plus(
 texts,
pad_to_max_length=True,
max_length=maxlen 
)
 return np.array(enc_di['input_ids'] )
data = pd.get_dummies(data, columns=['Sex'], drop_first=True)
 data.head()<prepare_x_and_y>
train_input_ids = roberta_encode(train_text,maxlen=100)
 test_input_ids = roberta_encode(test_text,maxlen=100 )
data_train = data.iloc[:891]
data_test = data.iloc[891:]
X = data_train.values
test = data_test.values
y = survived_train.values<import_modules>
train_input_ids,validation_input_ids,train_labels,validation_labels = train_test_split(train_input_ids,target,test_size=0.2 )
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV<train_model>
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_input_ids,train_labels)) 
.repeat() 
.shuffle(2048)
.batch(Batch_size)
.prefetch(AUTO)
)
 validation_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( validation_input_ids, validation_labels)) 
.batch(Batch_size)
.cache() 
.prefetch(AUTO)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_input_ids)
.batch(Batch_size)
 )
clf = tree.DecisionTreeClassifier(max_depth=3)
 clf.fit(X, y )<save_to_csv>
def create_model(bert_model):
input_ids = tf.keras.Input(shape=(100,),dtype='int32')
 output = bert_model(input_ids)[0]
output = output[:,0,:]
output = tf.keras.layers.Dense(3,activation='softmax' )(output)
 model = tf.keras.models.Model(inputs = input_ids,outputs = output)
 model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-5), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 return model
Y_pred = clf.predict(test)
 test_df['Survived'] = Y_pred
test_df[['PassengerId', 'Survived']].to_csv('dec_tree.csv', index=False )<set_options>
with strategy.scope() :
bert_model =(
 transformers.TFAutoModel 
.from_pretrained(MODEL)
)
 model = create_model(bert_model )
sns.set_palette('deep' )<load_from_csv>
history = model.fit(train_dataset,
validation_data = validation_dataset,
epochs = epochs,
batch_size = Batch_size,
steps_per_epoch = len(train_input_ids)//Batch_size
)
<concatenate><EOS> 
pred = model.predict(test_dataset,verbose=1)
 print(len(pred))
pred = pred.argmax(axis=1)
 submission.prediction = pred
submission.to_csv('submission.csv',index=False)
 submission.head()