Kaggle competition: Digit Recognizer
epochs = 50
batch_size = 1024 * 4
verbose = True
objective = 'val_output_layer_3_average_auc'
objective = 'output_layer_3_average_auc'
direction = 'max'
tr =(0, 400)
 te =(420, 500)
 train_indices = train[(train.date >= tr[0])&(train.date < tr[1])].index
test_indices = train[(train.date >= te[0])&(train.date < te[1])].index
model = create_model(input_dim=130, output_dims=(1,1,5,5), add_models=3)
 X_train, X_test = X[train_indices], X[test_indices]
y_train =(y_actions[train_indices][:,3], y_resps[train_indices][:,3], y_actions[train_indices], y_resps[train_indices])
 y_test =(y_actions[test_indices][:,3], y_resps[test_indices][:,3], y_actions[test_indices], y_resps[test_indices])
 rlr = ReduceLROnPlateau(monitor = objective, factor = 0.5, patience = 4, verbose = 1, min_delta = 1e-4, mode = direction)
 es = EarlyStopping(objective, patience=21, restore_best_weights=True, mode=direction)
 h = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=verbose, callbacks = [es, rlr])
 model.save_weights('./model.hdf5')
 metrics = model.evaluate(X_test, y_test, batch_size=batch_size)
 print(metrics )<train_model>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.utils import to_categorical
import os
import time
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Flatten,Dropout,MaxPooling2D,BatchNormalization
from keras.callbacks import EarlyStopping
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
import seaborn as sns
from keras import optimizers
from sklearn.metrics import classification_report
tf.keras.utils.plot_model(model, to_file=f'model.png', show_shapes=True )<predict_on_test>
path_train='.. /input/train.csv'
path_test=".. /input/test.csv"
train=pd.read_csv(path_train)
 test=pd.read_csv(path_test)
 X_train=train.drop("label",axis=1 ).values
Y_train=train["label"].values
X_test=test.values
X_train=X_train/X_train.max()
X_test=X_test/X_test.max()
pred = model.predict(X_test, batch_size=batch_size, verbose=True )<split>
label=[0,1,2,3,4,5,6,7,8,9]
nc=10
Y_train_d=to_categorical(Y_train,10)
 X_train_c=X_train.reshape(-1,28,28,1)
 X_test_c=X_test.reshape(-1,28,28,1 )
env = janestreet.make_env()
iter_test = env.iter_test()<define_variables>
np.random.seed(2)
 m=Sequential()
m.add(Conv2D(filters=128,kernel_size=4,padding="same",activation="relu",input_shape=(28,28,1)))
 m.add(Conv2D(filters=128,kernel_size=4,padding="same",activation="relu"))
m.add(MaxPooling2D(pool_size=2,strides=2))
m.add(Dropout(0.2))
m.add(Conv2D(filters=64,kernel_size=4,padding="same",activation="relu",))
m.add(Conv2D(filters=64,kernel_size=4,padding="same",activation="relu"))
m.add(MaxPooling2D(pool_size=2,strides=2))
m.add(Dropout(0.2))
m.add(Flatten())
 m.add(Dense(1024,activation="relu"))
m.add(Dropout(0.2))
m.add(Dense(512,activation="relu"))
m.add(Dropout(0.4))
m.add(Dense(256,activation="relu"))
m.add(Dropout(0.6))
m.add(Dense(128,activation="relu"))
m.add(Dense(nc,activation='softmax'))
m.summary()
selected_models = [model]<feature_engineering>
el=EarlyStopping(monitor='val_loss',min_delta=0.001,patience=5,restore_best_weights=True)
 ad=optimizers.Adam(lr=0.002,beta_1=0.9,beta_2=0.999,decay=0.004)
 m.compile(loss="categorical_crossentropy",optimizer=ad,metrics=["accuracy"])
 s=time.time()
h=m.fit(X_train_c,Y_train_d,batch_size=32,validation_split=0.4,epochs=50,callbacks=[el])
 e=time.time()
t=e-s
print("Addestramento completato in %d minuti e %d secondi" %(t/60,t*60))
start = time.time()
th = 0.5
j = 0
for(test_df, pred_df)in tqdm(iter_test):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* f_median
try:
pred = model(x_tt, training=False)[2].numpy().flatten()
pred = np.median(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 except:
pred_df.action = 0
else:
pred_df.action = 0
env.predict(pred_df)
 j +=1
total = time.time() - start
print(f'Expected time for 1M: { total * 1000000 /(j*60*60+1):.2} hours')
 print(f'Iters per second: {j/total:.1f} iter/s')
 print(f'Global time: {(time.time() - START_TIME)/ 60:.1f} minutes' )<load_pretrained>
acc=h.history['acc']
val_acc=h.history['val_acc']
loss=h.history['loss']
val_loss=h.history['val_loss']
<import_modules><EOS> 
y_test=m.predict(X_test_c)
 y_test = np.argmax(y_test,axis = 1)
 out=pd.DataFrame({"ImageId": list(range(1,len(y_test)+1)) ,"Label": y_test})
 out.to_csv("Submission_cnn.csv", index=False, header=True )