zoukankan      html  css  js  c++  java
  • 【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 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文档,便于日后查找)

  • 相关阅读:
    fileupload直接获得
    ajax分页
    jquery.cookie.js
    DataTable分页
    C#字串与Unicode互相转换方法
    Linq测试
    滚动条加载数据
    创建.PDF文件【1】
    小问题【6】
    小问题【4】
  • 原文地址:https://www.cnblogs.com/TensorSense/p/7413318.html
Copyright © 2011-2022 走看看