<jupyter_start><jupyter_text>Digit Recognizer by Neural Network `神经网络` 训练识别手写数字体`Accuracy:` 96.64%`Time:` 386.67s---Created on 2019-05-24Update  on 2019-05-26Author: 家丞Github: https://github.com/Jiachengciel/Kaggle --- 一. 基础环境配置<jupyter_code>import os
import numpy as np
import pandas as pd
import seaborn as sns
import time
import matplotlib.pyplot as plt

import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torch.autograd import Variable
from sklearn.model_selection import train_test_split

%matplotlib inline
sns.set(style='white', context='notebook', palette='deep')<jupyter_output><empty_output><jupyter_text>--- 二. 数据分析  2.1 数据导入<jupyter_code># Load the data
train_data = pd.read_csv("input/train.csv")

# Classify the data
Y_data = train_data.label
X_data = train_data.drop(labels=['label'], axis=1)

# Free some space
del train_data

print("Number of examples: {}, shape of input: {}".format(X_data.shape[0], X_data.shape))<jupyter_output>Number of examples: 42000, shape of input: (42000, 784)<jupyter_text>2.2 数据可视化<jupyter_code>g = sns.countplot(Y_data)
Y_data.value_counts()<jupyter_output><empty_output><jupyter_text>--- 三. 特征工程  3.1 检查缺失值<jupyter_code>X_data.isnull().any().describe()<jupyter_output><empty_output><jupyter_text>3.2 标准化<jupyter_code>X_np = X_data.values / 255 
Y_np = Y_data.values<jupyter_output><empty_output><jupyter_text>3.3 分割训练集和测试集<jupyter_code>X_train, X_test, Y_train, Y_test = train_test_split(X_np, Y_np,
                                                   test_size=0.2,
                                                   random_state=16)
print("Quantity of examples in Train: {}\nQuantity of examples in Test: {}".format(X_train.shape[0], X_test.shape[0]))<jupyter_output>Quantity of examples in Train: 33600
Quantity of examples in Test: 8400<jupyter_text>3.4 转换为Tensor形式<jupyter_code>X_Train = torch.from_numpy(X_train)
Y_Train = torch.from_numpy(Y_train).type(torch.LongTensor)

X_Test = torch.from_numpy(X_test)
Y_Test = torch.from_numpy(Y_test).type(torch.LongTensor)
# 可视化
plt.imshow(X_np[26].reshape(28,28))
plt.axis('off')
plt.title(str(Y_np[26]))
plt.show()<jupyter_output><empty_output><jupyter_text>--- 四. 模型选择和使用  4.1 定义参数<jupyter_code>BATCH_SIZE = 50
EPOCH = 15

Input_dim = 28*28
Hidden1_dim = 250
Hidden2_dim = Hidden3_dim = 100
Output_dim = 10

Learning_Rate = 0.02<jupyter_output><empty_output><jupyter_text>4.2 载入数据<jupyter_code>train = torch.utils.data.TensorDataset(X_Train, Y_Train)
test = torch.utils.data.TensorDataset(X_Test, Y_Test)

train_loader = torch.utils.data.DataLoader(train, BATCH_SIZE, shuffle=True)
test_loader = torch.utils.data.DataLoader(test, BATCH_SIZE, shuffle=False)<jupyter_output><empty_output><jupyter_text>4.3 定义模型<jupyter_code>class NeuralNetwork(nn.Module):
    def __init__(self, Input_dim, Hidden1_dim, Hidden2_dim, Hidden3_dim, Output_dim):
        super(NeuralNetwork, self).__init__()
        
        self.NN = nn.Sequential(
            # 1 layer: 784 --> 250
            nn.Linear(Input_dim, Hidden1_dim),
            nn.ReLU(),
            # 2 layer: 250 --> 100
            nn.Linear(Hidden1_dim, Hidden2_dim),
            nn.Tanh(),
            # 3 layer: 100 --> 100
            nn.Linear(Hidden2_dim, Hidden3_dim),
            nn.ELU(),
        )
         # 4 layer: 100 --> 10
        self.out = nn.Linear(Hidden3_dim, Output_dim)
        
    def forward(self, x):
        y = self.NN(x)
        out = self.out(y)
        return out
    
model = NeuralNetwork(Input_dim, Hidden1_dim, Hidden2_dim, Hidden3_dim, Output_dim)
error = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=Learning_Rate)

print(model)<jupyter_output>NeuralNetwork(
  (NN): Sequential(
    (0): Linear(in_features=784, out_features=250, bias=True)
    (1): ReLU()
    (2): Linear(in_features=250, out_features=100, bias=True)
    (3): Tanh()
    (4): Linear(in_features=100, out_features=100, bias=True)
    (5): ELU(alpha=1.0)
  )
  (out): Linear(in_features=100, out_features=10, bias=True)
)<jupyter_text>4.4 训练模型<jupyter_code>count = 0
loss_list =[]
iteration_list = []
accuracy_list = []
start = time.clock()

for epoch in range(EPOCH):
    for step, (images, labels) in enumerate(train_loader):
        train = Variable(images.view(-1, 28*28))
        labels = Variable(labels)
        
        # Clear gradients
        optimizer.zero_grad()
        
        # Forward 
        outputs = model(train.float())
        
        # Calculate the loss
        loss = error(outputs, labels)
        
        # Backward
        loss.backward()
        
        # Update parameters
        optimizer.step()
        
        count += 1
        
        if count % 50 == 0:
            # Calculate accuracy
            correct = 0
            
            for images, labels in test_loader:
                test = Variable(images.view(-1, 28*28))
                
                outputs = model(test.float())
                
                # 提取indices
                predicted = torch.max(outputs.data, 1)[1]
                
                correct += (predicted == labels).sum()
            
            accuracy = round(float(100 * correct.item() / float(X_test.shape[0])), 2)
            
            # Store loss and accuracy
            loss_list.append(loss.data)
            iteration_list.append(count)
            accuracy_list.append(accuracy)
            
            if count % 500 == 0:
                print('Iteration: {} Loss: {} Accuracy: {} %'.format(count, loss.data, accuracy))

                
end = time.clock()
print("运行时间：{} s".format(end-start))<jupyter_output>Iteration: 500 Loss: 1.1888461112976074 Accuracy: 74.51 %
Iteration: 1000 Loss: 0.651594877243042 Accuracy: 87.43 %
Iteration: 1500 Loss: 0.3641195595264435 Accuracy: 89.65 %
Iteration: 2000 Loss: 0.1792561411857605 Accuracy: 90.9 %
Iteration: 2500 Loss: 0.3956226408481598 Accuracy: 91.7 %
Iteration: 3000 Loss: 0.16354869306087494 Accuracy: 92.68 %
Iteration: 3500 Loss: 0.21069036424160004 Accuracy: 93.42 %
Iteration: 4000 Loss: 0.18269020318984985 Accuracy: 94.13 %
Iteration: 4500 Loss: 0.11764658242464066 Accuracy: 94.48 %
Iteration: 5000 Loss: 0.03900475427508354 Accuracy: 94.95 %
Iteration: 5500 Loss: 0.16841179132461548 Accuracy: 95.39 %
Iteration: 6000 Loss: 0.1387794464826584 Accuracy: 95.57 %
Iteration: 6500 Loss: 0.058572303503751755 Accuracy: 95.85 %
Iteration: 7000 Loss: 0.047127190977334976 Accuracy: 95.9 %
Iteration: 7500 Loss: 0.03813285753130913 Accuracy: 96.19 %
Iteration: 8000 Loss: 0.06524886935949326 Accuracy: 96.19 %
Iteration: 8500 Loss: 0.029939480125904083 Accura[...]<jupyter_text>4.5 可视化训练过程<jupyter_code>plt.plot(iteration_list, loss_list)
plt.xlabel("Number of iteration")
plt.ylabel("Loss")
plt.title("NN: Loss vs Number of iteration")
plt.show()

plt.plot(iteration_list,accuracy_list,color = "red")
plt.xlabel("Number of iteration")
plt.ylabel("Accuracy")
plt.title("ANN: Accuracy vs Number of iteration")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12216678545972916, 'fraction_numerical': 0.09080541696364933, 'mean_word_length': 3.2780487804878047, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '449457', 'license': 'MIT', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2577, 'n_words': 490, 'path': 'competitions/getting_started/Digit_Recognizer/Neural Network.ipynb', 'repo_name': 'Jiachengciel/Kaggle', 'size': '7015'}