tensorflow下对MNIST数据集进行识别的程序代码
2017-05-06 14:02
501 查看
# 下载mnist数据集: from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 引入tensorflow: import tensorflow as tf x = tf.placeholder(tf.float32, [None, 784]) # None意味着可以是任意长度的维度 W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) # 实现模型 y = tf.nn.softmax(tf.matmul(x, W) + b) # 定义cost/loss,用cross_entropy y_ = tf.placeholder(tf.float32, [None, 10]) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) # 注:tensorflow源码中用的是tf.nn.softmax_cross_entropy_with_logits,目的是保持数值稳定 # 开始训练 train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) # 启动模型,用InteractiveSession sess = tf.InteractiveSession() # 初始化变量 tf.global_variables_initializer().run() # 运行 train_step 1000次 for _ in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) # 注:feed进去的数据,代替placeholder # 评价模型 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
运行结果:
0.9172
相关文章推荐
- 用卷积神经网络对mnist进行数字识别程序(tensorflow)
- 使用tensorflow对Mnist数据集进行字体识别
- tensorflow 学习专栏(五):在mnist数据集上使用tensorflow实现临近算法(Nearest-Neighbor)进行手写数字识别
- 利用tensorflow一步一步实现基于MNIST 数据集进行手写数字识别的神经网络,逻辑回归
- Deep Learning-TensorFlow (1) CNN卷积神经网络_MNIST手写数字识别代码实现详解
- 一般knn算法识别mnist数据集(代码)
- tensorflow 学习专栏(四):使用tensorflow在mnist数据集上使用逻辑回归logistic Regression进行分类
- 使用Tensorflow实现CNN进行MNIST数字识别
- 使用PCA + KNN对MNIST数据集进行手写数字识别 python
- Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_训练模型
- tensorflow进行MNIST手写数字识别-优化版
- Tensorflow 使用CNN 进行mnist 训练代码示例
- Tensorflow-MNIST数字识别练习代码
- 深度学习笔记——TensorFlow学习笔记(三)使用TensorFlow实现的神经网络进行MNIST手写体数字识别
- Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_利用训练好的模型进行分类
- TensorFlow学习笔记(1):使用softmax对手写体数字(MNIST数据集)进行识别
- 在tensorflow上进行机器学习的“Hello World”:MNIST 手写识别
- 【TensorFlow-windows】(三) 多层感知器进行手写数字识别(mnist)
- 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)
- 使用TensorFlow训练神经网络识别MNIST数据代码