Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<find_best_params>
print('Working on "%s"' % Path('.' ).absolute() )
if not TRAINING:
models = []
for i in [0, 1, 2, 3, 4]:
torch.cuda.empty_cache()
device = torch.device("cuda:0")
 model = Model(train_features, targets)
 model.to(device)
 model.eval()
ckp_path = f'/kaggle/input/skeleton-with-pytorch/JSModel_{i}.pth'
model.load_state_dict(torch.load(ckp_path))
models.append(model )<find_best_params>
class NumpyImageList(ImageList):
def open(self, fn):
img = fn.reshape(28,28,1)
 return Image(pil2tensor(img, dtype=np.float32))
@classmethod
def from_csv(cls, path:PathOrStr, csv:str, **kwargs)->'ItemList':
df = pd.read_csv(Path(path)/csv, header='infer')
 res = super().from_df(df, path=path, cols=0, **kwargs)
 if 'label' in df.columns:
df = df.drop('label', axis=1)
 df = np.array(df)/255.
 res.items =(df-df.mean())/df.std()
return res
if TRAINING:
models = []
for i in [1, 3]:
torch.cuda.empty_cache()
device = torch.device("cuda:0")
 model = Model(train_features, targets)
 model.to(device)
 model.eval()
ckp_path = f'./JSModel_{i}.pth'
model.load_state_dict(torch.load(ckp_path))
models.append(model )<categorify>
test = NumpyImageList.from_csv('.. /input/', 'test.csv')
 test
models = [models[1], models[3], models[2]]
batch_size = 4096
label_smoothing = 1e-2
loss_fn = SmoothBCEwLogits(smoothing=label_smoothing)
 test_pred = np.zeros(( len(test), len(targets)))
 test_set = MyDataset(test, train_features, targets)
 test_loader = DataLoader(test_set, batch_size=4096, shuffle=False, num_workers=4)
 for id_, model in enumerate(models):
test_pred_, _, __ = inference_fn(model, test_loader, device)
 test_pred += test_pred_ / len(models)
 test_pred_ = np.median(test_pred_, axis=1)
 test_pred_ = np.where(test_pred_ >= 0.50, 1, 0 ).astype(int)
 auc_score = roc_auc_score(test['action'].values.reshape(-1, 1), test_pred_)
 u_score = utility_score(date=test.date.values.reshape(-1),
weight=test.weight.values.reshape(-1),
resp=test.resp.values.reshape(-1),
action=test_pred_.reshape(-1))
print("model", id_, "auc:", auc_score, "u_score:", u_score)
 test_preds = np.median(test_pred, axis=1)
 test_preds = np.where(test_preds >= 0.50, 1, 0 ).astype(int)
 auc_score = roc_auc_score(test['action'].values.reshape(-1, 1), test_preds)
 u_score = utility_score(date=test.date.values.reshape(-1),
weight=test.weight.values.reshape(-1),
resp=test.resp.values.reshape(-1),
action=test_preds.reshape(-1))
print(auc_score, u_score )<statistical_test>
tfms = get_transforms(do_flip=False)
 data =(NumpyImageList.from_csv('.. /input/', 'train.csv')
.split_by_rand_pct (.1)
.label_from_df(cols='label')
.add_test(test, label=0)
.transform(tfms)
.databunch(bs=128, num_workers=0)
.normalize(imagenet_stats))
data
env = janestreet.make_env()
env_iter = env.iter_test()
th = 0.5
for(test_df, pred_df)in tqdm(env_iter):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt.sum()):
x_tt = np.nan_to_num(x_tt)+ np.isnan(x_tt)* f_mean.values.reshape(1, -1)
 feature_inp = pd.DataFrame(x_tt)
 feature_inp.columns = features
feature_inp, _ = add_features(feature_inp,features)
 feature_inp = torch.tensor(feature_inp.values, dtype=torch.float ).to(device)
 pred = np.zeros(( 1, len(targets)))
 for model in models:
pred += model(feature_inp ).detach().cpu().numpy()
pred /= len(models)
 pred = pred.mean(axis=1 ).item()
pred_df.action = int(pred >= 0)
 else:
pred_df.action = 0
env.predict(pred_df )<import_modules>
data.show_batch(rows=5, figsize=(10,10))
import os
import time
import pickle
import random
import numpy as np
import pandas as pd
from tqdm import tqdm
from sklearn.metrics import log_loss, roc_auc_score
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torch.nn import CrossEntropyLoss, MSELoss
from torch.nn.modules.loss import _WeightedLoss
import torch.nn.functional as F 
<import_modules>
dropout = 0.25
model = nn.Sequential(
 nn.Conv2d(in_channels=3,
out_channels=32,
kernel_size=5),
nn.ReLU() ,
nn.BatchNorm2d(32),
nn.MaxPool2d(kernel_size=2,
stride=2),
nn.Dropout(dropout),
nn.Conv2d(in_channels=32,
out_channels=64,
kernel_size=3),
nn.ReLU() ,
nn.BatchNorm2d(64),
nn.MaxPool2d(kernel_size=2,
stride=2),
nn.Dropout(dropout),
nn.Conv2d(in_channels=64,
out_channels=64,
kernel_size=3),
nn.ReLU() ,
Flatten() ,
nn.Linear(64*3*3, 256),
nn.ReLU() ,
nn.Dropout(dropout*1.5),
nn.BatchNorm1d(256),
nn.Linear(256, 10), 
)
 if torch.cuda.is_available() :
model = model.cuda()
learn = Learner(data, model, metrics=accuracy, model_dir='/kaggle/working/models')
 learn.summary()
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout, Concatenate, Lambda, GaussianNoise, Activation
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout, Concatenate, Lambda, GaussianNoise, Activation
import tensorflow as tf
import numpy as np
import pandas as pd
import seaborn as sns
from tqdm import tqdm
import tensorflow_addons as tfa
from random import choices
from numba import njit
import matplotlib.pyplot as plt
import os,gc
import random
from random import choices
from sklearn import datasets
from tensorflow.python.framework import ops<prepare_x_and_y>
learn.fit_one_cycle(20, max_lr=slice(1e-1))
SEED = 1111
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 train = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv')
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train['weight'] != 0]
features_mean = []
features = [c for c in train.columns if 'feature' in c]
for i in features:
x = train[i].mean()
features_mean.append(x)
 train[i] = train[i].fillna(x)
 train['action'] =(( train['resp'].values)> 0 ).astype(int)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
X_train = train.loc[:, train.columns.str.contains('feature')]
y_train = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T<define_search_space>
learn.save('stage1' )
epochs = 200
batch_size = 4096
hidden_units = [160, 160, 160]
dropout_rates = [0.20, 0.20, 0.20, 0.20]
label_smoothing = 1e-2
learning_rate = 1e-3<choose_model_class>
learn.fit_one_cycle(5, max_lr=slice(1e-2))
def create_mlp(
 num_columns, num_labels, hidden_units, dropout_rates, label_smoothing, learning_rate 
):
inp = tf.keras.layers.Input(shape=(num_columns,))
x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Dropout(dropout_rates[0] )(x)
 for i in range(len(hidden_units)) :
x = tf.keras.layers.Dense(hidden_units[i] )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Activation(tf.keras.activations.swish )(x)
 x = tf.keras.layers.Dropout(dropout_rates[i + 1] )(x)
 x = tf.keras.layers.Dense(num_labels )(x)
 out = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(
 optimizer = tfa.optimizers.RectifiedAdam(learning_rate=learning_rate),
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics = tf.keras.metrics.AUC(name='AUC')
)
 return model<train_model>
learn.save('stage2' )
tf.keras.backend.clear_session()
tf.random.set_seed(SEED)
 clf = create_mlp(
 len(features), 5, hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 clf.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=2 )<feature_engineering>
predictions, *_ = learn.get_preds(DatasetType.Test)
 labels = np.argmax(predictions, 1)
 submission_df = pd.DataFrame({'ImageId': list(range(1,len(labels)+1)) , 'Label': labels})
 submission_df.to_csv(f'submission.csv', index=False )