用LSTM做手写数字识别

参考网址:https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-08-RNN2/ ,感谢!

一张图片是28*28,我们把28行作为28个时刻的输入。

注意输入的维度,初始状态的维度,LSTM中隐藏层的维度,最后输出的维度。

一个LSTM单元的输入是向量,所以tf.contrib.rnn.BasicLSTMCell有参数n_hidden_units,也就是隐藏单元个数。

初始状态的个数跟batch_size和隐藏单元个数有关,这里初始状态的shape=(256, 128)。

如果使用tf.nn.dynamic_rnn(cell, inputs), 我们要确定 inputs 的格式. tf.nn.dynamic_rnn 中的 time_major 参数会针对不同 inputs 格式有不同的值.
如果 inputs 为 (batches, steps, inputs) ==> time_major=False;
如果 inputs 为 (steps, batches, inputs) ==> time_major=True;
在dynamic_rnn的源码中是这样解释的:
输入inputs:
The RNN inputs.
If time_major == False (default), this must be a Tensor of shape:[batch_size, max_time, ...], or a nested tuple of such
elements.
If time_major == True, this must be a Tensor of shape: [max_time, batch_size, ...], or a nested tuple of such
elements.
输出outputs:
The RNN output Tensor.
If time_major == False (default), this will be a Tensor shaped:[batch_size, max_time, cell.output_size].
If time_major == True, this will be a Tensor shaped: [max_time, batch_size, cell.output_size].

计算最后的结果可以用隐状态h_state(即final_state[1])来计算,也可以用最后一个时刻的output输出来计算,结果是一样的。

完整的代码如下(复制代码到编译器上即可运行):

# View more python learning tutorial on my Youtube and Youku channel!!!

# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
# Youku video tutorial: http://i.youku.com/pythontutorial

"""
This code is a modified version of the code from this link:
https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/recurrent_network.py
His code is a very good one for RNN beginners. Feel free to check it out.
"""
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# set random seed for comparing the two result calculations
tf.set_random_seed(1)

# this is data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# hyperparameters
lr = 0.001
training_iters = 100000
# batch_size = 128
batch_size = 256

n_inputs = 28   # MNIST data input (img shape: 28*28)
n_steps = 28    # time steps
n_hidden_units = 128   # neurons in hidden layer
n_classes = 10      # MNIST classes (0-9 digits)

# tf Graph input
x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_classes])

# Define weights
weights = {   # 对权重进行随机初始化
    # (28, 128)
    'in': tf.Variable(tf.random_normal([n_inputs, n_hidden_units])),
    # (128, 10)
    'out': tf.Variable(tf.random_normal([n_hidden_units, n_classes]))
}
biases = {   # 对偏差进行随机初始化
    # (128, )
    'in': tf.Variable(tf.constant(0.1, shape=[n_hidden_units, ])),
    # (10, )
    'out': tf.Variable(tf.constant(0.1, shape=[n_classes, ]))
}

def RNN(X, weights, biases):
    # transpose the inputs shape from
    # X ==> (256 batch * 28 steps, 28 inputs)
    X = tf.reshape(X, [-1, n_inputs])

    # into hidden
    # X_in = (256 batch * 28 steps, 128 hidden)
    X_in = tf.matmul(X, weights['in']) + biases['in']   # 这里的维度是(256*28,128),输入先经过一个线性变化
    # X_in ==> (256 batch, 28 steps, 128 hidden)
    X_in = tf.reshape(X_in, [-1, n_steps, n_hidden_units])   # 这里的维度是(256,28,128)

    # cell
    cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units)  # 一个lstm单元的输入是向量,所以有这么多隐藏单元n_hidden_units
    # lstm cell is divided into two parts (c_state, h_state)
    init_state = cell.zero_state(batch_size, dtype=tf.float32)   # 初始状态的维度跟batch_size和隐藏单元个数有关,这里shape=(256, 128)

    outputs, final_state = tf.nn.dynamic_rnn(cell, X_in, initial_state=init_state, time_major=False)

    # hidden layer for output as the final results
    # results = tf.matmul(final_state[1], weights['out']) + biases['out']    # 可以这样用隐状态final_state[1]求results,也可以用下面的方法即最后时刻的输出outputs求results

    # # or
    # unpack to list [(batch, outputs)..] * steps
    # 这里的outputs是[batch_size, max_time, cell.output_size]的形式,现在要取最后一个时间的outputs,所以要调换一下max_time和batch_size,这样就可以直接用outputs[-1]来得到最后一个的输出
    outputs = tf.unstack(tf.transpose(outputs, [1,0,2]))  # tf.unstack默认是按行分解。
    results = tf.matmul(outputs[-1], weights['out']) + biases['out']    # shape = (128, 10)  # outputs[-1]是指最后一个时间的输出,outputs[-1]再经过一个线性变化得到结果

    return results

pred = RNN(x, weights, biases)  # 此时x还没有数值,后面用feed_dict输入
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))  # 此时y还没有数值,后面用feed_dict输入
train_op = tf.train.AdamOptimizer(lr).minimize(cost)

correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    step = 0
    while step * batch_size < training_iters:
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        batch_xs = batch_xs.reshape([batch_size, n_steps, n_inputs])  # 把[batch_size,784]变形为[batch_size,28,28],这样才符合x的维度
        sess.run(train_op, feed_dict={x: batch_xs,y: batch_ys,})
        if step % 20 == 0:
            batch_xs, batch_ys = mnist.test.next_batch(batch_size)   
            # 取batch_size大小的测试集,因为初始状态跟batch_size有关,所以这里取batch_size大小的测试集数据,否则会报错。也可以把batch_size作为一个参数来传递
            batch_xs = batch_xs.reshape([batch_size, n_steps, n_inputs])
            print("time:",step," accuracy:",sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys,}))
        step += 1

测试集准确率如下:

time: 0  accuracy: 0.23828125
time: 20  accuracy: 0.6796875
time: 40  accuracy: 0.74609375
time: 60  accuracy: 0.84375
time: 80  accuracy: 0.81640625
time: 100  accuracy: 0.875
time: 120  accuracy: 0.8828125
time: 140  accuracy: 0.91796875
time: 160  accuracy: 0.94140625
time: 180  accuracy: 0.89453125
time: 200  accuracy: 0.90625
time: 220  accuracy: 0.89453125
time: 240  accuracy: 0.96484375
time: 260  accuracy: 0.93359375
time: 280  accuracy: 0.953125
time: 300  accuracy: 0.953125
time: 320  accuracy: 0.94921875
time: 340  accuracy: 0.91796875
time: 360  accuracy: 0.93359375
time: 380  accuracy: 0.9453125
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值