Kaggle competition: Kannada MNIST
oof_multi_heads_all = np.load('.. /input/jp-multiheads/multi_heads_oof.npy' )<save_to_csv>
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense,Flatten,Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.optimizers import RMSprop,Adam, SGD
test = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sig_id = test[test['cp_type']!='ctl_vehicle'].sig_id.reset_index(drop=True)
 predictions_clip['sig_id'] = sig_id
sub_mh = pd.merge(test[['sig_id']], predictions_clip, on='sig_id', how='left')
 sub_mh.fillna(0, inplace=True)
 sub_mh.iloc[test_features['cp_type'] == 'ctl_vehicle',1:] = 0
sub_mh.to_csv('submission_mh.csv', index=False )<create_dataframe>
CV = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')
 train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 x_cv = CV.loc[:,'pixel0':].values
y_cv = CV.loc[:,:'label'].values
x_train = train.loc[:,'pixel0':].values
y_train = train.loc[:,:'label'].values
%who DataFrame
del folds, predictions_clip,targets, test,gen_cols, pca_gen_cols, ohe_cols, cell_cols, pca_cell_cols
gc.collect()
%who DataFrame<compute_train_metric>
print('x_train shape: {} y_train shape: {}'.format(x_train.shape, y_train.shape))
print('x_cv shape: {} y_cv shape: {}'.format(x_cv.shape, y_cv.shape))
def log_loss_numpy(y_pred):
y_true_ravel = np.asarray(y_true ).ravel()
y_pred = np.asarray(y_pred ).ravel()
y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
 loss = np.where(y_true_ravel == 1, - np.log(y_pred), - np.log(1 - y_pred))
return loss.mean()
def func_numpy_metric(weights):
oof_blend = np.tensordot(weights, oof, axes =(( 0),(0)))
 return log_loss_numpy(oof_blend)
 def grad_func(weights):
oof_clip = np.clip(oof, 1e-15, 1 - 1e-15)
 gradients = np.zeros(oof.shape[0])
 for i in range(oof.shape[0]):
a, b, c = y_true, oof_clip[i], np.zeros(( oof.shape[1], oof.shape[2]))
for j in range(oof.shape[0]):
if j != i:
c += weights[j] * oof_clip[j]
gradients[i] = -np.mean(( -a*b+(b**2)*weights[i]+b*c)/(( b**2)*(weights[i]**2)+2*b*c*weights[i]-b*weights[i]+(c**2)-c))
return gradients
@njit
def grad_func_jit(weights):
oof_clip = np.minimum(1 - 1e-15, np.maximum(oof, 1e-15))
gradients = np.zeros(oof.shape[0])
 for i in range(oof.shape[0]):
a, b, c = y_true, oof_clip[i], np.zeros(( oof.shape[1], oof.shape[2]))
for j in range(oof.shape[0]):
if j != i:
c += weights[j] * oof_clip[j]
gradients[i] = -np.mean(( -a*b+(b**2)*weights[i]+b*c)/(( b**2)*(weights[i]**2)+2*b*c*weights[i]-b*weights[i]+(c**2)-c))
return gradients<load_from_csv>
y_train = to_categorical(y_train)
 y_cv = to_categorical(y_cv )
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>
num_classes = y_train.shape[1]
%%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>
img_rows = 28
img_cols = 28
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
 x_cv = x_cv.reshape(x_cv.shape[0], img_rows, img_cols, 1)
 input_shape =(img_rows, img_cols, 1)
 x_train = x_train.astype('float32')
 x_cv = x_cv.astype('float32')
 x_train = x_train/255
x_cv = x_cv/255
print('x_train shape: {} y_train shape: {}'.format(x_train.shape, y_train.shape))
print('x_cv shape: {} y_cv shape: {}'.format(x_cv.shape, y_cv.shape))
test_weights = np.array([1 / oof.shape[0]] * oof.shape[0] )<train_on_grid>
model = Sequential()
model.add(Conv2D(32, kernel_size=(5,5), activation= 'relu', padding = 'Same',input_shape=input_shape))
model.add(Conv2D(64,(3,3), activation= 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Conv2D(32,(3,3), activation= 'relu', padding = 'Same'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation= 'relu'))
model.add(Dropout(0.50))
model.add(Dense(num_classes, activation= 'softmax'))
model.compile(optimizer=SGD(0.01), loss='categorical_crossentropy', metrics=['accuracy'])
 print(model.summary() )
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>
batch_size = 32
epochs = 20
history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=(x_cv, y_cv))
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>
test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 test_id = test.loc[:,:'id'].values
x_test = test.loc[:,'pixel0':].values
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
 x_test = x_test.astype('float32')
 x_test = x_test/255
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 +=(1/len(sub_all)) *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>
pred = model.predict(x_test )
!rm submission_TabNet.csv<set_options>
label = np.argmax(pred, axis=1 )
!pip install --no-index --find-links /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.0-py3-none-any.whl pytorch-tabnet
!pip install /kaggle/input/iterative-stratification/iterative-stratification-master/
warnings.filterwarnings("ignore")
 os.environ["CUDA_LAUNCH_BLOCKING"] = '1'
plt.style.use("fivethirtyeight")
 c_ = Fore.CYAN
m_ = Fore.MAGENTA
r_ = Fore.RED
b_ = Fore.BLUE
y_ = Fore.YELLOW
g_ = Fore.GREEN 
<set_options>
test_csv = pd.DataFrame(test['id'].values,columns=['id'] )
seed = 42
def set_seed(seed):
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
set_seed(seed )<load_from_csv>
test_csv['label'] = label
<prepare_x_and_y><EOS> 
test_csv.to_csv('submission.csv', index=False )