tensorflow1.2.0跑mnist例子

来源:互联网 发布:预算软件 编辑:程序博客网 时间:2024/06/07 11:35

参考了博客http://blog.csdn.net/hdmjdp/article/details/64548639,有些许不同,但是我这个采用spyder跑的,有时候能跑通,有时候会报错,先记录下来,有可能是spyder本身存在问题。

lstm_cell = rnn.BasicLSTMCell(n_hidden,state_is_tuple=True),state_is_tuple为True或者False都可以。

outputs, states = rnn.static_rnn(lstm_cell, _X, initial_state=_istate)改成

outputs, states = rnn.static_rnn(lstm_cell, _X, dtype = tf.float32)


# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)import tensorflow as tffrom tensorflow.contrib import rnnimport numpy as np'''MNIST的数据是一个28*28的图像,这里RNN测试,把他看成一行行的序列(28维度(28长的sequence)*28行)'''# RNN学习时使用的参数learning_rate = 0.001training_iters = 100000batch_size = 128display_step = 10# 神经网络的参数n_input = 28  # 输入层的nn_steps = 28  # 28长度n_hidden = 128  # 隐含层的特征数n_classes = 10  # 输出的数量,因为是分类问题,0~9个数字,这里一共有10个# 构建tensorflow的输入X的placeholderx = tf.placeholder("float", [None, n_steps, n_input])# tensorflow里的LSTM需要两倍于n_hidden的长度的状态,一个state和一个cell# Tensorflow LSTM cell requires 2x n_hidden length (state & cell)istate = tf.placeholder("float", [None, 2 * n_hidden])# 输出Yy = tf.placeholder("float", [None, n_classes])                     # 随机初始化每一层的权值和偏置weights = {    'hidden': tf.Variable(tf.random_normal([n_input, n_hidden])),  # Hidden layer weights    'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))}biases = {    'hidden': tf.Variable(tf.random_normal([n_hidden])),    'out': tf.Variable(tf.random_normal([n_classes]))}#构建RNNdef RNN(_X, _istate, _weights, _biases):     # 规整输入的数据    _X = tf.transpose(_X, [1, 0, 2])  # permute n_steps and batch_size    _X = tf.reshape(_X, [-1, n_input])  # (n_steps*batch_size, n_input)    # 输入层到隐含层,第一次是直接运算    _X = tf.matmul(_X, _weights['hidden']) + _biases['hidden']    # 之后使用LSTM    #lstm_cell = rnn_cell.LayerNormBasicLSTMCell(n_hidden, forget_bias=1.0)    lstm_cell = rnn.BasicLSTMCell(n_hidden,state_is_tuple=True)    # 28长度的sequence,所以是需要分解位28次    _X = tf.split(_X, n_steps, 0)  # n_steps * (batch_size, n_hidden)    #x = tf.split(x, n_steps, 0) # tf.split(value, num_or_size_splits, axis) versions > 0.12.0    # 开始跑RNN那部分    outputs, states = rnn.static_rnn(lstm_cell, _X, dtype = tf.float32)    #rnn.rnn.dynamic_rnn()    return tf.matmul(outputs[-1],_weights['out']) + biases['out']pred = RNN(x, istate, weights, biases)                                   # 定义损失和优化方法,其中算是为softmax交叉熵,优化方法为Adam  cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))  # Softmax loss  optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)  # Adam Optimizer    # 进行模型的评估,argmax是取出取值最大的那一个的标签作为输出  correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))  accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))    # 初始化  init = tf.global_variables_initializer()# 开始运行with tf.Session() as sess:    sess.run(init)    step = 1    # 持续迭代    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_input))        # 迭代        sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys,                                       istate: np.zeros((batch_size, 2 * n_hidden))})        # 在特定的迭代回合进行数据的输出        if step % display_step == 0:            # Calculate batch accuracy            acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys,                                                istate: np.zeros((batch_size, 2 * n_hidden))})            # Calculate batch loss            loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys,                                             istate: np.zeros((batch_size, 2 * n_hidden))})            print ("Iter " + str(step * batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + \                  ", Training Accuracy= " + "{:.5f}".format(acc))        step += 1    print ("Optimization Finished!")    # 载入测试集进行测试    test_len = 256    test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))    test_label = mnist.test.labels[:test_len]    print ("Testing Accuracy:", sess.run(accuracy, feed_dict={x: test_data, y: test_label,                                                             istate: np.zeros((test_len, 2 * n_hidden))}))