Kaggle competition: Kannada MNIST
np.log(0)*0<compute_test_metric>
def get_images_and_labels(csv,label):
fileraw = pd.read_csv(csv)
 labels = fileraw[label].to_numpy()
data = fileraw.drop([label],axis=1 ).to_numpy(dtype=np.float32)
 data = np.true_divide(data,255.).reshape(( fileraw.shape[0],28,28))
data = np.expand_dims(data, axis=1)
 return data, labels
def log_loss_multi(y_true, y_pred):
M = y_true.shape[1]
results = np.zeros(M)
 for i in range(M):
results[i] = log_loss_score(y_true[:,i], y_pred[:,i])
 return results.mean() 
<load_pretrained>
plt.title(f'Training Label: {train_labels[4]}')
 plt.imshow(train_data[4,0],cmap='gray');
def train_fold(fold, model, tr_dataloader, val_dataloader, cfg, optimizer, reducer, loss_fn, loss_fn_aux=None, accumulation=1, with_aux_class=False):
best_score = np.inf
best_preds = None
best_targets = None
for e in range(cfg.EPOCHS):
train_one_epoch(model, tr_dataloader, cfg, optimizer, loss_fn, loss_fn_aux=loss_fn_aux, accumulation=accumulation, with_aux_class=with_aux_class, verbose=cfg.verbose)
 preds, targets, score = evals(model, val_dataloader, cfg, loss_fn, loss_fn_aux=loss_fn_aux, with_aux_class=with_aux_class, verbose=cfg.verbose)
 reducer.step(score)
 if score < best_score:
print("
best_score = score
best_preds = preds
best_targets= targets
torch.save(model.state_dict() , cfg.save_name + f"_{fold}.pth")
 print("
return best_preds, best_targets, score<load_pretrained>
np.random.seed(42)
 ran_10_pct_idx =(np.random.random_sample(train_labels.shape)) <.1
train_90_labels = train_labels[np.invert(ran_10_pct_idx)]
train_90_data = train_data[np.invert(ran_10_pct_idx)]
valid_10_labels = train_labels[ran_10_pct_idx]
valid_10_data = train_data[ran_10_pct_idx]
def inference_fold(folds, model, test_loader,cfg):
preds = []
for fold in range(folds):
name = cfg.save_name + f"_{fold}.pth"
model.load_state_dict(torch.load(name))
p = inference_fn(model, test_loader, cfg, cfg.verbose,cfg.with_aux_class)
 preds.append(p)
 return preds<compute_test_metric>
train_ds = ArrayDataset(train_90_data,train_90_labels)
 valid_ds = ArrayDataset(valid_10_data,valid_10_labels)
 other_ds = ArrayDataset(other_data, other_labels)
 test_ds = ArrayDataset(test_data, test_labels )
def check_targets(targets):
for i in range(targets.shape[1]):
if len(np.unique(targets[:,i])) != 2:
return False
return True
def check_labels(A):
w = np.zeros(A.shape[1])
 for i in range(A.shape[1]):
if len(np.unique(A[:,i])) == 2:
w[i] = 1
return w.reshape(1, -1)
 def auc_multi(y_true, y_pred):
M = y_true.shape[1]
results = np.zeros(M)
 for i in range(M):
try:
results[i] = roc_auc_score(y_true[:,i], y_pred[:,i])
 except:
pass
return results.mean() 
<import_modules>
bs = 128
databunch = DataBunch.create(train_ds, valid_ds, test_ds=test_ds, bs=bs )
from iterstrat.ml_stratifiers import MultilabelStratifiedKFold 
<init_hyperparams>
leak = 0.25
conv_drop = 0.35
lin_drop = 0.08
six_conv_architecture = nn.Sequential(
 conv_layer(1,16,stride=1,ks=3,leaky=leak),
nn.Dropout(conv_drop),
conv_layer(16,32,stride=1,ks=3,leaky=leak),
nn.Dropout(conv_drop),
AdaptiveConcatPool2d(14),
conv_layer(64,64,stride=1,ks=5,leaky=leak),
nn.Dropout(conv_drop),
AdaptiveConcatPool2d(sz=7),
conv_layer(128,128,stride=1,ks=5,leaky=leak),
nn.Dropout(conv_drop),
conv_layer(128,64,stride=1,ks=3,leaky=leak),
nn.Dropout(conv_drop),
conv_layer(64,32,stride=1,ks=3,leaky=leak),
nn.Dropout(conv_drop),
Flatten() ,
nn.Linear(1568, 50),
relu(inplace=True,leaky=leak),
nn.Dropout(lin_drop),
nn.Linear(50,25),
relu(inplace=True,leaky=leak),
nn.Dropout(lin_drop),
nn.Linear(25,10)
 )
class Config(object):
def __init__(self):
self.num_class = targets_tr.shape[1]
self.aux_class = targets2_tr.shape[1]
self.use_apex=False
self.verbose=False
self.batch_size = 128
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.SPLITS = 10
self.num_cats=[3+1,2+1] if remove_vehicle else [2+1,3+1,2+1]
self.cats_emb_size=[1]* cat_tr.shape[1]
self.num_numericals= numerical_tr.shape[1]
self.hidden_size_numericals=2048
self.num_ensembling = 1
self.strategy = "KFOLD"
self.save_name = ".. /input/moannweights/MOAMLP-aux-raw/MOA_mlp-KFOLD10_"
cfg = Config()
cfg.with_aux_class = True if cfg.aux_class is not None else False
print(cfg.num_class, cfg.aux_class,cfg.with_aux_class )<choose_model_class>
learn = Learner(databunch, six_conv_architecture, loss_func = nn.CrossEntropyLoss() , metrics=[accuracy] )
loss_fn = nn.BCEWithLogitsLoss(reduction="none")
 loss_fn_aux = nn.BCEWithLogitsLoss(reduction="none" )<create_dataframe>
learn.fit_one_cycle(15 )
test_dataset = MOADataset(cat_test, numerical_test)
 test_dataloader = DataLoader(test_dataset, batch_size=cfg.batch_size, shuffle=False, num_workers=2)
<choose_model_class>
preds, ids = learn.get_preds(DatasetType.Test)
 y = torch.argmax(preds, dim=1 )
model = MOA_MLP(num_cats=cfg.num_cats , cats_emb_size=cfg.cats_emb_size, num_numericals=cfg.num_numericals, hidden_size_numericals=cfg.hidden_size_numericals,
num_class=cfg.num_class, aux=cfg.aux_class)
 print(model )<init_hyperparams>
submission = pd.DataFrame({ 'id': ids,'label': y } )
<compute_test_metric><EOS> 
submission.to_csv(path_or_buf ="submission4.csv", index=False )