Kaggle competition: Digit Recognizer
result = pd.DataFrame(
 {
'feature': model_type,
'train time': model_train_time,
'validation prediction time':model_pred_time,
'rmse train':model_rmse_train,
'rmse validation':model_rmse_val
} 
)
 result.sort_values(by='rmse validation' )<train_model>
np.random.seed(42)
 torch.random.seed = 42
rfr=RandomForestRegressor(max_features=0.4, n_estimators=1000, random_state=RANDOM_STATE)
 start_time = time.time()
rfr.fit(train_x, train_y)
 time_lapse = time.time() - start_time
model_type.append('Random Forest')
 model_train_time.append(time_lapse)
 time_lapse<predict_on_test>
train_df = pd.read_csv(dirname+'/train.csv')
 test_df = pd.read_csv(dirname + '/test.csv')
 train_df.head()
rfr_pred_train = rfr.predict(train_x)
 rfr_pred_train_rmse = evaluation_metric(rfr_pred_train, train_y)
 model_rmse_train.append(rfr_pred_train_rmse)
 rfr_pred_train_rmse<predict_on_test>
train = train_df.to_numpy()
test = test_df.to_numpy()
start_time = time.time()
rfr_pred_val = rfr.predict(val_x)
 time_lapse = time.time() - start_time
model_pred_time.append(time_lapse)
<compute_test_metric>
@Transform
def get_label(a):
return a[0]
@Transform
def get_x(a):
return a[-28*28:]
@Transform
def nums_to_tensor(a):
return TensorImageBW(torch.from_numpy(a ).view(1,28,28))
rfr_rmse = evaluation_metric(rfr_pred_val, val_y)
 model_rmse_val.append(rfr_rmse)
 rfr_rmse<create_dataframe>
x_tfms = [get_x,nums_to_tensor, ]
y_tfms = [get_label, Categorize]
cut = int(len(train)*0.8)
 splits = [list(range(cut)) , list(range(cut,len(train)-1)) ]
bs = 128
result = pd.DataFrame(
 {
'feature': model_type,
'train time': model_train_time,
'validation prediction time':model_pred_time,
'rmse train':model_rmse_train,
'rmse validation':model_rmse_val
} 
)
 result.sort_values(by='rmse validation' )<train_model>
dsets = Datasets(train,[x_tfms, y_tfms], splits = splits)
 dls = dsets.dataloaders(bs=bs, after_batch=[IntToFloatTensor() ,*aug_transforms(do_flip=False,batch=True,), Normalize.from_stats(*mnist_stats)] )
mnet = MLPRegressor(hidden_layer_sizes=(200,200,100, 100, 50, 25), activation = 'logistic', alpha = 0.001, max_iter = 1000, early_stopping = True, random_state = RANDOM_STATE)
 start_time = time.time()
mnet.fit(train_x, train_y)
 time_lapse = time.time() - start_time
model_type.append('Neural Net')
 model_train_time.append(time_lapse)
 time_lapse 
<predict_on_test>
test_dl =dls.test_dl(test )
mnet_pred_train = mnet.predict(train_x)
 mnet_pred_train_rmse = evaluation_metric(mnet_pred_train, train_y)
 model_rmse_train.append(mnet_pred_train_rmse)
 mnet_pred_train_rmse<predict_on_test>
model = xresnet18(c_in=1,n_out=10, sa=True, act_cls=Mish )
start_time = time.time()
mnet_pred_val = mnet.predict(val_x)
 time_lapse = time.time() - start_time
model_pred_time.append(time_lapse )<compute_test_metric>
learn = Learner(dls,model, loss_func=CrossEntropyLossFlat() , metrics=accuracy, )
mnet_rmse = evaluation_metric(mnet_pred_val, val_y)
 model_rmse_val.append(mnet_rmse)
 mnet_rmse<create_dataframe>
lr_min, lr_steep = learn.lr_find() ;lr_min, lr_steep
result = pd.DataFrame(
 {
'feature': model_type,
'train time': model_train_time,
'validation prediction time':model_pred_time,
'rmse train':model_rmse_train,
'rmse validation':model_rmse_val
} 
)
 result.sort_values(by='rmse validation' )<train_model>
learn.fit_one_cycle(20,lr_min )
params = {'n_estimators': 1000,
'max_depth': 5,
'min_samples_split': 6,
'learning_rate': 0.01,
'loss': 'ls',
'max_features':0.25,
'random_state':RANDOM_STATE}
gbm = GradientBoostingRegressor(**params)
 start_time = time.time()
gbm.fit(train_x, train_y)
 time_lapse = time.time() - start_time
model_type.append('Gradient Boosting')
 model_train_time.append(time_lapse)
 time_lapse<predict_on_test>
learn.save('20epoch_Xresnet18-sa' )
gbm_pred_train = gbm.predict(train_x)
 gbm_pred_train_rmse = evaluation_metric(gbm_pred_train, train_y)
 model_rmse_train.append(gbm_pred_train_rmse)
 gbm_pred_train_rmse<predict_on_test>
learn = learn.load('20epoch_Xresnet18-sa' )
start_time = time.time()
gbm_pred_val = gbm.predict(val_x)
 time_lapse = time.time() - start_time
model_pred_time.append(time_lapse )<compute_test_metric>
learn.fit_one_cycle(2,lr_min/50 )
gbm_rmse = evaluation_metric(gbm_pred_val, val_y)
 model_rmse_val.append(gbm_rmse)
 gbm_rmse<create_dataframe>
learn.save('20+2epoch_Xresnet18-sa' )
result = pd.DataFrame(
 {
'feature': model_type,
'train time': model_train_time,
'validation prediction time':model_pred_time,
'rmse train':model_rmse_train,
'rmse validation':model_rmse_val
} 
)
 result.sort_values(by='rmse validation' )<train_model>
preds,targets = learn.tta()
accuracy(preds,targets )
param = {'n_estimators': 1000,
'max_depth': 5,
'learning_rate': 0.01,
'random_state':RANDOM_STATE}
xgb = XGBRegressor(**param)
 start_time = time.time()
xgb.fit(train_x, train_y, eval_set=[(train_x, train_y),(val_x, val_y)], early_stopping_rounds = 5, eval_metric = 'rmse')
 time_lapse = time.time() - start_time
model_type.append('XGBoost')
 model_train_time.append(time_lapse)
 time_lapse<predict_on_test>
preds,_ = learn.tta(dl=test_dl )
xgb_pred_train = xgb.predict(train_x)
 xgb_pred_train_rmse = evaluation_metric(xgb_pred_train, train_y)
 model_rmse_train.append(xgb_pred_train_rmse)
 xgb_pred_train_rmse<predict_on_test>
class_preds =np.argmax(preds,axis=1)
 pred_submission = [dls.vocab[i] for i in class_preds]
<compute_test_metric><EOS> 
submission = pd.read_csv(dirname+'/sample_submission.csv')
 submission['Label']=pred_submission
submission.to_csv('submission.csv',index=False )