Tensorflow的hello world!——Softmax Regression 识别手 4000 写数字
2018-03-13 18:53
344 查看
from tensorflow.examples.tutorials.mnist import input_data mnist=input_data.read_data_sets("MNIST_data/",one_hot=True) print(mnist.train.images.shape,mnist.train.labels.shape) print(mnist.test.images.shape,mnist.test.labels.shape) print(mnist.validation.images.shape, mnist.validation.labels.shape) import tensorflow as tf #载入tensorflow模块 sess=tf.InteractiveSession() #创建一个新的session 使用这个命令还会使这个session注册为默认的 x=tf.placeholder(tf.float32,[None,784]) #创建一个placeholder 输入数据的地方 数据类型:float 数据维度:[不限条数,784维] W=tf.Variable(tf.zeros([784,10])) b=tf.Variable(tf.zeros([10])) y=tf.nn.softmax(tf.matmul(x,W)+b) y_=tf.placeholder(tf.float32,[None,10]) #创建一个placeholder 输入是真实的label #loss function cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1])) train_step=tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) #选择优化算法 随机梯度下降 SGD tf.global_variables_initializer().run() for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) train_step.run({x:batch_xs,y_:batch_ys}) correc_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) accuracy=tf.reduce_mean(tf.cast(correc_prediction,tf.float32)) print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))Softmax Regression
当我们处理多分类任务时,通常需要使用Softmax Regression模型,即使在卷积神经网络或者循环神经网络中,如果是分类模型,最后一层也同样是Softmax Regression。它的工作原理很简单,将可以判定为某类的特征相加,然后将这些特征转换为判定是这一类的概率。
SGD(Stochastic Gradient Decent)
普通的梯度下降函数的缺点:
1)机器学习应用中面对的数据集很大,普通梯度函数需要精确的计算每一步的导数,效率很低。
2) 如果不小心陷入鞍点,或者比较差的局部最优解(注意这里是比较差的局部最优解,我们通常也不追求全局最优,神经网络可能有多个局部最优解来达到比较好的分类效果,而全局最优解反而更容易过拟合),GD没办法跑出来。
SGD恰好弥补了这两个缺点:因为SGD对导数的要求特别低,可以包含大量噪声,举例子说比如神经网络中,训练时只需要从几百万个点中随机拿出128或者256个数据点,算一个不那么精准的导数,就可以往导数对应梯度方向走一步,如图:
虽然SGD需要的步子更多,但是需要的计算量缺少很多。详见:SGD
相关文章推荐
- Tensorflow实战——利用Softmax Regression识别手写数字
- tensorflow实战 Softmax Regression识别手写体数字
- Tensorflow实现MNIST手写数字识别(Softmax Regression)
- TensorFlow实现Softmax Regression识别手写数字中"TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败”问题
- Tensorflow实战2:Softmax Regression识别手写数字
- Tensorflow实现Softmax Regression识别手写数字
- TensorFlow 实现Softmax Regression识别手写数字
- tensorflow实战(一)TensorFlow实现 softmax Regression 识别手写数字
- TensorFlow实战 3.2实现softmax regression识别手写数字
- tensorflow 第一个程序MNIST手写数字识别(Softmax Regression实现)
- TensorFlow笔记(一)---Softmax Regression识别手写数字
- TensorFlow(二)实现Softmax Regression 识别手写数字
- Tensorflow实战之用softmax Regression识别手写数字
- TensorFlow 实现Softmax Regression 识别数字
- TensorFlow实现Softmax Regression识别手写数字
- tensorflow实现softmax regression识别手写数字
- tensorflow实战第三章——用Softmax Regression识别手写数字
- 学习笔记TF024:TensorFlow实现Softmax Regression(回归)识别手写数字
- 用TensorFlow的Softmax Regression进行手写数字识别
- tensorflow用Softmax Regression识别MNIST手写数字识别