text_plain_output_1.png
from torch import nn, optim

from torch.autograd import Variable

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

import torch

import torch.nn.functional as F

use_gpu = torch.cuda.is_available()
use_gpu

df_train_path = '/kaggle/input/digit-recognizer/train.csv'
df_test_path = '/kaggle/input/digit-recognizer/test.csv'
X_train = pd.read_csv(df_train_path)
X_test = pd.read_csv(df_test_path)
y_train = X_train['label']
X_train = X_train.drop('label', axis=1)

sns.set_style('darkgrid')
y_train.unique()

X_train /= 255.0
X_test /= 255.0
X_train = X_train.values
y_train = y_train.values
X_test = X_test.values

X_train = X_train.reshape(-1, 1, 28, 28)
X_val = X_val.reshape(-1, 1, 28, 28)

X_train = torch.from_numpy(X_train)
X_val = torch.from_numpy(X_val)
y_train = torch.from_numpy(y_train)
y_val = torch.from_numpy(y_val)

X_train.shape

class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, (5, 5), padding=2)
        self.conv2 = nn.Conv2d(6, 16, (5, 5))
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = F.max_pool2d(x, (2, 2))
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, (2, 2))
        shape = x.size()[1:]
        features = 1
        for s in shape:
            features *= s
        x = x.view(-1, features)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.fc2(x)
        x = F.relu(x)
        x = self.fc3(x)
        return x
net = LeNet()
if use_gpu:
    net = net.cuda()

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001)

n_samples = X_train.shape[0]
batch_size = 4
n_epochs = 2
for epoch in range(0, n_epochs):
    for i in range(0, n_samples, batch_size):
        if i + batch_size >= n_samples:
            mini_batch_data = Variable(X_train[i:n_samples, :, :, :].clone(), requires_grad=True)
            mini_batch_label = Variable(y_train[i:n_samples].clone(), requires_grad=False)
        else:
            mini_batch_data = Variable(X_train[i:i + batch_size, :, :, :].clone(), requires_grad=True)
            mini_batch_label = Variable(y_train[i:i + batch_size].clone(), requires_grad=False)
        mini_batch_data = mini_batch_data.type(torch.FloatTensor)
        mini_batch_label = mini_batch_label.type(torch.LongTensor)
        if use_gpu:
            mini_data = mini_batch_data.cuda()
            mini_label = mini_batch_label.cuda()
        optimizer.zero_grad()
        batch_output = net(mini_data)
        batch_loss = criterion(batch_output, mini_label)
        batch_loss.backward()
        optimizer.step()

n_val_samples = X_val.shape[0]
true_counter = 0
for val_idx in range(n_val_samples):
    val_sample = X_val[val_idx].clone().unsqueeze(dim=0)
    val_sample = val_sample.type(torch.FloatTensor)
    if use_gpu:
        val_sample = val_sample.cuda()
    pred = net(val_sample)
    _, pred = torch.max(pred, 1)
    if pred == y_val[val_idx]:
        true_counter += 1
true_counter /= 1.0

X_test = X_test.reshape(-1, 1, 28, 28)
X_test = torch.from_numpy(X_test)
n_test_samples = X_test.shape[0]
net.eval()

output_file = np.ndarray(shape=(n_test_samples, 2), dtype=int)
for test_idx in range(n_test_samples):
    test_sample = X_test[test_idx].clone().unsqueeze(dim=1)
    test_sample = test_sample.type(torch.FloatTensor)
    if use_gpu:
        test_sample = test_sample.cuda()
    pred = net(test_sample)
    _, pred = torch.max(pred, 1)
    output_file[test_idx][0] = test_idx + 1
    output_file[test_idx][1] = pred
submission = pd.DataFrame(output_file, dtype=int, columns=['ImageId', 'Label'])

sample = 150
plt.imshow(X_test[sample][0].numpy())
print(output_file[sample][1])