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'