Kaggle competition: Kannada MNIST
EMBEDDING_FILES = [
'.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec',
'.. /input/glove840b300dtxt/glove.840B.300d.txt'
]<define_variables>
train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv" )
NUM_MODELS = 2
BATCH_SIZE = 512
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = 4 * LSTM_UNITS
EPOCHS = 3
MAX_LEN = 220<load_from_csv>
X = train.drop(['label'], axis = 1)
 X_valid = test.drop(['id'], axis = 1 )
train = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv',low_memory=True)
 test = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv',low_memory=True )<categorify>
X_train, X_dev, Y_train, Y_dev = train_test_split(X, Y, test_size = 0.2 )
def preprocess(data):

 punct = "/-'?!.,
def clean_special_chars(text, punct):
for p in punct:
text = text.replace(p, ' ')
 return text
data = data.astype(str ).apply(lambda x: clean_special_chars(x, punct))
return data 
<prepare_x_and_y>
f = 2**2
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(f*16,kernel_size=(3,3),padding="same",activation='relu',
kernel_initializer='he_uniform',
input_shape=(28,28,1)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(f*16,(3,3), padding='same',
activation ='relu',
kernel_regularizer=regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(f*16,(5,5), padding='same', activation ='relu',
kernel_regularizer=regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(f*32,(3,3), padding='same', activation ='relu',
kernel_regularizer=regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(f*32,(3,3), padding='same', activation ='relu',
kernel_regularizer=regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(f*32,(5,5), padding='same', activation ='relu',
kernel_regularizer=regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.15),
tf.keras.layers.Conv2D(f*64,kernel_size=(3,3),padding="same",activation='relu',
kernel_regularizer=regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(f*64,kernel_size=(3,3),padding="same",activation='relu',
kernel_regularizer=regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.15),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 print(model.summary() )
x_train = preprocess(train['comment_text'])
 y_train = np.where(train['target'] >= 0.5, 1, 0 )<prepare_x_and_y>
initial_learningrate=0.001
model.compile(optimizer=
RMSprop(lr=initial_learningrate),
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'] )
y_aux_train = train[['target', 'severe_toxicity', 'obscene', 'identity_attack', 'insult', 'threat']]
x_test = preprocess(test['comment_text'] )<train_model>
lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='acc',
patience=300,
verbose=1,
factor=0.5,
min_lr=0.00001 )
tokenizer = text.Tokenizer()
tokenizer.fit_on_texts(list(x_train)+ list(x_test))<string_transform>
def lr_decay(epoch, initial_learningrate = 0.001):
return initial_learningrate * 0.99 ** epoch
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test )<concatenate>
train_datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.1,
zoom_range=0.25,
horizontal_flip=False)
 valid_datagen = ImageDataGenerator(
 horizontal_flip=False,
rotation_range=15,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.15,
zoom_range=0.25, 
)
 callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=5)
 batchsize = 512*4
epoch = 45
history = model.fit_generator(train_datagen.flow(X, Y, batch_size = batchsize),
steps_per_epoch = 100,
epochs = epoch,
callbacks=[callback,
LearningRateScheduler(lr_decay),
lr
],
validation_data=valid_datagen.flow(X_dev, Y_dev),
validation_steps=50,
)
x_train = sequence.pad_sequences(x_train, maxlen=MAX_LEN)
 x_test = sequence.pad_sequences(x_test, maxlen=MAX_LEN )<set_options>
test_loss, test_acc = model.evaluate(X_dev, Y_dev,verbose=2)
 print('
Test accuracy: ', test_acc)
 print('
Test loss: ', test_loss )
del train
gc.collect()<compute_test_metric>
yhat = model.predict_classes(X_valid)
 submission['label']=pd.Series(yhat)
 submission.to_csv('submission.csv',index=False )
def get_coefs(word, *arr):
return word, np.asarray(arr, dtype='float32' )<load_pretrained>
model.save('my_model.h5' )