MNIST数据集的前向传播

Posted William_Tao(攻城狮)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MNIST数据集的前向传播相关的知识,希望对你有一定的参考价值。

步骤

1.加载数据集
2.使用ReLU激活函数使得范围在【0,1】之间
3.初始化网络
4.运用前向传播函数forward

代码

import torch
from torch.utils.data import DataLoader
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import  numpy as np
def init_network():
    network =
    weight_scale =1e-3
    network['W1'] = np.random.randn(784,50) * weight_scale
    network['b1'] = np.ones(50)
    network['W2'] = np.random.randn(50, 100) * weight_scale
    network['b2'] = np.ones(100)
    network['W3'] = np.random.randn(100, 10) * weight_scale
    network['b3'] = np.ones(10)
    return network
def _relu(x):
    return np.maximum(0,x)
def forward(network,x):
    w1,w2,w3 =network['W1'],network['W2'],network['W3']
    b1,b2,b3 = network['b1'],network['b2'],network['b3']
    a1 = x.dot(w1) + b1
    z1=_relu(a1)
    a2 = z1.dot(w2) + b2
    z2 = _relu(a2)
    a3 = z2.dot(w3) + b3
    y = a3
    return  y

if __name__ == '__main__':
    train_dataset = dsets.MNIST(root='/ml/pymnist',  # 选择数据的根目录
                                train=True,  # 选择训练集
                                transform=None,  # 不考虑使用任何数据预处理
                                download=True)  # 从网络上download图片
    test_dataset = dsets.MNIST(root='/ml/pymnist',  # 选择数据的根目录
                               train=False,  # 选择测试集
                               transform=None,  # 不考虑使用任何数据预处理
                               download=True)  # 从网络上download图片
    network = init_network()
    accuracy_cnt = 0
    x = test_dataset.data.numpy().reshape(-1,28*28)
    labels = test_dataset.targets.numpy()
    for i in range(len(x)):
        y = forward(network,x[i])
        p = np.argmax(y)
        if p == labels[i]:
            accuracy_cnt+=1

        print("Accuracy:" + str(float(accuracy_cnt)/len(x)*100)+"%")

结果

代码优化(批处理)

import torch
from torch.utils.data import DataLoader
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import  numpy as np
def init_network():
    network =
    weight_scale =1e-3
    network['W1'] = np.random.randn(784,50) * weight_scale
    network['b1'] = np.ones(50)
    network['W2'] = np.random.randn(50, 100) * weight_scale
    network['b2'] = np.ones(100)
    network['W3'] = np.random.randn(100, 10) * weight_scale
    network['b3'] = np.ones(10)
    return network
# def _relu(x):
#     return np.maximum(0,x)
def forward(network,x):
    w1,w2,w3 =network['W1'],network['W2'],network['W3']
    b1,b2,b3 = network['b1'],network['b2'],network['b3']
    a1 = x.dot(w1) + b1
    z1=_softmax(a1)
    a2 = z1.dot(w2) + b2
    z2 = _softmax(a2)
    a3 = z2.dot(w3) + b3
    y = a3
    return  y
def _softmax(x):
    if x.ndim ==2:
        c = np.max(x,axis=1)
        x = x.T -c #溢出对策
        y = np.exp(x)/np.sum(np.exp(x),axis=0)
        return y.T
    c = np.max(x)
    exp_x = np.exp(x-c)
    return exp_x / np.sum(exp_x)
if __name__ == '__main__':
    train_dataset = dsets.MNIST(root='/ml/pymnist',  # 选择数据的根目录
                                train=True,  # 选择训练集
                                transform=None,  # 不考虑使用任何数据预处理
                                download=True)  # 从网络上download图片
    test_dataset = dsets.MNIST(root='/ml/pymnist',  # 选择数据的根目录
                               train=False,  # 选择测试集
                               transform=None,  # 不考虑使用任何数据预处理
                               download=True)  # 从网络上download图片
    network = init_network()
    accuracy_cnt = 0
    batch_size = 100
    x = test_dataset.data.numpy().reshape(-1,28*28)
    labels = test_dataset.targets.numpy()
    for i in range(0,len(x),batch_size):
        x_batch = x[i:i+batch_size]

        y_batch = forward(network,x_batch)
        p = np.argmax(y_batch,axis=1)
        accuracy_cnt+=np.sum(p==labels[i:i+batch_size])
        print("Accuracy:" + str(float(accuracy_cnt)/len(x)*100)+"%")

softmax和ReLU区别
ReLU一般处理二分类问题
softmax一般处理三分类以上的问题

以上是关于MNIST数据集的前向传播的主要内容,如果未能解决你的问题,请参考以下文章

卷积神经网络 cnnff.m程序 中的前向传播算法 数据 分步解析

深度学习:神经网络中的前向传播和反向传播算法推导

用numpy实现CNN卷积神经网络

神经网络的前向与后向传播简单理解

深入理解RNN与LSTM

深入理解RNN与LSTM