• Tensorflow递归神经网络学习练习


    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data

    #载入数据集
    mnist = input_data.read_data_sets("F:TensorflowProjectMNIST_data",one_hot=True)

    # 输入图片是28*28
    n_inputs = 28 #输入一行,一行有28个数据
    max_time = 28 #一共28行
    lstm_size = 100 #隐层单元
    n_classes = 10 # 10个分类
    batch_size = 50 #每批次50个样本
    n_batch = mnist.train.num_examples // batch_size #计算一共有多少个批次

    #这里的none表示第一个维度可以是任意的长度
    x = tf.placeholder(tf.float32,[None,784])
    #正确的标签
    y = tf.placeholder(tf.float32,[None,10])

    #初始化权值
    weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))
    #初始化偏置值
    biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))

    #定义RNN网络
    def RNN(X,weights,biases):
    # inputs=[batch_size, max_time, n_inputs]
    inputs = tf.reshape(X,[-1,max_time,n_inputs])
    #定义LSTM基本CELL
    #lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size)
    lstm_cell = rnn.BasicLSTMCell(lstm_size)
    # final_state[0]是cell state
    # final_state[1]是hidden_state
    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


    #计算RNN的返回结果
    prediction= RNN(x, weights, biases)
    #损失函数
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
    #使用AdamOptimizer进行优化
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    #结果存放在一个布尔型列表中
    correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
    #求准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#把correct_prediction变为float32类型
    #初始化
    init = tf.global_variables_initializer()

    with tf.Session() as sess:
      sess.run(init)
      for epoch in range(6):
        for batch in range(n_batch):
          batch_xs,batch_ys = mnist.train.next_batch(batch_size)
          sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print ("Iter " + str(epoch) + ", Testing Accuracy= " + str(test_acc ))

    #############运行结果

    Extracting F:TensorflowProjectMNIST_data	rain-images-idx3-ubyte.gz
    Extracting F:TensorflowProjectMNIST_data	rain-labels-idx1-ubyte.gz
    Extracting F:TensorflowProjectMNIST_data	10k-images-idx3-ubyte.gz
    Extracting F:TensorflowProjectMNIST_data	10k-labels-idx1-ubyte.gz
    WARNING:tensorflow:From <ipython-input-5-efe314a2932e>:40: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
    Instructions for updating:
    
    Future major versions of TensorFlow will allow gradients to flow
    into the labels input on backprop by default.
    
    See tf.nn.softmax_cross_entropy_with_logits_v2.
    
    Iter 0, Testing Accuracy= 0.7372
    Iter 1, Testing Accuracy= 0.8334
    Iter 2, Testing Accuracy= 0.908
    Iter 3, Testing Accuracy= 0.9182
    Iter 4, Testing Accuracy= 0.9172
    Iter 5, Testing Accuracy= 0.9266


    ##############第二次运行
    Iter  0  ,Testing Accuracy= 0.744
    Iter  1  ,Testing Accuracy= 0.8028
    Iter  2  ,Testing Accuracy= 0.8803
    Iter  3  ,Testing Accuracy= 0.9115
    Iter  4  ,Testing Accuracy= 0.9262
    Iter  5  ,Testing Accuracy= 0.9303
  • 相关阅读:
    【python】变量定义及全局局部变量
    【python】重要的内置函数
    【python】迭代器iterator
    Java序列化与反序列化
    java中的IO操作总结
    Java中List Set Map 是否有序等总结
    java.lang.Class.getDeclaredMethod()方法详解
    一个servlet处理多个请求(使用Method的反射机制)
    java类的访问权限
    java中的基本数据类型存放位置
  • 原文地址:https://www.cnblogs.com/herd/p/9478039.html
Copyright © 2020-2023  润新知