手写数字识别单一图像和批量图像—python实现

手写数字识别 python实现

# coding: utf-8
import sys, os
sys.path.append(os.pardir) 
import numpy as np
import pickle
from dataset.mnist import load_mnist
import time

def softmax(x):
    x = x - np.max(x) # 溢出对策
    return np.exp(x) / np.sum(np.exp(x))
def sigmoid(x):
    return 1 / (1 + np.exp(-x))  

def get_data():
    (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)
    return x_test, t_test


def init_network():
    with open("sample_weight.pkl", 'rb') as f:
        network = pickle.load(f)
    return network


def predict(network, x):
    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']

    a1 = np.dot(x, W1) + b1
    z1 = sigmoid(a1)
    a2 = np.dot(z1, W2) + b2
    z2 = sigmoid(a2)
    a3 = np.dot(z2, W3) + b3
    y = softmax(a3)

    return y


x, t = get_data()
network = init_network()
accuracy_cnt = 0
start = time.time()
for i in range(len(x)):
    y = predict(network, x[i])
    p= np.argmax(y) # 获取概率最高的元素的索引
    if p == t[i]:
        accuracy_cnt += 1
end = time.time()
print("time:",end-start)
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
time: 1.3383796215057373
Accuracy:0.9352

网络形状解析

    x,_ = get_data()
    print(x.shape)
    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    print(W1.shape,W2.shape,W3.shape)

输出结果如下:

(10000, 784)#测试数据为10000 784
(784, 50) (50, 100) (100, 10)

在这里插入图片描述
处理过程:输入一个由784个元素构成的一维数组后,输出一个有10个元素的一维数组。
如果输入多张呢?
在这里插入图片描述
输入数据的形状为 100 × 784,输出数据的形状为
100 × 10。这表示输入的100张图像的结果被一次性输出了
批处理对计算机的运算大有好处,可以缩短每张图像的处理时间。因为大多数处理数值计算的库都进行了能够高效处理大型数组运算的最优化。并且在神经网络中,当数据传送称为瓶颈时,可以将更多的时间用在计算上。

# coding: utf-8
import sys, os
sys.path.append(os.pardir)  
import numpy as np
import pickle
from dataset.mnist import load_mnist

def softmax(x):
    x = x - np.max(x) # 溢出对策
    return np.exp(x) / np.sum(np.exp(x))
def sigmoid(x):
    return 1 / (1 + np.exp(-x))  

def get_data():
    (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)
    return x_test, t_test


def init_network():
    with open("sample_weight.pkl", 'rb') as f:
        network = pickle.load(f)
    return network


def predict(network, x):
    w1, w2, w3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']

    a1 = np.dot(x, w1) + b1
    z1 = sigmoid(a1)
    a2 = np.dot(z1, w2) + b2
    z2 = sigmoid(a2)
    a3 = np.dot(z2, w3) + b3
    y = softmax(a3)

    return y


x, t = get_data()
network = init_network()

batch_size = 100 # 批数量
accuracy_cnt = 0
start = time.time()
for i in range(0, len(x), batch_size):
    x_batch = x[i:i+batch_size]
    y_batch = predict(network, x_batch)
    p = np.argmax(y_batch, axis=1)
    accuracy_cnt += np.sum(p == t[i:i+batch_size])
end = time.time()
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))

time: 0.06778287887573242
Accuracy:0.9352

批量处理时间比不批量快大约20倍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为了维护世界和平_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值