《TensorfFlow实战》读书笔记(三) —— Tensorflow 实现 Softmax Regression 识别手写数字

《TensorfFlow实战》读书笔记(三) —— Tensorflow 实现 Softmax Regression 识别手写数字

记录 概述,要点,代码,思维导图


书籍信息

书名:《Tensorflow实战》
作者:黄文坚 唐源
出版社出版:电子工业出版社
ISBN:978-7-121-30912-0


3.2 Tensorflow 实现 Softmax Regression 识别手写数字


这里写图片描述


概述

1. 机器学习 Hello World 任务, 28 x 28 像素, 0~9 共10类
2. 用 spyder 运行
3. 为了简化模型, 把 28 x 28 展开成 1 维 ( 28 x 28 = 784 )
4. 对 10 个种类进行 one-hot 编码, 数字 0 为 [1,0,0,0,0,0,0,0,0,0],数字 5 为 [0,0,0,0,0,1,0,0,0,0]
5. 使用 Softmax Regression 算法训练分类模型
6. 这里写图片描述


代码

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()  # 创建新的 InteractiveSession,之后的运算默认在这个 Session 跑
x = tf.placeholder(tf.float32, [None, 784]) # 输入数据的地方, 第一个参数为数据类型,第二个参数代表 tensor 的 shape( 尺寸 ),None 代表不限条数输入,784 为 784 个一维向量

W = tf.Variable(tf.zeros([784, 10])) # variable 用来储存模型参数,在迭代中持久化
b = tf.Variable(tf.zeros([10]))  # 初始化为 0,因为训练时会自动学习合适的值,10 代表 10 类

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

y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) # 定义好 loss, forward 和 backward 自动实现。 cross_entropy 作为 loss,tf.reduce_mean 求每个 batch 的均值

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) # 定义 随机梯度下降 为优化算法,学习率为 0.5,目标为 cross_entropy

tf.global_variables_initializer().run()  # 全局参数初始化并run

for i in range(1000): 
    batch_xs, batch_ys = mnist.train.next_batch(100) # 随机抽取 100 条样本构成mini-batch 并 feed 给 placeholder
    train_step.run({x: batch_xs, y_: batch_ys})
# 已完成训练,下面验证准确率
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # argmax 从 tensor 中寻找最大值的序号,tf.argmax(y,1) 就是预测数字中概率最大的那个,而 argmax(y_,1) 找样本的真实数字类别

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # tf.cast 将 correct_prediction 输出的 bool 值转换成 float32 并求平均

print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels})) #准确率为 92% 左右

步骤

1. 定义算法公式, 也就是神经网络 forward 时的计算。
2. 定义 loss,选定优化器,并指定优化器优化 loss。
3. 迭代地对数据进行训练。
4. 在测试集或验证集上对准确率进行评测。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值