• Tensorflow学习(四)——递归神经网络RNN


    实现一个简单的RNN(代码如下)

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    from tensorflow.contrib import rnn
    
    
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
    batch_size = 50
    num_batch = mnist.train.num_examples // batch_size
    # 输入(图片尺寸28*28)
    num_input = 28      # 一行28个数据
    max_time = 28       # 行数量
    LSTM_size = 100     # 隐藏单元数量(block数量)
    num_classes = 10    # 10个分类(对应输出)
    
    x = tf.placeholder(tf.float32, [None, 784])
    y = tf.placeholder(tf.float32, [None, 10])
    weights = tf.Variable(tf.truncated_normal([LSTM_size, num_classes], stddev=0.1))
    biases = tf.Variable(tf.constant(0.1, shape=[num_classes]))
    
    
    def RNN(X):
        # inputs格式固定:[batch_size, max_time, num_input]
        inputs = tf.reshape(X, [-1, max_time, num_input])
        # 定义LSTM基本的cell
        LSTM_cell = rnn.BasicLSTMCell(LSTM_size)
        outputs, final_state = tf.nn.dynamic_rnn(LSTM_cell, inputs, dtype=tf.float32)
        results = tf.nn.softmax(tf.matmul(final_state[1], weights) + biases)
        return results
    
    
    prediction = RNN(x)
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
    train = tf.train.AdamOptimizer(0.0001).minimize(loss)
    correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(6):
            for batch in range(num_batch):
                batch_xs, batch_ys = mnist.train.next_batch(batch_size)
                sess.run(train, feed_dict={x: batch_xs, y: batch_ys})
            acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
            print('iter' + str(epoch) + ', testing accuracy:' + str(acc))
  • 相关阅读:
    js转化 保留2位小数
    python练习:打印九九乘法表
    PyCharm常用快捷键及工具
    python关键字
    Python学习资源
    Jira项目导入,被导入项目与目的系统数据类型不一致导入不成功的解决方案
    压测的时候到底要不要加集合点?
    Java Vuser协议JDBC脚本编写(MySQL)
    eclipse工具使用
    oracle忘记sys,system密码的解决方法
  • 原文地址:https://www.cnblogs.com/horacle/p/13167756.html
Copyright © 2020-2023  润新知