博文主要内容有:

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 2017 @author: ASUS
"""
#import itchat
#from PIL import Image
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data mnist = 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文档,便于日后查找)

【TensorFlow-windows】(一)实现Softmax Regression进行手写数字识别(mnist)的更多相关文章

  1. TensorFlow 之 手写数字识别MNIST

    官方文档: MNIST For ML Beginners - https://www.tensorflow.org/get_started/mnist/beginners Deep MNIST for ...

  2. keras框架的MLP手写数字识别MNIST,梳理?

    keras框架的MLP手写数字识别MNIST 代码: # coding: utf-8 # In[1]: import numpy as np import pandas as pd from kera ...

  3. keras和tensorflow搭建DNN、CNN、RNN手写数字识别

    MNIST手写数字集 MNIST是一个由美国由美国邮政系统开发的手写数字识别数据集.手写内容是0~9,一共有60000个图片样本,我们可以到MNIST官网免费下载,总共4个.gz后缀的压缩文件,该文件 ...

  4. TensorFlow(十二):使用RNN实现手写数字识别

    上代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist ...

  5. 吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集

    import tensorflow as tf tf.reset_default_graph() # 配置神经网络的参数 INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE ...

  6. Tensorflow - Tutorial (7) : 利用 RNN/LSTM 进行手写数字识别

    1. 经常使用类 class tf.contrib.rnn.BasicLSTMCell BasicLSTMCell 是最简单的一个LSTM类.没有实现clipping,projection layer ...

  7. 吴裕雄 python 神经网络——TensorFlow实现AlexNet模型处理手写数字识别MNIST数据集

    import tensorflow as tf # 输入数据 from tensorflow.examples.tutorials.mnist import input_data mnist = in ...

  8. Tensorflow手写数字识别---MNIST

    MNIST数据集:包含数字0-9的灰度图, 图片size为28x28.训练样本:55000,测试样本:10000,验证集:5000

  9. Tensorflow笔记——神经网络图像识别(五)手写数字识别

随机推荐

  1. uva 11468 AC自动机+概率DP

    #include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...

  2. Linux System Programming 学习笔记(二) 文件I/O

    1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...

  3. JSON 序列化与弱类型

    一.C#中JSON序列化有多种方式: 使用“DataContractJsonSerializer ”类时需要, 1.引用程序集 System.Runtime.Serialization 和 Syste ...

  4. 转 整理 Linux服务器部署系列之一—Apache篇

    花了差不多一天,参考了几个博客,终于初步配成功了 Apache,先总结一下: 如果apache安装成为linux的服务的话,可以用以下命令操作: service httpd start 启动 serv ...

  5. bq25896 charging status CHRG_STAT register 0xB

    condition 1 :                    adapter 全部電流往 system去,                    battery current 也往 system ...

  6. Android 禁止状态栏下拉status bar

    如果你有这样的需求:用户进入你的app以后,所有的操作都是你的app中设定的,用户不可以拥有系统设置等行为的能力.然而,Android系统,可以通过从顶部往下拉,从而得到一个通知和快速系统设置的页面: ...

  7. Chtml的一些例子

    <!-- 如果是修改的话,调用已关联的单元 --> <?php foreach ($unionZones as $unit) { echo CHtml::openTag('div', ...

  8. AC日记——Andryusha and Socks Codeforces 780a

    A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. python学习之-- 生成唯一ID

    以下以2种方法生成唯一ID def uuid_method(): """第一种方法""" import uuid return str(uu ...

  10. Java 界面编程【03】事件监听

    当你把界面都设计好了,总需要添加相应的执行动作给组件,在JAVA中有相应的时间处理机制,叫做“监听器”,给组件添加相应执行动作的过程叫做“注册”,其中的“监听器”是一个接口,里面包含了相应的执行函数, ...