Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<split>
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.keras.backend.set_session(tf.Session(config=config))
train_X, val_X, test_X, train_y, val_y, word_index, train_X_len, val_X_len, test_X_len = load_and_prec()
embedding_matrix_1 = load_glove(word_index)
 embedding_matrix_3 = load_para(word_index )<define_variables>
from keras.layers import LeakyReLU
from keras.layers.normalization import BatchNormalization
outputs = []<predict_on_test>
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 sample_sub = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 dig = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv' )
pred_val_y, pred_test_y, best_score = train_pred(model_lstm_gru_atten(embedding_matrix), epochs = 6)
 outputs.append([pred_val_y, pred_test_y, best_score, 'LSTM'] )<sort_values>
x_train = train.values[:,1:].reshape(train.shape[0], 28, 28, 1)
 x_train = x_train.astype('float32')
 x_train = x_train / 255.0
y_train = train.values[:,0]
x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size = 0.1, random_state=2019)
 x_test = test.values[:,1:].reshape(test.shape[0], 28, 28, 1)
 x_test = x_test.astype('float32')
 x_test = x_test / 255.0
outputs.sort(key=lambda x: x[2])
 weights = [i for i in range(1, len(outputs)+ 1)]
weights = [float(i)/ sum(weights)for i in weights]
print(weights )<compute_test_metric>
lb = preprocessing.LabelBinarizer()
y_train = lb.fit_transform(y_train)
 y_valid = lb.fit_transform(y_valid )
pred_val_y = np.mean([outputs[i][0] for i in range(len(outputs)) ], axis = 0)
 thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 res = metrics.f1_score(val_y,(pred_val_y > thresh ).astype(int))
thresholds.append([thresh, res])
 print("F1 score at threshold {0} is {1}".format(thresh, res))
thresholds.sort(key=lambda x: x[1], reverse=True)
 best_thresh = thresholds[0][0]
print("Best threshold: ", best_thresh )<save_to_csv>
modelo = Sequential()
modelo.add(Conv2D(64,(3,3), padding='same', input_shape=(28, 28, 1)))
 modelo.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(MaxPooling2D(2, 2))
modelo.add(Dropout(0.2))
modelo.add(Conv2D(128,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(Conv2D(128,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(MaxPooling2D(2,2))
modelo.add(Dropout(0.2))
modelo.add(Conv2D(256,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(Conv2D(256,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(MaxPooling2D(2,2))
modelo.add(Dropout(0.2))
modelo.add(Flatten())
 modelo.add(Dense(256))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(BatchNormalization())
 modelo.add(Dense(10, activation='softmax'))
pred_test_y = np.mean([outputs[i][1] for i in range(len(outputs)) ], axis = 0)
 pred_test_y =(pred_test_y > best_thresh ).astype(int)
 test_df = pd.read_csv(".. /input/test.csv", usecols=["qid"])
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test_y
out_df.to_csv("submission.csv", index=False )<define_variables>
epochs=100
batch_size=200
history = modelo.fit(x_train, y_train,
epochs=epochs,
batch_size= batch_size,
callbacks=[LearningRateScheduler(lr_decay)],
validation_data=(x_valid, y_valid))
embed_size = 300
max_features = 95000
maxlen = 70<import_modules>
scores = modelo.predict(x_test)
 score_classes = np.argmax(scores, axis = 1)
 x_dig=dig.drop('label',axis=1 ).iloc[:,:].values
print(x_dig.shape)
 x_dig = x_dig.reshape(x_dig.shape[0], 28, 28,1)
 print(x_dig.shape)
 y_dig=dig.label
print(y_dig.shape)
 preds_dig=modelo.predict_classes(x_dig/255)
 metrics.accuracy_score(preds_dig, y_dig )
import os
import time
import numpy as np
import pandas as pd
from tqdm import tqdm
import math
from sklearn.model_selection import train_test_split
from sklearn import metrics
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, CuDNNLSTM, Embedding, Dropout, Activation, CuDNNGRU, Conv1D
from keras.layers import Bidirectional, GlobalMaxPool1D, GlobalMaxPooling1D, GlobalAveragePooling1D
from keras.layers import Input, Embedding, Dense, Conv2D, MaxPool2D, concatenate
from keras.layers import Reshape, Flatten, Concatenate, Dropout, SpatialDropout1D
from keras.optimizers import Adam
from keras.models import Model
from keras import backend as K
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints, optimizers, layers<load_from_csv>
output = pd.DataFrame({'id': sample_sub['id'],
'label': score_classes})
 output.head()
def load_and_prec() :
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape)
 train_df, val_df = train_test_split(train_df, test_size=0.08, random_state=2018)
 train_X = train_df["question_text"].fillna("_
val_X = val_df["question_text"].fillna("_
test_X = test_df["question_text"].fillna("_
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X = tokenizer.texts_to_sequences(train_X)
 val_X = tokenizer.texts_to_sequences(val_X)
 test_X = tokenizer.texts_to_sequences(test_X)
 train_X = pad_sequences(train_X, maxlen=maxlen)
 val_X = pad_sequences(val_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 train_y = train_df['target'].values
val_y = val_df['target'].values
np.random.seed(2018)
 trn_idx = np.random.permutation(len(train_X))
val_idx = np.random.permutation(len(val_X))
train_X = train_X[trn_idx]
val_X = val_X[val_idx]
train_y = train_y[trn_idx]
val_y = val_y[val_idx]
return train_X, val_X, test_X, train_y, val_y, tokenizer.word_index<statistical_test>
output.to_csv('submission.csv', index=False )