Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<load_from_csv>
%matplotlib notebook
tf.random.set_seed(42)
 np.random.seed(43)
y_true = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv', index_col = 'sig_id' ).values
oof_dict = {'NN-Transfer-Learning': '.. /input/nn-transferlearning-7seeds4248-10folds/oof_nn_transfer_all.npy',
'TabNet': '.. /input/tabnetfe7seeds424810folds/oof_TabNet_all.npy',
'ResNet': '.. /input/resent-1841/resnet_oof.npy',
'Multi-Heads': '.. /input/jp-multiheads/multi_heads_oof.npy'
}
sub = {}
sub['NN-Transfer-Learning']= sub_nn_transfer
sub['TabNet'] = submission_TabNet
sub['ResNet'] = sub_rs
sub['Multi-Heads'] = sub_mh
oof = []
sub_all = []
oof_tmp = np.zeros([y_true.shape[0], y_true.shape[1]])
 for i in oof_dict:
sub_tmp = sub[i].iloc[:,1:].values
if sub_tmp.sum() > 0.0000000001:
oof.append(np.load(oof_dict[i]))
sub_all.append(sub_tmp)
 oof = np.asarray(oof)
 sub_all = np.asarray(sub_all)
<compute_test_metric>
!nvidia-smi
%%time
log_loss_scores = {}
for n, key in enumerate(oof_dict.keys()):
score_oof = log_loss_numpy(oof[n])
 log_loss_scores[key] = score_oof
print(f'{key} CV:\t', score_oof)
 print('-' * 50 )<define_variables>
df_train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 df_val = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')
 df_test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv' )
test_weights = np.array([1 / oof.shape[0]] * oof.shape[0] )<train_on_grid>
y = tf.keras.utils.to_categorical(df_train['label'].values)
 y_val = tf.keras.utils.to_categorical(df_val['label'].values)
 y_not_enc = df_train['label'].values
df_train = df_train.drop(['label'], axis=1)
 df_val = df_val.drop(['label'], axis=1)
 X = df_train.values.reshape(-1, 28, 28, 1)
 X_val = df_val.values.reshape(-1, 28, 28, 1)
tol = 1e-10
init_guess = [1 / oof.shape[0]] * oof.shape[0]
bnds = [(0, 1)for _ in range(oof.shape[0])]
cons = {'type': 'eq',
'fun': lambda x: np.sum(x)- 1,
'jac': lambda x: [1] * len(x)}
print('Inital Blend OOF:', func_numpy_metric(init_guess))
start_time = time()
res_scipy = minimize(fun = func_numpy_metric,
x0 = init_guess,
method = 'SLSQP',
jac = grad_func_jit,
bounds = bnds,
constraints = cons,
tol = tol)
 print(f'[{str(datetime.timedelta(seconds = time() - start_time)) [2:7]}] Optimised Blend OOF:', res_scipy.fun)
 print('Optimised Weights:', res_scipy.x )<normalization>
datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.,
rotation_range=15,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=False)
 datagen.fit(X)
 valid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.)
print('Check the sum of all weights:', np.sum(res_scipy.x))
if np.sum(res_scipy.x)- 1 <= tol:
print('Great! The sum of all weights equals to 1!')
 else:
print('Manual adjustion is needed to modify the weights.' )<load_from_csv>
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters = 16, kernel_size = 3, strides = 1, activation = 'selu', kernel_initializer='glorot_normal', input_shape =(28, 28, 1)))
 model.add(tf.keras.layers.Conv2D(filters = 16, kernel_size = 3, strides = 1, activation = 'selu', kernel_initializer='glorot_normal', padding='same', input_shape =(28, 28, 1)))
 model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.MaxPooling2D(pool_size = 2, strides = 2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Conv2D(filters = 32, kernel_size = 3, strides = 1, activation = 'selu', kernel_initializer='glorot_normal', padding='same'))
model.add(tf.keras.layers.Conv2D(filters = 32, kernel_size = 3, strides = 1, activation = 'selu', kernel_initializer='glorot_normal', padding='same'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.MaxPooling2D(pool_size = 2, strides = 2))
model.add(tf.keras.layers.AlphaDropout(0.25))
model.add(tf.keras.layers.Conv2D(filters = 64, kernel_size = 3, strides = 1, activation = 'selu', kernel_initializer='glorot_normal', padding='same'))
model.add(tf.keras.layers.Conv2D(filters = 64, kernel_size = 3, strides = 1, activation = 'selu', kernel_initializer='glorot_normal', padding='same'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.MaxPooling2D(pool_size = 2, strides = 2))
model.add(tf.keras.layers.AlphaDropout(0.25))
model.add(tf.keras.layers.Flatten())
 model.add(tf.keras.layers.Dense(units = 1024, activation = 'relu'))
model.add(tf.keras.layers.Dense(units = 10, activation = 'softmax'))
model.compile(optimizer = tf.keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0),
loss = 'categorical_crossentropy', metrics = ['accuracy'])
 model.summary()
sub = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 tmp_sub = np.zeros([sub.shape[0], sub.shape[1]-1])
 for i in range(len(sub_all)) :
tmp_sub += res_scipy.x[i]*sub_all[i]
sub.iloc[:,1:] = np.clip(tmp_sub, 0.000005,0.999995)
 sub.iloc[test_features['cp_type'] == 'ctl_vehicle',1:] = 0
sub.to_csv('submission.csv', index=False )<load_from_csv>
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=0.0001)
 history = model.fit_generator(datagen.flow(X, y, batch_size=256),
steps_per_epoch=256, epochs=50,
validation_data=valid_datagen.flow(X_val, y_val),
validation_steps=50,
callbacks=[reduce_lr] ).history
!rm submission_TabNet.csv<set_options>
sub = pd.DataFrame(df_test['id'], columns=['id'])
 df_test = df_test.drop(columns=['id'])
 X_val = df_test.values.reshape(-1, 28, 28, 1)/ 255
sub['label'] = np.argmax(model.predict(tf.cast(X_val, tf.float32)) , axis=1 )
<install_modules><EOS> 
sub.to_csv('/kaggle/working/submission.csv', index=False )