TensorFlow学习笔记(一)-- Softmax回归模型识别MNIST

最近学习Tensorflow,特此笔记,学习资料为21个项目玩转深度学习 基于TensorFlow的实践详解
Softmax回归是一个线性的多分类模型,它是从Logistic回归模型转化而来的,不同的是Logistic回归模型是一个二分类模型,而Softmax回归模型是一个多分类模型

Softmax函数将模型对于各个类别的预测值转化为概率,假设模型在三个类别上的预测值分别为(a, b, c),则运用Softmax函数之后的概率值分别为(e^a/(e^a+e^b+e^c), e^b/(e^a+e^b+e^c), e^c/(e^a+e^b+e^c))

上代码:

#coding=utf-8
#导入tensorflow
import tensorflow as tf
#导入MNIST教学模块
from tensorflow.examples.tutorials.mnist import input_data
#读入MNIST数据
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

# 创建x,x是一个占位符(placeholder),代表待识别的图片
x = tf.placeholder(tf.float32, [None,784])

# W是softmax模型的参数,将一个784的输入转为10维的输出
W = tf.Variable(tf.zeros([784, 10]))
# b是另一个参数,一般叫做偏置(bias)
b = tf.Variable(tf.zeros([10]))

#y表示模型的输出
y = tf.nn.softmax(tf.matmul(x, W) + b)

#y_是实际的图像标签,同样以占位符表示
# y_ = tf.placeholder(tf.float32, [None, 10])
y_ = tf.placeholder(tf.float32, [None,10])
#现在,我们得到了两个重要的Tensor:y and y_
#y是模型的输出,y_是图像是实际标签,y_是独热表示的

# 根据y和y_构造交叉熵损失
cross_entropy = \
    tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y)))

#定义损失之后,就可以用梯度下降法对模型的参数(W and b)进行优化
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

#创建一个session 只有在session中才能运行优化步骤train_step
sess = tf.InteractiveSession()
#运行之前必须要初始化所有变量,分配内存
tf.global_variables_initializer().run()

#进行1000步梯度下降
for _ in range(1000):
    # 在mnist.train中取100个数据
    # batch_xs是形状为(100, 784)的图像数据,batch_ys是形如(100,10)的实际标签
    # batch_xs, batch_ys对应着两个占位符x and y
    batch_xs, batch_ys = mnist.train.next_batch(100)
    # 在session中运行train_step,运行时要传入占位符的值
    sess.run(train_step, feed_dict={x:batch_xs, y_:batch_ys})

# 正确的预测结果
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
# 计算预测准确率,它们都是tensor
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 在Session中运行Tensor可以得到Tensor的值
# 这里获取最终模型的准确率
print(sess.run(accuracy, feed_dict={x:mnist.test.images, y_:mnist.test.labels }))

运行环境:Windows10 64bit, Python3.6.4 64bit, TensorFlow1.7.0 cpu version

解释几个函数:


tf.argmax(input, axis=None, name=None, dimension=None)
此函数是对矩阵按行或列计算最大值

参数
input:输入Tensor
axis:0表示按列,1表示按行
name:名称
dimension:和axis功能一样,默认axis取值优先。新加的字段
返回:Tensor 一般是行或列的最大值下标向量


tf.cast(x, dtype, name=None)
x: 输入Tensor
dtype: 要转化的类型
name: 名称
返回:转化成dtype类型的Tensor


tf.reduce_mean(input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None, keep_dims=None)
功能:求均值
input_tensor:输入Tensor
axis:0表示按列,1表示按行


tf.equal(x, y)
判断两个输入Tensor x, y 是否相等,返回值为相同维度的布尔值Tensor
关于TensorFlow的占位符,变量以及会话,参考博客Tensorflow入门—-占位符、常量和Session

猜你喜欢

转载自blog.csdn.net/no_superintendent/article/details/80034502