用反向传播学习识别mnist手写数字(mini-batch版)

NN的学习,用了BP高效求梯度
用了mini-batch一次处理一个batch的数据,加快计算

# BP_Study.py
# 反向传播学习,mnist手写数字分类
# 2层网络

import numpy as np
import time
from dataset.mnist import load_mnist
from TwoLayerNet import TwoLayerNet
import matplotlib.pyplot as plt

start = time.clock()

# 读入数据
(x_train, t_train), (x_test, t_test) = \
    load_mnist(normalize=True, one_hot_label=True)

net = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)

iter_num = 10000
# 每个epoch跑max(1, train_size / batch_size)次,这里是600次
# 即每个epoch随机选600个batch, 一个epoch相当于把整个训练数据集遍历一遍
# 共迭代10000次,所以会有50/3=16个epoch,相当于把训练数据集跑了16遍
learning_rate = 0.1
train_size = x_train.shape[0]
batch_size = 100
train_loss_list = []
train_acc_list = []
test_acc_list = []

iter_per_epoch = max(1, train_size / batch_size)
# 每个epoch跑max(1, train_size / batch_size)次,这里是600次
# 即每个epoch随机选600个batch, 一个epoch相当于把整个训练数据集遍历一遍
# 共迭代10000次,所以会有50/3=16个epoch,相当于把训练数据集跑了16遍

for i in range(iter_num):
    batch_mask = np.random.choice(train_size, batch_size)
    x_batch = x_train[batch_mask]
    t_batch = t_train[batch_mask]

    # 反向传播求梯度
    grad = net.gradient(x_batch, t_batch)

    # 更新参数
    for key in ('w1', 'b1', 'w2', 'b2'):
        net.params[key] -= learning_rate * grad[key]

    loss = net.loss(x_batch, t_batch)
    train_loss_list.append(loss)
    # print('loss:' + str(loss))

    if i % iter_per_epoch == 0:
        # 每个epoch计算一次精度,所以总共只计算16次
        train_acc = net.accuracy(x_train, t_train)
        test_acc = net.accuracy(x_test, t_test)
        train_acc_list.append(train_acc)
        test_acc_list.append(test_acc)
        print('train_acc,test_acc |' + str(train_acc) + ',' + str(test_acc))

# 画损失函数的变化
x1 = np.arange(len(train_loss_list))
ax1 = plt.subplot(211)
plt.plot(x1, train_loss_list)
plt.xlabel("iteration")
plt.ylabel("loss")

# 画训练精度,测试精度随着epoch的变化
markers = {
   
   'train': 'o', 'test': 's'}
x2 = np.arange(len(train_acc_list))
ax2 = plt.subplot(212)
plt.plot(x2, train_acc_list, label='train acc'
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值