700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【TensorFlow-windows】(一)实现Softmax Regression进行手写数字识别(mnist)

【TensorFlow-windows】(一)实现Softmax Regression进行手写数字识别(mnist)

时间:2021-10-09 12:37:08

相关推荐

【TensorFlow-windows】(一)实现Softmax Regression进行手写数字识别(mnist)

博文主要内容有:

1.softmax regression的TensorFlow实现代码(教科书级的代码注释)

2.该实现中的函数总结

平台:

1.windows 10 64位

2.Anaconda3-4.2.0-Windows-x86_64.exe (当时TF还不支持python3.6,又懒得在高版本的anaconda下配置多个python环境,于是装了一个3-4.2.0(默认装python3.5),建议装anaconda3的最新版本,TF1.2.0版本已经支持python3.6!)

3.TensorFlow1.1.0

先贴代码,函数

# -*- coding: utf-8 -*-"""Created on Mon Jun 12 16:36:43 @author: ASUS"""#import itchat#from PIL import Imageimport tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets('MNIST_data/', one_hot = True) # 是一个tensorflow内部的变量print(mnist.train.images.shape, mnist.train.labels.shape) # 训练集形状, 标签形状sess = tf.InteractiveSession() # sess被注册为默认的session x = tf.placeholder(tf.float32, [None, 784]) # Placeholder是输入数据的地方#-------------给weights和bias创建Variable对象-------------# Variable是用来存储模型参数,与存储数据的tensor不同,tensor一旦使用掉就消失W = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))# 计算 softmax 输出 y 其中x形状是[None, 784],None是为batch数而准备的y = tf.nn.softmax(tf.matmul(x, W) + b)#-------------交叉熵损失函数-------------# y_存放真实标签y_ = tf.placeholder(tf.float32, [None, 10])# recude_mean和reduce_sum意思缩减维度的均值,以及缩减维度的求和# reduce_mean在这里是对一个batch进行求均值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)#---------------全局参数初始化-------------------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})#---------------准确率验证-------------------# tf.argmax是寻找tensor中值最大的元素的序号 ,在此用来判断类别# tf.equal是判断两个变量是否相等, 返回的是bool值correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_, 1))# tf.cast用于数据类型转换accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))print(accuracy)print(accuracy.eval({x:mnist.test.images, y_: mnist.test.labels}))

其中用到的函数总结:

1. sess = tf.InteractiveSession() 将sess注册为默认的session

2. tf.placeholder() , Placeholder是输入数据的地方,也称为占位符,通俗的理解就是给输入数据(此例中的图片x)和真实标签(y_)提供一个入口,或者是存放地。(个人理解,可能不太正确,后期对TF有深入认识的话再回来改~~)

3. tf.Variable() Variable是用来存储模型参数,与存储数据的tensor不同,tensor一旦使用掉就消失

4. tf.matmul() 矩阵相乘函数

5. tf.reduce_mean 和tf.reduce_sum 是缩减维度的计算均值,以及缩减维度的求和

6. tf.argmax() 是寻找tensor中值最大的元素的序号 ,此例中用来判断类别

7. tf.cast() 用于数据类型转换

(ps: 可将每篇博文最后的函数总结复制到一个word文档,便于日后查找)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。