Kaggle competition: Digit Recognizer
test_set[norminal_features].isnull().sum()<data_type_conversions>
import time
import torch
import matplotlib.pyplot as plt
from torch import nn, optim
from torchvision import transforms
from torch.utils.data import TensorDataset, DataLoader
for col in norminal_features:
train_set[col] = train_set[col].astype(str)
 test_set[col] = test_set[col].astype(str)
 train_set[col] = train_set[col].fillna('Unknown')
 test_set[col] = test_set[col].fillna('Unknown' )<categorify>
epochs = 50
batch_size = 32
lr = 1e-3
momentum = 0.9
weight_decay = 1e-4
train_ratio = 0.8
device = torch.device("cuda" if torch.cuda.is_available() else "cpu" )
ohe = OneHotEncoder(handle_unknown = "ignore", sparse=False)
 ohe_train_set = pd.DataFrame(ohe.fit_transform(train_set[norminal_features]))
ohe_test_set= pd.DataFrame(ohe.transform(test_set[norminal_features]))
ohe_train_set.index = train_set.index
ohe_test_set.index = test_set.index<drop_column>
train_df = np.array(pd.read_csv('/kaggle/input/digit-recognizer/train.csv'))
imgs = train_df[:,1:].reshape(-1, 28, 28)
 labels = train_df[:,0]
imgs = np.expand_dims(imgs, axis=1)/ 255.0
num_train = int(imgs.shape[0] * train_ratio)
 train_imgs = imgs[:num_train]
train_labels = labels[:num_train]
val_imgs = imgs[num_train:]
val_labels = labels[num_train:]
train_dataset = TensorDataset(torch.tensor(train_imgs, dtype=torch.float), torch.tensor(train_labels))
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, pin_memory=True)
 val_dataset = TensorDataset(torch.tensor(val_imgs, dtype=torch.float), torch.tensor(val_labels))
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, pin_memory=True)
train_set = train_set.drop(norminal_features, axis = 1)
 test_set = test_set.drop(norminal_features, axis = 1 )<concatenate>
def conv_cell(cin, cout, k, s, p=None):
if not p:
p =(k-1)// 2
cell = nn.Sequential()
cell.add_module('conv', nn.Conv2d(in_channels=cin, out_channels=cout, kernel_size=k, stride=s, padding=p, bias=False))
cell.add_module('bn', nn.BatchNorm2d(cout))
cell.add_module('act', nn.ReLU())
 return cell
net = nn.Sequential(
 conv_cell(1, 32, 3, 2),
conv_cell(32, 32, 3, 1),
conv_cell(32, 32, 3, 1),
conv_cell(32, 64, 3, 2),
conv_cell(64, 64, 3, 1),
conv_cell(64, 64, 3, 1),
nn.AdaptiveAvgPool2d(1),
nn.Flatten() ,
nn.Linear(64, 10)
)
 print(net)
 net = net.to(device )
train_base = train_set.copy()
test_base = test_set.copy()
train_set = pd.concat([train_set, ohe_train_set], axis=1)
 test_set = pd.concat([test_set, ohe_test_set], axis =1 )<count_missing_values>
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters() , lr=lr, momentum=momentum, weight_decay=weight_decay )
test_set[discrete_features].isnull().sum()<count_missing_values>
def accuracy(dataloader, net):
total = 0
correct = 0
with torch.no_grad() :
for data in dataloader:
inputs, labels = data[0].to(device), data[1].to(device)
 outputs = net(inputs)
 _, predicted = torch.max(outputs.data, 1)
 total += labels.size(0)
 correct +=(predicted == labels ).sum().item()
return correct/total
train_set[discrete_features].isnull().sum()
test_set[discrete_features].isnull().sum()
for col in discrete_features:
test_set[col] = test_set[col].fillna(0 )<drop_column>
best = 0
train_losses = []
train_accuracies = []
val_accuracies = []
for epoch in range(epochs):
tic = time.time()
epoch_loss = 0
for i, data in enumerate(train_loader):
inputs, labels = data[0].to(device), data[1].to(device)
 optimizer.zero_grad()
outputs = net(inputs)
 loss = loss_fn(outputs, labels)
 loss.backward()
optimizer.step()
epoch_loss += loss.item()
if i % 100 == 99:
print("[Epoch {}/{} Batch {}] lr: {}, loss: {:.4f}".format(
 epoch+1, epochs, i+1, optimizer.param_groups[0]['lr'], epoch_loss/(i+1)))
 toc = time.time()
train_acc = accuracy(train_loader, net)
 val_acc = accuracy(val_loader, net)
 print("Epoch {}, time: {:.2f}s, train accuracy: {:.4f}, val accuracy: {:.4f}".format(
 epoch+1, toc-tic, train_acc, val_acc))
print('-'*30)
 train_losses.append(epoch_loss/(i+1))
train_accuracies.append(train_acc)
 val_accuracies.append(val_acc)
 if val_acc > best:
best = val_acc
torch.save(net.state_dict() , 'mnist_best.pt')
 torch.save(net.state_dict() , 'mnist_last.pt')
 print("Training complete.Best validation accuracy:", best )
correlation_features = corr_cols.append(corr_cols_small ).drop('SalePrice' )<drop_column>
test_df = np.array(pd.read_csv('/kaggle/input/digit-recognizer/test.csv'))
test_imgs = test_df.reshape(-1, 28, 28)
 test_imgs = np.expand_dims(test_imgs, axis=1)/ 255.0
test_dataset = TensorDataset(torch.tensor(test_imgs, dtype=torch.float))
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
 net.load_state_dict(torch.load('mnist_best.pt'))
net.eval()
test_results = []
for data in test_loader:
inputs = data[0].to(device)
 outputs = net(inputs)
 _, predicted = torch.max(outputs.data, axis=1)
 test_results.extend(predicted.tolist() )
<define_variables><EOS> 
num_test = len(test_results)
 test_df = pd.DataFrame(np.vstack(( np.arange(1, num_test+1), np.array(test_results)) ).T, columns=['ImageId', 'Label'])
 test_df.to_csv('submission.csv', index=False )