Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<compute_train_metric>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import torch
import torch.nn as nn
from torchvision import transforms, models
from torch.utils.data import DataLoader, Dataset
from torch.autograd import Variable
from torch import nn, optim
from torch.optim import lr_scheduler
import torch.nn.functional as F
import os
def model_val(model_name, model_obj):
now_time = time.time()
model_obj.fit(X_train, y_train)
 scores = cross_val_score(model_obj, X_test, y_test, cv=5,
scoring='neg_mean_squared_error')
 avg_score = np.abs(scores.mean())
 print("MSE 평균 : ", avg_score)
 pro_time = time.time() - now_time
print("수행 시간 : {0:.3f}".format(pro_time))
print("{} Score : {}".format(model_name, avg_score))
return avg_score, pro_time<define_variables>
WORKING_FOLDER = "/kaggle/input/digit-recognizer"
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )
model_list = ["RandomForestRegressor", "xgb_basic",
"lightgbm-model1", "lightgbm-model2"]
exe_model = []
model_score = []
model_time = []<find_best_params>
train_csv_df = pd.read_csv(os.path.join(WORKING_FOLDER, "train.csv"))
print(train_csv_df.label.head())
 print(train_csv_df.info() )
m_name = model_list[0]
if m_name not in exe_model:
model = RandomForestRegressor(random_state=30)
 mse_score, p_time = model_val(m_name, model)
 exe_model.append(m_name)
 model_score.append(mse_score)
 model_time.append(p_time)
 else:
print(f"{m_name} 이미 실행 완료" )<import_modules>
n_feat_cols = 28 * 28
class MNIST_Dataset(Dataset):

 def __init__(self, dataframe, transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=20, translate=(0.1, 0.1)) ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
])) :
df = dataframe
if len(df.columns)== n_feat_cols:
self.X = df.values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = None
else:
self.X = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = torch.from_numpy(df.iloc[:,0].values)
 self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.y is not None:
return self.transform(self.X[idx]), self.y[idx]
else:
return self.transform(self.X[idx])
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import AdaBoostRegressor<choose_model_class>
class MNIST_ResNet(ResNet):
def __init__(self):
super(MNIST_ResNet, self ).__init__(BasicBlock, [2, 2, 2, 2], num_classes=10)
 self.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=1, padding=3,bias=False)
 model = MNIST_ResNet()
model = model.to(device)
xg_reg = xgb.XGBRegressor(objective ='reg:linear',
colsample_bytree = 0.3,
learning_rate = 0.1,
max_depth = 3,
alpha = 0.1,
n_estimators = 1000)
 mse_score, p_time = model_val('xgb_basic', xg_reg)
 exe_model.append('xgb_basic')
 model_score.append(mse_score)
 model_time.append(p_time )<choose_model_class>
train_csv_labels = train_csv_df.iloc[:,0].values
batch_size = 256
train_full_dataset = MNIST_Dataset(train_csv_df)
 train_full_loader = torch.utils.data.DataLoader(dataset=train_full_dataset, batch_size=batch_size, shuffle=True)
m_lgbm1 = lgb.LGBMRegressor()
mse_score, p_time = model_val('lightgbm-model1', m_lgbm1)
 exe_model.append('lightgbm-model1')
 model_score.append(mse_score)
 model_time.append(p_time )<init_hyperparams>
epochs = 50
steps = 0
print_every = 132
train_losses, val_losses = [], []
step_size = 10
base_lr = 0.01
optimizer = optim.Adam(model.parameters() , lr=base_lr)
 criterion = nn.CrossEntropyLoss()
exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=step_size, gamma=0.1)
 print("Training...")
 for e in range(epochs):
print("Epoch ", e)
 running_loss = 0
for images, labels in train_full_loader:
images = images.to(device)
 labels = labels.to(device)
 steps += 1
optimizer.zero_grad()
log_ps = model(images)
 loss = criterion(log_ps, labels)
 loss.backward()
optimizer.step()
running_loss += loss.item()
if steps % print_every == 0:
train_losses.append(running_loss/len(train_full_loader))
print("Epoch: {}/{}.. ".format(e+1, epochs),
"Training Loss: {:.3f}.. ".format(train_losses[-1])
)
 exp_lr_scheduler.step()
hyperparameters = {'boosting_type': 'gbdt',
'colsample_bytree': 0.7250136792694301,
'is_unbalance': False,
'learning_rate': 0.013227664889528229,
'min_child_samples': 20,
'num_leaves': 56,
'reg_alpha': 0.7543896477745794,
'reg_lambda': 0.07152751159655985,
'subsample_for_bin': 240000,
'subsample': 0.5233384321711397,
'n_estimators': 2000}
m_lgbm2 = lgb.LGBMRegressor(**hyperparameters)
 mse_score, p_time = model_val('lightgbm-model2', m_lgbm2)
 exe_model.append('lightgbm-model2')
 model_score.append(mse_score)
 model_time.append(p_time )<train_model>
test_csv_df = pd.read_csv(os.path.join(WORKING_FOLDER, "test.csv"))
batch_size = 256
test_dataset = MNIST_Dataset(test_csv_df)
 test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False )
print(exe_model)
 print(model_score)
 print(model_time )<define_variables>
submission = [['ImageId', 'Label']]
with torch.no_grad() :
model.eval()
image_id = 1
for images in test_loader:
images = images.to(device)
 log_ps = model(images)
 ps = torch.exp(log_ps)
 top_p, top_class = ps.topk(1, dim=1)
 for prediction in top_class:
submission.append([image_id, prediction.item() ])
 image_id += 1
<init_hyperparams><EOS> 
submission_df = pd.DataFrame(submission)
 submission_df.columns = submission_df.iloc[0]
submission_df = submission_df.drop(0, axis=0)
 submission_df.to_csv("submission.csv", index=False )