Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<predict_on_test>
from torchvision import datasets,transforms
from torch.utils.data import TensorDataset, DataLoader,Dataset
from torch import nn,optim
import torch.nn.functional as F
import torchvision.transforms.functional as FF
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
y_valid_preds = gridsearch_logistic_model.predict_proba(X_valid)[:, 1]<compute_test_metric>
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Sequential(
 nn.Conv2d(1, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=5, stride=2, padding=14),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Dropout2d(0.25)
)
 self.conv2 = nn.Sequential(
 nn.Conv2d(32, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=5, stride=2, padding=6),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Dropout2d(0.25)
)
 self.conv3 = nn.Sequential(
 nn.Conv2d(64, 128, kernel_size=4),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Dropout2d(0.25)
)
 self.fc = nn.Sequential(
 nn.Linear(128*1*1, 10)
)
 def forward(self, x):
x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = x.view(-1, 128*1*1)
 x = self.fc(x)
 return x
roc_auc = roc_auc_score(y_valid, y_valid_preds)
 print(f'검증 데이터 ROC AUC : {roc_auc:.4f}' )<save_to_csv>
class MNIST_data(Dataset):

 def __init__(self, file_path,
transform = transforms.Compose([transforms.ToPILImage() , transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
):
df = pd.read_csv(file_path)
 if len(df.columns)== 784:
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] )
y_preds = gridsearch_logistic_model.best_estimator_.predict_proba(X_test)[:,1]
submission['target'] = y_preds
submission.to_csv('submission.csv' )<load_from_csv>
batch_size = 64
train_dataset = MNIST_data('.. /input/digit-recognizer/train.csv', transform= transforms.Compose(
 [transforms.ToPILImage() ,transforms.ToTensor() , transforms.Normalize(mean=(0.5,), std=(0.5,)) ]))
test_dataset = MNIST_data('.. /input/digit-recognizer/test.csv')
 train_size = int(0.85 * len(train_dataset))
valid_size=int(0.15*len(train_dataset))
train_dataset,valid_dataset = torch.utils.data.random_split(train_dataset, [train_size,valid_size])
 train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size, shuffle=True)
 valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset,
batch_size=batch_size, shuffle=True)
 test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size, shuffle=False )
train = pd.read_csv('/kaggle/input/cat-in-the-dat/train.csv', index_col='id')
 test = pd.read_csv('/kaggle/input/cat-in-the-dat/test.csv', index_col='id')
 submission = pd.read_csv('/kaggle/input/cat-in-the-dat/sample_submission.csv', index_col='id' )<concatenate>
dftrain=pd.read_csv(".. /input/digit-recognizer/train.csv")
 dftest=pd.read_csv(".. /input/digit-recognizer/test.csv")
 submission=pd.read_csv(".. /input/digit-recognizer/sample_submission.csv" )
all_data = pd.concat([train, test], ignore_index=True)
 all_data = all_data.drop('target', axis=1 )<categorify>
train=np.array(dftrain.iloc[:,1:])
 labels=dftrain['label']
all_data['bin_3'] = all_data['bin_3'].map({'F':0, 'T':1})
 all_data['bin_4'] = all_data['bin_4'].map({'N':0, 'Y':1} )<categorify>
model=NeuralNetwork().cuda()
print(model )
ord1dict = {'Novice':0, 'Contributor':1,
'Expert':2, 'Master':3, 'Grandmaster':4}
ord2dict = {'Freezing':0, 'Cold':1, 'Warm':2,
'Hot':3, 'Boiling Hot':4, 'Lava Hot':5}
all_data['ord_1'] = all_data['ord_1'].map(ord1dict)
 all_data['ord_2'] = all_data['ord_2'].map(ord2dict )<categorify>
optimizer=optim.Adam(model.parameters() ,lr=0.0003)
 crit=nn.CrossEntropyLoss()
ord_345 = ['ord_3', 'ord_4', 'ord_5']
ord_encoder = OrdinalEncoder()
all_data[ord_345] = ord_encoder.fit_transform(all_data[ord_345])
 for col, categories in zip(ord_345, ord_encoder.categories_):
print(col)
 print(categories )<feature_engineering>
epochs=30
step=0
train_losses,test_losses,accu=[],[],[]
for i in range(epochs):
running_loss=0
for images,labels in train_loader:
images,labels=images.cuda() ,labels.cuda()
optimizer.zero_grad()
logps=model(images)
 loss=crit(logps,labels)
 loss.backward()
optimizer.step()
running_loss+=loss.item()
else:
test_loss=0
accuracy=0
with torch.no_grad() :
model.eval()
for images,labels in valid_loader:
images,labels=images.cuda() ,labels.cuda()
logps=model(images)
 test_loss+=crit(logps,labels)
 ps=torch.exp(logps)
 topp,topc=ps.topk(1,dim=1)
 equal=topc==labels.view(*topc.shape)
 accuracy+=torch.mean(equal.type(torch.FloatTensor))
model.train()
train_losses.append(running_loss/len(train_loader))
test_losses.append(test_loss/len(valid_loader))
accu.append(accuracy)
 print("Epoch: {}/{}".format(i+1,epochs),
"Training Loss {}".format(running_loss/len(train_loader)) ,
"Testing Loss {}".format(test_loss/len(valid_loader)) ,
"Accuracy {}".format(accuracy/len(valid_loader)) )
all_data['nom_5'] = all_data['nom_5'].str[3:]
all_data['nom_6'] = all_data['nom_6'].str[3:]
all_data['nom_7'] = all_data['nom_7'].str[3:]
all_data['nom_8'] = all_data['nom_8'].str[3:]
all_data['nom_9'] = all_data['nom_9'].str[3:]<define_variables>
pred=[]
nom_cols = ['nom_' + str(i)for i in range(10)]<categorify>
with torch.no_grad() :
model.eval()
for images in test_loader:
logps=model(images.cuda())
 ps=torch.exp(logps)
 topp,topc=ps.topk(1,dim=1)
 for i in topc:
pred.append(i[0].item())
nom_onehot_encoder = OneHotEncoder(drop='first')
 encoded_nom_matrix = nom_onehot_encoder.fit_transform(all_data[nom_cols])
 all_data = all_data.drop(nom_cols, axis=1)
 encoded_nom_matrix<categorify>
submission['Label']=pred
<normalization><EOS> 
submission.to_csv("submissionD1.csv",index=False )