Kaggle competition: Digit Recognizer
random.seed(42)
 np.random.seed(42)
 tf.random.set_seed(42 )<load_pretrained>
train = pd.read_csv('.. /input/train.csv')
 train.head()
tfa.register_all()<define_variables>
test = pd.read_csv(".. /input/test.csv")
 test.head()
xlmr_learning_rate = 1e-5
dataset_dir = '/kaggle/input/jigsaw-multilingual-toxic-comment-classification'
final_classifier_name = '/kaggle/working/xlmr_for_toxicity.h5'<load_pretrained>
train['label'].value_counts().sort_index()
xlmroberta_tokenizer = AutoTokenizer.from_pretrained(model_name)
 xlmroberta_config = XLMRobertaConfig.from_pretrained(model_name)
 print(xlmroberta_config )<train_model>
y = train['label']
X = train.drop(['label'], axis = 1 )
sentence_embedding_size = xlmroberta_config.hidden_size
print('Sentence embedding size is {0}'.format(sentence_embedding_size))
assert max_seq_len <= xlmroberta_config.max_position_embeddings<load_from_csv>
X = X / 255
test = test / 255
corpus_for_training = load_train_set(
 os.path.join(dataset_dir, "jigsaw-toxic-comment-train.csv"),
text_field="comment_text", lang_field="lang",
sentiment_fields=["toxic", "severe_toxic", "obscene", "threat", "insult",
"identity_hate"] 
)
 assert 'en' in corpus_for_training<load_from_csv>
y = np.array(y )
multilingual_corpus = load_train_set(
 os.path.join(dataset_dir, "validation.csv"),
text_field="comment_text", lang_field="lang", sentiment_fields=["toxic", ] 
)
 assert 'en' not in multilingual_corpus
max_size = 0
print('Multilingual data:')
 for language in sorted(list(multilingual_corpus.keys())) :
print(' {0}\t\t{1} samples'.format(language, len(multilingual_corpus[language])))
 assert set(map(lambda cur: cur[1], multilingual_corpus[language])) == {0, 1}
if len(multilingual_corpus[language])> max_size:
max_size = len(multilingual_corpus[language] )<feature_engineering>
from sklearn.model_selection import StratifiedKFold
nonenglish_languages = sorted(list(multilingual_corpus.keys()))
corpus_for_validation = dict()
for lang in nonenglish_languages:
random.shuffle(multilingual_corpus[lang])
 n = len(multilingual_corpus[lang])// 2
corpus_for_validation[lang] = multilingual_corpus[lang][0:n]
corpus_for_training[lang] = multilingual_corpus[lang][n:]
del multilingual_corpus[lang]<load_from_csv>
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32,(3,3), padding = "same", activation = "relu", input_shape = X.shape[1:]))
model.add(tf.keras.layers.MaxPool2D(2,2))
model.add(tf.keras.layers.Conv2D(64,(3,3), padding = "same", activation = "relu"))
model.add(tf.keras.layers.MaxPool2D(2,2))
model.add(tf.keras.layers.Conv2D(128,(3,3), padding = "same", activation = "relu"))
model.add(tf.keras.layers.MaxPool2D(2,2))
model.add(tf.keras.layers.Conv2D(256,(3,3), padding = "same", activation = "relu"))
model.add(tf.keras.layers.MaxPool2D(2,2))
model.add(tf.keras.layers.Flatten())
 model.add(tf.keras.layers.Dense(10, activation='softmax'))
texts_for_submission = load_test_set(
 os.path.join(dataset_dir, "test.csv"),
text_field="content", lang_field="lang", id_field="id" 
)
 print('Data for submission:')
 for language in sorted(list(texts_for_submission.keys())) :
print(' {0}\t\t{1} samples'.format(language, len(texts_for_submission[language])) )<create_dataframe>
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )
dataset_for_training, n_batches_per_data = build_dataset(
 texts=corpus_for_training, dataset_size=150000,
tokenizer=xlmroberta_tokenizer, maxlen=max_seq_len,
batch_size=batch_size_for_xlmr, shuffle=True
)<create_dataframe>
k_fold = StratifiedKFold(n_splits=12, random_state=12, shuffle=True)
 for k_train_index, k_test_index in k_fold.split(X, y):
model.fit(X[k_train_index,:], y[k_train_index], epochs=5 )
dataset_for_validation, n_batches_per_epoch = build_dataset(
 texts=corpus_for_validation, dataset_size=6000,
tokenizer=xlmroberta_tokenizer, maxlen=max_seq_len,
batch_size=batch_size_for_xlmr, shuffle=False
)<set_options>
val_loss, val_acc = model.evaluate(X, y)
 val_acc
del corpus_for_training
gc.collect()<train_model>
model.save("dr_cnn_model.h5" )
preparing_duration = int(round(time.time() - experiment_start_time))
print("Duration of data loading and preparing to the Siamese NN training is "
"{0} seconds.".format(preparing_duration))<choose_model_class>
test_pred = model.predict(test )
with strategy.scope() :
xlmr_based_classifier = build_classifier(
 transformer_name=model_name,
hidden_state_size=sentence_embedding_size,
max_len=max_seq_len,
lr=xlmr_learning_rate
)<train_model>
submission = pd.DataFrame()
submission['ImageId'] = range(1,(len(test)+1))
submission['Label'] = np.argmax(test_pred, axis=1 )
train_classifier(nn=xlmr_based_classifier, trainset=dataset_for_training,
steps_per_trainset=n_batches_per_data,
steps_per_epoch=min(5 * n_batches_per_epoch, n_batches_per_data),
validset=dataset_for_validation,
max_duration=int(round(2.0 * 3600.0 - preparing_duration)) ,
classifier_file_name=final_classifier_name )<predict_on_test>
submission.to_csv("submission.csv", index=False )
<set_options><EOS> 
submission.to_csv("submission.csv", index=False )