Kaggle competition: Digit Recognizer
submission = pd.DataFrame({"id": test.iloc[:,0].values,"target": np.argmax(predictions,axis=1)})
 submission.to_csv("submission.csv", index=False)
 submission.head()<install_modules>
%%script false --no-raise-error
print(device_lib.list_local_devices())
!pip install transformers==3.5.1
!pip install pyspellchecker
!pip install -U joblib textblob
!python -m textblob.download_corpora<import_modules>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
from sklearn.model_selection import train_test_split, StratifiedKFold
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from keras.models import Sequential, load_model
from keras.layers import Conv2D, BatchNormalization, Dropout, Flatten, Dense
from keras.callbacks import LearningRateScheduler
import tensorflow as tf
import pandas as pd
import torchtext
from transformers import BertTokenizer, BertForMaskedLM, BertConfig
import transformers
import torch
from torch.utils.data import Dataset, DataLoader
from torch import optim
from torch import cuda
from sklearn.model_selection import train_test_split
import re
import string
from joblib import Parallel, delayed
from textblob import TextBlob
from textblob.translate import NotTranslated
from time import sleep<load_from_csv>
gpus = tf.config.experimental.list_physical_devices('GPU')
 if gpus:
try:
tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
 for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
 logical_gpus = tf.config.experimental.list_logical_devices('GPU')
 print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
 except RuntimeError as e:
print(e )
train_val_df = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test_df = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )<feature_engineering>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer//test.csv' )
train_val_df = train_val_df.loc[:,["text","target"]]
test_df = test_df.loc[:,["text"]]
test_df["target"] = [0]*len(test_df["text"] )<define_variables>
y = train.pop('label')
 train_y = to_categorical(y)
 train_X = train/255
test_X = test/255
train_X = train_X.to_numpy()
test_X = test_X.to_numpy()
check_df = train_val_df<define_variables>
sub_test_X, sub_train_X, sub_test_y, sub_train_y = train_test_split(train_X, train_y,
train_size=0.2, stratify=train_y )
languages = ["de"]
parallel = Parallel(n_jobs=-1, backend="threading", verbose=5 )<categorify>
img_gen = ImageDataGenerator(rotation_range = 12, width_shift_range=.12, height_shift_range=.12,
zoom_range=.12 )
def translate_text(comment, language):
if hasattr(comment, "decode"):
comment = comment.decode("utf-8")
 text = TextBlob(comment)
 try:
text = text.translate(to=language)
 sleep(2.0)
 text = text.translate(to="en")
 sleep(2.0)
 except NotTranslated:
pass
return str(text )<save_to_csv>
def build_model(save = False):
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation = 'relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation = 'relu', padding = 'same'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation = 'relu', padding = 'same'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 3, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation ='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 return model
def train_model(model, train_X, train_y, save = False):
sub_test_X, sub_train_X, sub_test_y, sub_train_y = train_test_split(train_X, train_y,
train_size=0.2, stratify=train_y)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 start = time.clock()
model.fit_generator(img_gen.flow(sub_train_X, sub_train_y), epochs=10,
steps_per_epoch=sub_train_X.shape[0]//64,
validation_data =(sub_test_X,sub_test_y), callbacks=[annealer])
 print(f'Overall it takes {time.clock() -start} sec')
 if save ==True:
model.save('/kaggle/input/trained-model/model.h5')
 def load_my_model() :
re_model = load_model('/kaggle/input/trained-model/first_model.h5')
 return re_model
comments_list = check_df["text"]
for language in languages:
print('Translate comments using "{0}" language'.format(language))
translated_data = parallel(delayed(translate_text )(comment, language)for comment in comments_list)
 check_df['text'] = translated_data
result_path = os.path.join("train_val_" + language + ".csv")
 check_df.to_csv(result_path, index=False )<load_from_csv>
num_models = 3
models = [0]*num_models
for i in range(num_models):
models[i] = build_model()
train_model(models[i],train_X, train_y)
train_val_de_df = pd.read_csv("./train_val_de.csv")
 train_concat_df = train_val_de_df<concatenate>
prediction = np.zeros(( test_X.shape[0],10))
for i in range(len(models)) :
prediction += models[i].predict(test_X )
print(train_concat_df )<concatenate>
predict = np.argmax(prediction, axis =1)
 predict = np.vstack(( np.arange(predict.shape[0])+1, predict)).T
train_val_df = pd.concat([train_val_df,train_concat_df] )<load_pretrained>
submission = pd.DataFrame(data=predict, columns=['imageid', 'label'] )
tokenizer = BertTokenizer.from_pretrained('bert-base-cased' )<import_modules>
submission.to_csv('submit.csv',index=False )