TensorFlow学习01-SoftmaxRegression识别手写数字

SoftmaxRegression识别手写数字

整个神经网络的流程:

定义算法公式,也就是神经网络的forward时的计算

定义loss,选定优化器,并指定优化器优化loss

迭代地对数据进行训练

在测试集或验证集上对准确率进行评测

Logistic回归的回顾(二分类)

在Logistic回归中比较重要的有两个公式,一个是阶跃函数:

阶跃函数

另一个是对应的损失函数

损失函数

最终,Logistic回归需要求出的是2个概率:

Logistic回归求出的2个概率

Softmax Regression 求k个概率(多分类):机器学习中把某类的特征,然后将特征转化为判定某一类的概率。

tf.InteractiveSession()会将这个Session注册为默认的session。

placeholder()输入数据的地方,第一个参数是数据类型,第二个是shape数据的尺寸[None, 784],None代表不限条数的输入,784维的向量

为w,b创建Variable对象,在模型迭代训练中是持久化的,而tensor一旦被用掉就会消失。

tensorflow最好的地方不是定义公式,而是将forward和backward的内容自动实现,只要接下来定义好loss,训练的时候会自动求导并进行梯度下降,完成对Softmax Regression模型参数的自动学习。

需要定义一个loss function来描述模型对问题的分类精度。Loss越小,与真实偏差越小,训练的目的就是减小loss。

对于多分类,通常使用cross-entropy(交叉熵)最为loss function。

cross-entropy(交叉熵)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

tf.reduce_sum()求和,tf.reduce_mean()求平均值

每个被定义的公式只是个节点,只有调用run()方法的时候才会启动

代码:

mnistSoftmaxRegression.py

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

# one_hot是10维的向量

mnist = input_data.read_data_sets("MNIST-data/", one_hot=True)

# 查看一下数据集的情况

# print(mnist.train.image.shape, mnist.train.labels.shape)

# print(mnist.test.image.shape, mnist.test.labels.shape)

# print(mnist.validation.image.shape, mnist.validation.labels.shape)

sess = tf.InteractiveSession()

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

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

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

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

# 定义cross-entropy

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

# 用一个placeholder 输入真实的label,y_ * tf.log(y)就是计算yi*log(yi)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

# 然后定义一个随机梯度下降算法SGD(Stochastic Dradient Desent)

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

# 全局参数初始化

tf.global_variables_initializer().run()

for i in range(10000):

batch_xs, batch_ys = mnist.train.next_batch(100)

train_step.run({x: batch_xs, y_:batch_ys})

if i % 1000 == 0:

print(i)

# 对模型的准确率进行验证tf.argmax(y, 1)预测的最大值,和样本的真实数字比较

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

# 用tf.cast将correct_prediction输出的bool值转换为float32

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

#

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

你可能感兴趣的:(TensorFlow学习01-SoftmaxRegression识别手写数字)