学习TensorFlow,在MNIST数据集上建立softmax回归模型并测试

一、代码

<span style="font-size:18px;">from tensorflow.examples.tutorials.mnist import input_data
mnist =input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow astf
sess =tf.InteractiveSession()
x =tf.placeholder(tf.float32, shape=[None, 784])
y_ =tf.placeholder(tf.float32, shape=[None, 10])
W =tf.Variable(tf.zeros([784,10]))
b =tf.Variable(tf.zeros([10]))
sess.run(tf.initialize_all_variables())
y =tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy =-tf.reduce_sum(y_*tf.log(y))
train_step =tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
for i in range(1000):
  batch = mnist.train.next_batch(50)
  train_step.run(feed_dict={x: batch[0], y_:batch[1]})
correct_prediction =tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy =tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval(feed_dict={x:mnist.test.images, y_: mnist.test.labels}))</span>

二、运行结果

三、代码解析

import tensorflow as tf

sess =tf.InteractiveSession()

InteractiveSession()可以一边构建计算图,一边执行,而Session()需要把计算图全部构建完成才能执行

x =tf.placeholder(tf.float32, shape=[None, 784])

y_ =tf.placeholder(tf.float32, shape=[None, 10])

创建图像输入节点和目标输出节点

W = tf.Variable(tf.zeros([784,10]))

b = tf.Variable(tf.zeros([10]))

构建softmax回归模型的参数:权重和偏置

sess.run(tf.initialize_all_variables())

初始化所有的variables</span>

y = tf.nn.softmax(tf.matmul(x,W)+ b)

cross_entropy = -tf.reduce_sum(y_*tf.log(y))

预测输出,使用交叉熵作为损失函数

train_step =tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

步长为0.01,使用梯度下降法训练模型

for i in range(1000):

batch = mnist.train.next_batch(50)

train_step.run(feed_dict={x:batch[0], y_: batch[1]})

训练周期为1000,每个周期batch是50幅图像

correct_prediction =tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

比较每个预测结果和真实结果,返回一个二值向量

accuracy =tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

将二值向量转换为浮点向量,并计算正确率

print(accuracy.eval(feed_dict={x:mnist.test.images, y_: mnist.test.labels}))

打印输出训练模型对数据集的测试结果,feed_dict指定输入图像数据和目标输出结果

参考资料:https://www.tensorflow.org/versions/r0.7/tutorials/mnist/pros/index.html

最新文章

  1. mac-改造你的terminal
  2. [原]Water Water Search Problems&amp;#39; Set~Orz【updating...】
  3. Why Does Everyone Else Appear to Be Succeeding?
  4. 浅析ASP.NET的状态保持
  5. 行列的几种命名方式.line-.colume======.row-.col=========.tr-.td
  6. 工具:BT Sync 同步文件
  7. struts2的注解配置全面解析
  8. 从一个非开发人员转行silverlight满一年的工作总结(第一次发帖)
  9. java工厂类与反射机制
  10. Allegro PCB -通孔焊盘制作 及Flash制作
  11. 微信小程序demo
  12. 炸金花的JS实现从0开始之 -------现在什么都不会(1)
  13. Android Studio 插件开发详解一:入门练手
  14. STL--sort源码分析
  15. day_44_Django
  16. 【Python】安装error: Microsoft Visual C++ 14.0 is required. Get it with &quot;Microsoft Visual C++ Build Tools&quot;
  17. 2019-04-03-day025-异常与日志
  18. 【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas
  19. SOA 解惑
  20. 有强大的cURL,忘掉httpclient的吧!

热门文章

  1. SpringCloud学习之sleuth&amp;zipkin【二】
  2. SQL优化实用方法
  3. .net带参数SQL语句的完整定义
  4. GNS3 1.4.0b3 MSTP多生成树配置实验
  5. jupyter notebook 更换主题的方法
  6. Apache 443端口占用解决方法
  7. c++DLL编程详解
  8. 使用RedisDesktopManager工具,解决连接失败问题
  9. ACM 人见人爱A^B
  10. Node.js HTTP