text_plain_output_1.png
from sklearn.model_selection import train_test_split

from torch import nn, optim

from torch.autograd import Variable

from torch.utils.data import DataLoader

import matplotlib.pyplot as plt

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()

sample_id = 50

X_train /= 255.0
X_test /= 255.0
X_train = X_train.values
y_train = y_train.values
X_test = X_test.values
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=2)
X_train = torch.from_numpy(X_train).type(torch.FloatTensor)
X_val = torch.from_numpy(X_val).type(torch.FloatTensor)
y_train = torch.from_numpy(y_train).type(torch.LongTensor)
y_val = torch.from_numpy(y_val).type(torch.LongTensor)

batch_size = 100
num_epochs = 20

train = torch.utils.data.TensorDataset(X_train, y_train)
val = torch.utils.data.TensorDataset(X_val, y_val)
train_loader = DataLoader(train, batch_size=batch_size, shuffle=False)
val_loader = DataLoader(val, batch_size=batch_size, shuffle=False)

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

class CNNModel(nn.Module):
    def __init__(self):
        super(CNNModel, self).__init__()
        self.cnn1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=0)
        self.relu1 = nn.ReLU()
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)
        self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=0)
        self.relu2 = nn.ReLU()
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)
        self.fc1 = nn.Linear(32 * 4 * 4, 10)
    def forward(self, x):
        out = self.cnn1(x)
        out = self.relu1(out)
        out = self.maxpool1(out)
        out = self.cnn2(out)
        out = self.relu2(out)
        out = self.maxpool2(out)
        out = out.view(out.size(0), -1)
        out = self.fc1(out)
        return out

class LeNet_dropout(nn.Module):
    def __init__(self):
        super(LeNet_dropout, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, (5, 5), padding=2)
        self.dropout1 = nn.Dropout2d(p=0.2)
        self.conv2 = nn.Conv2d(6, 16, (5, 5))
        self.dropout2 = nn.Dropout2d(p=0.2)
        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.dropout1(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, (2, 2))
        x = self.dropout2(x)
        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

model = LeNet()
if use_gpu:
    model = model.cuda()

model = LeNet_dropout()
if use_gpu:
    model = net.cuda()

model = CNNModel()
if use_gpu:
    model = net.cuda()

criterion = nn.CrossEntropyLoss()

optimizer = optim.SGD(model.parameters(), lr=0.001)

count = 0
loss_list = []
val_loss_list = []
iteration_list = []
accuracy_list = []
for epoch in range(num_epochs):
    for images, labels in train_loader:
        train_batch = Variable(images.view(batch_size, 1, 28, 28), requires_grad=True)
        labels_batch = Variable(labels, requires_grad=False)
        if use_gpu:
            train_batch = train_batch.cuda()
            labels_batch = labels_batch.cuda()
        optimizer.zero_grad()
        outputs = model(train_batch)
        loss = criterion(outputs, labels_batch)
        loss.backward()
        optimizer.step()
        count += 1
        if count % 50 == 0:
            correct = 0
            total = 0
            for images, labels in val_loader:
                val_batch = Variable(images.view(-1, 1, 28, 28), requires_grad=False)
                labels_batch = Variable(labels, requires_grad=False)
                if use_gpu:
                    val_batch = val_batch.cuda()
                    labels_batch = labels_batch.cuda()
                outputs = model(val_batch)
                pred = torch.max(outputs.data, 1)[1]
                val_loss = criterion(outputs, labels_batch)
                total += len(labels)
                correct += (pred == labels_batch).sum()
            accuracy = correct / float(total) * 100
            loss_list.append(loss.data)
            val_loss_list.append(val_loss.data)
            iteration_list.append(count)
            accuracy_list.append(accuracy)

plt.figure(figsize=(15, 8))
plt.plot(iteration_list, loss_list, label='training')
plt.plot(iteration_list, val_loss_list, label='validation')
plt.xlabel('Number of iteration')
plt.ylabel('Loss')
plt.title('CNN: Loss vs Number of iteration')
plt.legend()
plt.show()
plt.figure(figsize=(15, 8))
plt.plot(iteration_list, accuracy_list, color='red')
plt.xlabel('Number of iteration')
plt.ylabel('Accuracy')
plt.title('CNN: Accuracy vs Number of iteration')
plt.show()