Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<count_values>
!pip install transformers==3.0.2
test.dtypes.value_counts()<drop_column>
import tensorflow as tf
import transformers
from sklearn.model_selection import KFold
data = pd.concat(( train, test))
data.drop('Survived', axis = 1, inplace = True)
 data.drop('PassengerId', axis = 1, inplace = True )<count_values>
transformers.__version__
data.Sex.value_counts(normalize = True )<count_values>
from transformers import AutoTokenizer, TFAutoModel
embarked_counts = data.Embarked.value_counts(normalize = True)
 embarked_counts<sort_values>
train_df = pd.read_csv("/kaggle/input/contradictory-my-dear-watson/train.csv")
 test_df = pd.read_csv("/kaggle/input/contradictory-my-dear-watson/test.csv" )
null_values = data.columns[data.isnull().any() ]
null_features = data[null_values].isnull().sum().sort_values(ascending = False)
 missing_data = pd.DataFrame({'No of Nulls' :null_features})
 missing_data<count_missing_values>
epochs = 3
maxlen = 50
model_name = "jplu/tf-xlm-roberta-large"
batch_size = 16 * strategy.num_replicas_in_sync
test.isnull().sum()<count_missing_values>
tokenizer = AutoTokenizer.from_pretrained(model_name )
test.isnull().sum()<count_missing_values>
%%time
train_encode = tokenizer(list(train_df.premise.values), list(train_df.hypothesis.values),
max_length=maxlen, return_tensors="np", padding=True,
return_token_type_ids=True, return_attention_mask=True )
train.isnull().sum()<count_missing_values>
def get_model(maxlen=50):
base_model = TFAutoModel.from_pretrained(model_name)
 input_ids = tf.keras.layers.Input(shape =(maxlen,), dtype=tf.int32, name="input_ids")
 input_type = tf.keras.layers.Input(shape =(maxlen,), dtype=tf.int32, name="token_type_ids")
 input_mask = tf.keras.layers.Input(shape =(maxlen,), dtype=tf.int32, name="attention_mask")
 embedding = base_model([input_ids, input_mask, input_type])[0]
print(embedding.shape)
 output = tf.keras.layers.Dense(3, activation="softmax" )(embedding[:, 0, :])
 model = tf.keras.models.Model(inputs=[input_ids, input_mask, input_type], outputs = output)
 model.compile(tf.keras.optimizers.Adam(1e-5), "sparse_categorical_crossentropy", ["accuracy"])
 return model
train.isnull().sum()<data_type_conversions>
%%time
ps = cls_model([train_encode['input_ids'][:10], train_encode['attention_mask'][:10], train_encode['token_type_ids'][:10]] )
data["Embarked"] = data["Embarked"].fillna('S' )<filter>
fold = KFold(n_splits=3, shuffle=True, random_state=108 )
data[data['Fare'].isnull() ]<data_type_conversions>
%%time
hists = []
models = []
for i,(train_idx, val_idx)in enumerate(fold.split(np.arange(train_df.label.shape[0]))):
print(f"----FOLD: {i+1}----
",train_idx, val_idx)
 x_train = [train_encode['input_ids'][train_idx],
train_encode['attention_mask'][train_idx],
train_encode['token_type_ids'][train_idx]]
y_train = train_df.label.values[train_idx]
x_val = [train_encode['input_ids'][val_idx],
train_encode['attention_mask'][val_idx],
train_encode['token_type_ids'][val_idx]]
y_val = train_df.label.values[val_idx]
hist=cls_model.fit(x_train, y_train,
epochs=epochs,
batch_size = batch_size,
validation_data =(x_val, y_val), 
)
 hists.append(hist)
 gc.collect()
def fill_missing_fare(df):
median_fare=df[(df['Pclass'] == 3)&(df['Embarked'] == 'S')]['Fare'].median()
df["Fare"] = df["Fare"].fillna(median_fare)
 return df
data=fill_missing_fare(data )<define_search_space>
gc.collect()
sizeof_null = data["Age"].isnull().sum()
rand_age = np.random.randint(0, 80, size = sizeof_null )<data_type_conversions>
%%time
test_encode = tokenizer(list(test_df.premise.values), list(test_df.hypothesis.values),
max_length=maxlen, return_tensors="tf", padding=True,
return_token_type_ids=True, return_attention_mask=True )
age_slice = data["Age"].copy()
age_slice[np.isnan(age_slice)] = rand_age
data["Age"] = age_slice
data["Age"] = data["Age"].astype(int )<feature_engineering>
ps = cls_model.predict([test_encode['input_ids'], test_encode['attention_mask'], test_encode['token_type_ids']],
verbose=1, batch_size=batch_size )
data['Age'] = data['Age'].astype(int)
 data.loc[ data['Age'] <= 18, 'Age'] = 0
data.loc[(data['Age'] > 18)&(data['Age'] <= 35), 'Age'] = 1
data.loc[(data['Age'] > 35)&(data['Age'] <= 60), 'Age'] = 2
data.loc[(data['Age'] > 60)&(data['Age'] <= 80), 'Age'] = 3
data['Age'].value_counts()<data_type_conversions>
submission = test_df.id.copy().to_frame()
submission['prediction'] = np.argmax(ps, 1 )
<count_values><EOS> 
submission.to_csv("submission.csv", index = False )