Kaggle competition: Digit Recognizer
if not training or tuning:
model_fn = lambda hp: create_model(hp,159,5)
 hp = pd.read_pickle('/kaggle/input/jscnn/best_hp_cnn_day_86.pkl')
 model = model_fn(hp)
 model.load_weights('/kaggle/input/jscnn/JS_CNN_day_86.hdf5')
 samples_mean = pd.read_csv('/kaggle/input/jscnn/f_mean.csv')
 features_transform = np.load('/kaggle/input/jscnn/features_transform _130.npy')
 env = janestreet.make_env()
iter_test = env.iter_test()
for(test_df, sample_prediction_df)in iter_test:
weight = test_df.weight.iloc[0]
with open("/kaggle/input/jscnn/ordered_columns.txt", 'rb')as f:
ordered_cols = pickle.load(f)
 test_df = test_df[ordered_cols]
X_test = test_df.values[0]
if weight==0:
sample_prediction_df.action = 0
else:
for index, x in np.ndenumerate(X_test):
idx=index[0]
if np.isnan(x):
X_test[idx] = samples_mean.iloc[idx]
X_test=X_test.reshape(( 1,-1))
X_metadata = np.matmul(X_test,features_transform)
 X = np.concatenate(( X_test,X_metadata),axis=1)
 prediction = np.median(model(X, training=False ).numpy() [0])
 sample_prediction_df.action = int(round(prediction))
env.predict(sample_prediction_df)
<set_options>
%matplotlib inline
plt.style.use('fivethirtyeight')
 y_ = Fore.YELLOW
r_ = Fore.RED
g_ = Fore.GREEN
b_ = Fore.BLUE
m_ = Fore.MAGENTA
c_ = Fore.CYAN
sr_ = Style.RESET_ALL
warnings.filterwarnings('ignore')
<load_from_csv>
base = Path('.. /input' )
folder_path = '.. /input/jane-street-market-prediction/'
sample = pd.read_csv(folder_path + 'example_sample_submission.csv')
 test_data = pd.read_csv(folder_path + 'example_test.csv' )<set_options>
data_df = pd.read_csv(base/'train.csv')
 data_df.head()
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42 )<load_from_csv>
trn_df = data_df.drop(val_df.index)
 trn_df.shape
features = [f'feature_{i}' for i in range(130)]
config = {
"epochs":100,
"train_batch_size":1024,
"valid_batch_size":1024,
"test_batch_size":64,
"nfolds":5,
"learning_rate":0.0005,
'encoder_input':len(features),
"input_size1":len(features),
"input_size2":128,
'output_size':5,
}
data_path = '.. /input/jsmp-pytorch-bottelneck-model-train'
train_data_mean = pd.read_csv(f"{data_path}/train_data_mean.csv" ).to_numpy()<normalization>
trn_x, trn_y = trn_df.loc[:, 'pixel0':'pixel783'], trn_df['label']
val_x, val_y = val_df.loc[:, 'pixel0':'pixel783'], val_df['label']
class GaussianNoise(nn.Module):
def __init__(self,device,sigma=0.1, is_relative_detach=True):
super().__init__()
self.sigma = sigma
self.is_relative_detach = is_relative_detach
self.noise = torch.tensor(0,dtype=torch.float ).to(device)
 def forward(self, x):
if self.training and self.sigma != 0:
scale = self.sigma * x.detach() if self.is_relative_detach else self.sigma * x
sampled_noise = self.noise.repeat(*x.size() ).normal_() * scale
x = x + sampled_noise
return x
class Autoencoder(nn.Module):
def __init__(self,input_size,gaussian_noise,noise_level= 0.1):
super(Autoencoder,self ).__init__()
self.noise_level = noise_level
self.gaussian_noise = gaussian_noise
self.layer1 = self.batch_linear(input_size,768,nn.ReLU)
 self.layer2 = self.batch_linear(768,768,nn.ReLU)
 self.layer3 = self.batch_linear(768,128,nn.ReLU)
 self.layer4 = self.batch_linear(128,768,nn.ReLU)
 self.layer5 = self.batch_linear(768,768,nn.ReLU)
 self.layer6 = self.batch_linear(768,input_size)
 def swap_noise(self,x):
batch_size = x.shape[0]
num_columns = x.shape[1]
random_rows = torch.randint(low = 0,high = batch_size,size=(batch_size,))
t = x[random_rows]
random_swap = torch.rand(num_columns)< self.noise_level
x[:,random_swap] = t[:,random_swap]
return x
def batch_linear(self,inp,out,activation=None):
if activation:
return nn.Sequential(nn.BatchNorm1d(inp),nn.Linear(inp,out),activation())
 else:
return nn.Sequential(nn.BatchNorm1d(inp),nn.Linear(inp,out))
def forward(self,x):
x = self.gaussian_noise(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.layer5(x)
 x = self.layer6(x)
 return x
def get_encoder(self,x):
x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 return x<choose_model_class>
def reshape(dt_x, dt_y):
dt_x = np.array(dt_x, dtype = np.uint8 ).reshape(-1,28,28)
 dt_x = np.stack(( dt_x,)*3, axis=-1)
 dt_y = np.array(dt_y)
 return dt_x, dt_y
class Model(nn.Module):
def __init__(self,input_size1,input_size2,output_size):
super(Model,self ).__init__()
self.layer1 = self.batch_linear_drop(input_size1,256,0.3,activation=nn.ELU)
 self.layer2 = self.batch_linear(256,128,activation= nn.ELU)
 self.layer3 = self.batch_linear_drop(input_size1+128,256,0.1,nn.ReLU)
 self.layer4 = self.batch_linear_drop(input_size1+384,256,0.1,nn.ELU)
 self.layer5 = self.batch_linear(256,128,nn.ReLU)
 self.layer6 = self.batch_linear_drop(384,256,0.1,nn.ELU)
 self.layer7 = self.batch_linear(256,128,nn.ReLU)
 self.layer8 = self.batch_linear_drop(512,256,0.1,nn.ELU)
 self.layer9 = self.batch_linear(256,128,nn.ReLU)
 self.layer10 = self.batch_linear_drop(384,256,0.1,nn.ELU)
 self.layer11 = self.batch_linear(256,128,nn.ReLU)
 self.layer12 = self.batch_linear(768,256,nn.SELU)
 self.layer13 = self.batch_linear(256,128,nn.SELU)
 self.layer14 = nn.Sequential(nn.BatchNorm1d(128),nn.Linear(128,output_size))
def batch_linear_drop(self,inp,out,drop,activation=None):
if activation:
return nn.Sequential(nn.BatchNorm1d(inp),nn.Dropout(drop),nn.Linear(inp,out),activation())
 else:
return nn.Sequential(nn.BatchNorm1d(inp),nn.Dropout(drop),nn.Linear(inp,out))
def batch_linear(self,inp,out,activation=None):
if activation:
return nn.Sequential(nn.BatchNorm1d(inp),nn.Linear(inp,out),activation())
 else:
return nn.Sequential(nn.BatchNorm1d(inp),nn.Linear(inp,out))
def forward(self,input1,input2):
x1 = self.layer1(input1)
 x1 = self.layer2(x1)
 x2 = torch.cat([input1,x1],1)
 x2 = self.layer3(x2)
 x3 = torch.cat([input1,x1,x2],1)
 x3 = self.layer4(x3)
 x3 = self.layer5(x3)
 x4 = torch.cat([x2,x3],1)
 x4 = self.layer6(x4)
 x4 = self.layer7(x4)
 x5 = torch.cat([x2,x3,x4],1)
 x5 = self.layer8(x5)
 x5 = self.layer9(x5)
 x6 = torch.cat([x3,x4,x5],1)
 x6 = self.layer10(x6)
 x6 = self.layer11(x6)
 x7 = torch.cat([x1,x2,x3,x5,x6],1)
 x7 = self.layer12(x7)
 x7 = self.layer13(x7)
 x7 = self.layer14(x7)
 return x7 
<categorify>
trn_x, trn_y = reshape(trn_x, trn_y)
 val_x, val_y = reshape(val_x, val_y )
data_path = '.. /input/jsmp-pytorch-bottelneck-model-train'
models = list()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 for i in range(config['nfolds']):
model = Model(config['input_size1'],config['input_size2'],config['output_size'])
 model.load_state_dict(torch.load(f"{data_path}/model{i}.bin",map_location=device))
model.to(device)
 model.eval()
models.append(model)
 gaussian_noise = GaussianNoise(device)
 encoder = Autoencoder(config['input_size1'],gaussian_noise)
 encoder.load_state_dict(torch.load(f'{data_path}/encoder.bin',map_location=device))
encoder.to(device)
 encoder.eval() ;<categorify>
train=Path('.. /working/data/train')
 save(train, trn_x, trn_y)
 valid = Path('.. /working/data/valid')
 save(valid, val_x, val_y )
def inference(test):
all_prediction = np.zeros(( test.shape[0],5))
inputs = torch.tensor(test,dtype=torch.float)
 for model in models:
inputs = inputs.to(device,dtype=torch.float)
 encoder_inp = encoder.get_encoder(inputs)
 outputs = model(inputs,encoder_inp)
 all_prediction += outputs.sigmoid().detach().cpu().numpy()
return all_prediction/len(models )<load_from_csv>
path = Path('.. /working/data/')
 data =(ImageList.from_folder(path)
.split_by_folder(train='train', valid='valid')
.label_from_folder() 
.transform(get_transforms(do_flip=False), size=28)
.databunch(bs=256 ).normalize(imagenet_stats))
test_data = pd.read_csv(folder_path + 'example_test.csv')
 test_data.fillna(0,inplace=True)
 test_data = test_data[features].to_numpy()
predictions = inference(test_data)
 predictions = predictions.mean(axis=1)
 sns.distplot(predictions);<split>
learn = cnn_learner(data, models.resnet34, loss_func=nn.CrossEntropyLoss() , metrics=accuracy )
env = janestreet.make_env()
iter_test = env.iter_test()<predict_on_test>
learn.fit_one_cycle(3, 1e-2 )
%%time
all_predictions = list()
for(test_df, sample_prediction_df)in iter_test:
if test_df['weight'].item() != 0:
test_df.fillna(0,inplace=True)
 predictions = inference(test_df[features].to_numpy())
 prediction = np.mean(predictions)
 all_predictions.append(prediction)
 sample_prediction_df.action = np.where(prediction >= 0.5, 1, 0 ).astype(int)
 else:
sample_prediction_df.action = 0
env.predict(sample_prediction_df )<load_from_csv>
learn.save('stage1' )
submission = pd.read_csv('./submission.csv')
 submission.head()<install_modules>
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()
!pip install -q git+https://github.com/mljar/mljar-supervised.git@dev<import_modules>
learn.fit_one_cycle(15, slice(5e-5))
import pandas as pd
from supervised.automl import AutoML<load_from_csv>
learn.save('stage2' )
train = pd.read_csv(".. /input/bnp-paribas-cardif-claims-management/train.csv.zip")
 test = pd.read_csv(".. /input/bnp-paribas-cardif-claims-management/test.csv.zip")
 sub = pd.read_csv(".. /input/bnp-paribas-cardif-claims-management/sample_submission.csv.zip")
 x_cols = [f for f in train.columns if "v" in f]<train_model>
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()
automl = AutoML(
 total_time_limit=8*3600,
optuna_time_budget=1800,
mode="Optuna", 
)
 automl.fit(train[x_cols], train["target"] )<save_to_csv>
learn.fit_one_cycle(5, 5e-5 )
pred = automl.predict_proba(test)
 sub["PredictedProb"] = pred[:, 1]
sub.to_csv("./1_submission.csv", index=False )<import_modules>
def create_tst(path:Path, test):
path.mkdir(parents=True, exist_ok=True)
 for i in range(len(test)) :
matplotlib.image.imsave(str(path/(str(i)+ '.jpeg')) , test[i] )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedKFold
from sklearn.preprocessing import MinMaxScaler<load_from_csv>
test_df = pd.read_csv(base/'test.csv')
 test_df = np.array(test_df, dtype=np.uint8 ).reshape(-1,28,28)
 test_df = np.stack(( test_df,)*3, axis=-1)
 test_df.shape
data = pd.read_csv('.. /input/ghouls-goblins-and-ghosts-boo/train.csv.zip')
 data<load_from_csv>
tst_path = Path('.. /working/test')
 create_tst(tst_path, test_df )
validate_data = pd.read_csv('.. /input/ghouls-goblins-and-ghosts-boo/test.csv.zip')
 validate_data<define_variables>
preds = []
ImageId = []
for i in range(len(test_df)) :
img = open_image(tst_path/str(str(i)+'.jpeg'))
pred_cls, pred_idx, pred_img = learn.predict(img)
 preds.append(int(pred_idx))
ImageId.append(i+1 )
validate_data_ids = validate_data['id']<count_missing_values>
submission = pd.DataFrame({'ImageId':ImageId, 'Label':preds} )
data.isnull().any()<data_type_conversions>
submission.to_csv('submission.csv',index=False )
<compute_test_metric><EOS> 
shutil.rmtree(tst_path)
 path_val = Path('.. /working/data/valid')
 shutil.rmtree(path_val)
 path_trn = Path('.. /working/data/train')
 shutil.rmtree(path_trn )