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