Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<import_modules>
import numpy as np
import pandas as pd
import pandas as pd<import_modules>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
import pandas as pd<load_from_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
train_data = pd.read_csv("/kaggle/input/jigsaw-multilingual-toxic-comment-classification/jigsaw-toxic-comment-train.csv")
 val_data = pd.read_csv("/kaggle/input/jigsaw-multilingual-toxic-comment-classification/validation.csv")
 test_data = pd.read_csv("/kaggle/input/jigsaw-multilingual-toxic-comment-classification/test.csv")
 unintended_bias_train = pd.read_csv("/kaggle/input/jigsaw-multilingual-toxic-comment-classification/jigsaw-unintended-bias-train.csv")
<data_type_conversions>
print(train.dtypes[:5])
 print(train.dtypes[:5] )
<concatenate>
xtrain = xtrain / 255.0
test = test / 255.0
train = pd.concat([
train_data[['comment_text', 'toxic']],
unintended_bias_train[['comment_text', 'toxic']].query('toxic==1'),
unintended_bias_train[['comment_text', 'toxic']].query('toxic==0' ).sample(n=50000, random_state=0)
 ] )<import_modules>
print(type(ytrain))
nclasses = ytrain.max() - ytrain.min() + 1
print("Shape of ytrain before: ", ytrain.shape)
 ytrain = to_categorical(ytrain, num_classes = nclasses)
 print("Shape of ytrain after: ", ytrain.shape)
 print(type(ytrain))
import numpy as np 
<categorify>
seed = 2
np.random.seed(seed)
 split_pct = 0.1
xtrain, xval, ytrain, yval = train_test_split(xtrain,
ytrain,
test_size=split_pct,
random_state=seed,
shuffle=True,
stratify=ytrain 
)
 print(xtrain.shape, ytrain.shape, xval.shape, yval.shape )
def regular_encode(texts, tokenizer, maxlen=512):
enc_di = tokenizer.batch_encode_plus(
 texts,
return_token_type_ids=False,
pad_to_max_length=True,
max_length=maxlen 
)
 return np.array(enc_di['input_ids'] )<prepare_x_and_y>
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Dropout, Lambda, Flatten, BatchNormalization
from keras.layers import Conv2D, MaxPool2D, AvgPool2D
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
%%time
x_train = regular_encode(list(train.comment_text.values), tokenizer, maxlen=MAX_LEN)
 x_valid = regular_encode(list(val_data.comment_text.values), tokenizer, maxlen=MAX_LEN)
 x_test = regular_encode(list(test_data.content.values), tokenizer, maxlen=MAX_LEN)
 y_valid = val_data.toxic.values<prepare_x_and_y>
model = Sequential()
dim = 28
nclasses = 10
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', activation='relu', input_shape=(dim,dim,1)))
 model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', activation='relu',))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(120, activation='relu'))
model.add(Dense(84, activation='relu'))
model.add(Dense(nclasses, activation='softmax'))
y_train = train.toxic.values 
<define_variables>
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )
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)
 )<load_pretrained>
lr_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
%%time
with strategy.scope() :
transformer_layer = TFAutoModel.from_pretrained(MODEL)
 model = build_model(transformer_layer, maxlen=MAX_LEN)
 model.summary()<train_model>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=30,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(xtrain )
n_steps = x_train.shape[0] // BATCH_SIZE
train_history = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
epochs=EPOCHS
)<train_model>
epochs = 15
batch_size = 64
n_steps = x_valid.shape[0] // BATCH_SIZE
train_history_2 = model.fit(
 valid_dataset.repeat() ,
steps_per_epoch=n_steps,
epochs=EPOCHS
)<predict_on_test>
history = model.fit_generator(datagen.flow(xtrain,ytrain, batch_size=batch_size),
epochs=epochs,
validation_data=(xval,yval),
verbose=1,
steps_per_epoch=xtrain.shape[0] // batch_size,
callbacks=[lr_reduction] )
<load_from_csv><EOS> 
predictions = model.predict_classes(test, verbose=1)
 submissions = pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("mnist2908.csv", index=False, header=True )