Tensorflow-RNN循环网络-手写数字识别(MNIST数据集)
2020-01-14 18:44
477 查看
#RNN卷积神经网络-手写数字识别--MNIST数据集 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据 mnist = input_data.read_data_sets('C:/Users/lenovo/Desktop/TF/MNIST_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.nn.rnn_cell.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) #传入inputs个是固定 #outputs, final_state维度 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) #结果存放在一个bool类型列表中 correct_prediction = tf.equal(tf.arg_max(prediction,1), tf.arg_max(y, 1)) #argmax返回一维张量中最大的值所在的位置 #求准确率 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) with tf.Session() as sess: tf.global_variables_initializer().run() for epoch in range(21): 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}) acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels}) print('Iter'+ str(epoch)+',Testing Accuracy=' + str(acc))
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 深度学习与TensorFlow实战(六)全连接网络基础—MNIST数据集输出手写数字识别准确率
- 利用tensorflow一步一步实现基于MNIST 数据集进行手写数字识别的神经网络,逻辑回归
- tensorflow 学习笔记12 循环神经网络RNN LSTM结构实现MNIST手写识别
- 使用tensorflow利用神经网络分类识别MNIST手写数字数据集,转自随心1993
- tensorflow训练mnist数据集-识别手写数字
- tensorflow 学习专栏(五):在mnist数据集上使用tensorflow实现临近算法(Nearest-Neighbor)进行手写数字识别
- 神经网络——实现MNIST数据集的手写数字识别
- 手写汉字数字识别详细过程(构建数据集+CNN神经网络+Tensorflow)
- 深度学习-传统神经网络使用TensorFlow框架实现MNIST手写数字识别
- tensorflow——用RNN实现MNIST手写数字识别
- Tensorflow——MNIST手写数字数据集识别分类,准确率达到98%以上的方法实验
- tensorflow构建RNN识别mnist手写数字
- 基于TensorFlow1.4.0的FNN全连接网络识别MNIST手写数据集
- tensorflow1.1/循环神经网络手写数字啊识别
- TensorFlow实现机器学习的“Hello World”--Mnist手写数字识别
- TensorFlow学习笔记(3)--实现Softmax逻辑回归识别手写数字(MNIST数据集)
- 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)
- 基于tensorflow的MNIST手写数字识别--入门篇
- tensorflow使用RNN分析mnist手写体数字数据集
- 对抗神经网络学习(一)——GAN实现mnist手写数字生成(tensorflow实现)