softmax分类

import tensorflow as tf
import numpy as npfrom input_data import read_data_sets mnist = read_data_sets('MNIST_data', one_hot=True) def add_layer(inputs, in_size, out_size, active_function=None):
"""
:param inputs:
:param in_size: 行
:param out_size: 列 , [行, 列] =矩阵
:param active_function:
:return:
"""
with tf.name_scope('layer'):
with tf.name_scope('weights'):
W = tf.Variable(tf.random_normal([in_size, out_size]), name='W') #
with tf.name_scope('bias'):
b = tf.Variable(tf.zeros([1, out_size]) + 0.1) # b是代表每一行数据,对应out_size列个数据
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.matmul(inputs, W) + b
if active_function is None:
outputs = Wx_plus_b
else:
outputs = active_function(Wx_plus_b)
return outputs def compute_accuracy(v_xs, v_ys):
""" 计算的准确率 """
global prediction # prediction value
y_pre = sess.run(prediction, feed_dict={xs: v_xs})
# 与期望的值比较 bool
correct_pre = tf.equal(tf.argmax(y_pre, 1), tf.argmax(ys, 1))
# 将bools转化为数字
accuracy = tf.reduce_mean(tf.cast(correct_pre, 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])
ys = tf.placeholder(tf.float32, [None, 10]) # softmax + cross_entropy = classification
# add output layer
prediction = add_layer(xs, 784, 10, active_function=tf.nn.softmax) # softmax分类 # the loss between prediction and really
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
reduction_indices=[1])) # training
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.Session()
sess.run(tf.initialize_all_variables()) # start training
for i in range(1000):
batch_x, batch_y = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_x, ys: batch_y})
if i % 50 == 0:
print(compute_accuracy(mnist.test.images, mnist.test.labels))

最新文章

  1. ~是什么意思 在C语言中,~0代表什么
  2. cookieContainer应用
  3. Spring 事务管理高级应用难点剖析--转
  4. 注意java的对象引用
  5. js变量声明与赋值以及函数声明
  6. kinect for windows - SkeletonBasics-D2D详解之一
  7. eclipse中以debug方式启动tomcat报错
  8. charAt()的功能
  9. 转:客户端session与服务端session
  10. 笔记7 AOP练习<有疑问>
  11. UITableView section 圆角 阴影
  12. MySQL 优化小技巧
  13. svn 的truck、tag、 merge
  14. MySQL设置空密码
  15. 初识mysql数据库
  16. skynet 报错 skynet 服务缺陷 Lua死循环
  17. 读asyncio模块源码时的知识补漏
  18. 原生JS和jQuery版实现文件上传功能
  19. map模块使用方法
  20. hdu 2412 Party at Hali-Bula 经典树形DP

热门文章

  1. HTML结构组成
  2. CF1119 Global Round 2
  3. SUST OJ 1675: Fehead的项目(单调栈)
  4. 【转】Python xlrd、xlwt、xlutils读取、修改Excel文件
  5. Django 数据库操作进阶F和Q操作
  6. Go随机数的使用
  7. Git密钥生成步骤SSH Key
  8. 【jmeter】jMeter使用Badboy录制Web测试脚本
  9. 【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明
  10. FQ:从入门到放弃(二)