Kaggle competition: Digit Recognizer
class Validation_Metrics(object):
def __init__(self,activation_func=nn.Softmax(dim=1)) :
self.predictions=[]
self.labels=[]
self.activation_func=activation_func
self.collapsed=False
def update(self,model_outputs,labels):

 if not self.collapsed:
self.predictions.append(self.activation_func(model_outputs ).detach())
 self.labels.append(labels.detach())
 else:
raise ValueError('Error, one cannot add further values to a logger once it has been collapsed')
 def collapse(self):

 if self.collapsed:
pass
else:
self.predictions=torch.cat(self.predictions ).cpu().numpy()
self.labels=torch.cat(self.labels ).cpu().numpy()
self.collapsed=True
def Confusion_matrix(self):

 self.collapse()
Confusion_matrix=np.zeros(10,10)
 pred=np.argmax(self.predictions,axis=1)
 labels=self.labels
return confusion_matrix(labels,pred)
 def AUC(self):

 self.collapse()
pred=self.predictions
labels=np.zeros(pred.shape)
 labels[np.arange(label.shape[0]),self.labels]=1.0
aucs = []
for i in range(labels.shape[1]):
aucs.append(roc_auc_score(labels[:, i], pred[:, i]))
return aucs
def F1_score(self):
self.collapse()
pred=np.argmax(self.predictions,axis=1)
 labels=self.labels
return f1_score(labels, pred, average=None)
 def Accuracy(self):
self.collapse()
pred=np.argmax(self.predictions,axis=1)
 labels=self.labels
return accuracy_score(labels, pred)
<compute_train_metric>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 print(train.shape)
 train.head()
def val(model,dataloader,criterion,device):
total_loss=0
n_total=0
criterion.reduction="sum"
Metrics=Validation_Metrics()
model.eval()
with torch.no_grad() :
for images,labels in tqdm.tqdm(dataloader):
images=images.to(device)
 labels=labels.to(device)
 outputs=model(images)
 loss=criterion(outputs,labels)
 Metrics.update(outputs,labels)
 total_loss+=loss.item()
n_total+=labels.shape[0]
return total_loss/n_total,Metrics 
<define_variables>
x_train =(train.iloc[:,1:].values ).astype('float32')
 y_train = train.iloc[:,0].values.astype('int32')
 x_test = test.values.astype('float32' )
n_folds=5<prepare_output>
x_train = x_train/255.0
x_test = x_test/255.0
train_df.insert(1,"fold",np.random.randint(1,n_folds+1,size=train_df.shape[0]))<define_variables>
y_train = keras.utils.to_categorical(y_train, 10)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state = 42 )
def Get_Train_Val_Set(fold_i,transform=transform):
train_set=TrainDataSet(train_df[train_df.fold!=fold_i],transforms=transform)
 test_set=TrainDataSet(train_df[train_df.fold==fold_i],transforms=transform)
 return train_set, test_set<set_options>
batch_size = 64
epochs = 20
input_shape =(28, 28, 1 )
USE_CUDA = torch.cuda.is_available()
device = torch.device("cuda" if USE_CUDA else "cpu" )<choose_model_class>
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal'))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.20))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax'))
criterion=nn.CrossEntropyLoss()
optimizer_name="Adam"
optimizer_parameters={"lr":0.0001}
epochs=1<choose_model_class>
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics = ['accuracy'] )
def create_optimizer(model,optimizer_name,optimizer_parameters):
if optimizer_name=="SGD":
return optim.SGD(model.parameters() ,**optimizer_parameters)
 elif optimizer_name=="Adam":
return optim.Adam(model.parameters() ,**optimizer_parameters )<load_pretrained>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=15,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False )
Best_val_accuracy=0
for fold in range(1,n_folds+1):
print(f"Training fold {fold}")
 model=create_model()
model.to(device)
 optimizer=create_optimizer(model,optimizer_name,optimizer_parameters)
 TrainSet,ValSet=Get_Train_Val_Set(fold)
 TrainLoader=DataLoader(TrainSet, batch_size=256)
 ValLoader=DataLoader(ValSet, batch_size=1024)
 for epoch in range(epochs):
train_loss=train_once(model,TrainLoader,criterion,optimizer,device)
 print(f"For epoch {epoch+1}, the Train Loss was: {train_loss}")
 val_loss,Metrics=val(model,ValLoader,criterion,device)
 print(f"The Val Loss was {val_loss}, and the val accuracy was {Metrics.Accuracy() }")
 if Metrics.Accuracy() >Best_val_accuracy:
print("New Best, saving")
 torch.save(model.state_dict() ,f"fold{fold}Best.pt")
<choose_model_class>
datagen.fit(X_train)
 model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 1,
steps_per_epoch = X_train.shape[0] // batch_size
)
optimizer=create_optimizer(model,optimizer,optimizer_parameters)
 optimizer<define_variables>
predictions = model.predict(X_test)
 results = np.argmax(predictions, axis = 1 )
<create_dataframe><EOS> 
submissions = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 submissions['Label'] = results
submissions.to_csv('submission.csv', index = False )