import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data', one_hot = True) #
# add layer
#
def add_layer(inputs, in_size, out_size, activation_function = None): Weights = tf.Variable(tf.random_normal([in_size, out_size])) # hang lie
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b) return outputs def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs:v_xs})
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))#返回最大值的索引号
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs:v_xs, ys:v_ys})
return result #
# define placeholder for inputs to network
#
xs = tf.placeholder(tf.float32, [None, 784]) # 28x28, 784 dimention / sample
ys = tf.placeholder(tf.float32, [None, 10]) #
# add output layer
#
prediction = add_layer(xs, 784, 10, activation_function = tf.nn.softmax) #
# the error between prediction and real data
#
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
reduction_indices=[1])) #loss
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.Session()
sess.run(tf.global_variables_initializer()) for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs:batch_xs, ys:batch_ys})
if i % 50 == 0:
print(compute_accuracy(
mnist.test.images, mnist.test.labels))

  

解释 compute_accuracy 的计算原理:

来自:https://blog.csdn.net/cy_tec/article/details/52046806

最新文章

  1. AngularJs的UI组件ui-Bootstrap分享(十一)——Typeahead
  2. WPF入门教程系列六——布局介绍与Canvas(一)
  3. UIDatePicker的用法
  4. C语言 百炼成钢18
  5. 菜鸟学习Spring Web MVC之二
  6. (转)也谈基于NodeJS的全栈式开发(基于NodeJS的前后端分离)
  7. IOS开发-cell的动态高度
  8. jquery 入门之-------jquery 简介
  9. [terry笔记]Oracle数据泵-schema导入导出
  10. 素数筛法--SPOJ Problem 2 Prime Generator
  11. Spring框架:Spring容器具体解释
  12. Spring配置多数据源错误总结
  13. React Native 系列(五) -- 组件间传值
  14. dataTable插件的使用
  15. JdbcTemplate中queryForObject方法返回空结果或不正确结果数量的解决方法
  16. 另辟蹊径 直取通州的“墨迹天气”APP应用的成功故事
  17. shell 函数调用
  18. form表单的默认提交行为
  19. discuz回贴通知插件实现-插件的多语言
  20. SQL Server 中添加表注释

热门文章

  1. 解决Python开发中,Pycharm中无法使用中文输入法问题
  2. ThreadLocal 简单解析
  3. [分布式学习]消息队列之rocketmq笔记
  4. 物联网架构成长之路(43)-k8s从入门到放弃
  5. VMware exsi虚拟机磁盘扩容
  6. 扩展centos7.4虚拟机磁盘大小
  7. 【Zabbix】zabora批量部署
  8. 使用Charles进行HTTPS抓包及常见问题
  9. f(n-1) + f(n-2)的编译器处理
  10. 【java提高】---patchca生成验证码