zoukankan      html  css  js  c++  java
  • Tensorflow实战(1)

    一、环境配置:

      我并没有按照书上的安装方法去安装Tensorflow,选择了一种偷懒的方式,因为先安装了Anaconda,所以直接:conda install tensorflow。因为台式机是集成显卡,没有安装NVDIA的显卡,所以没有使用GPU版本的。目前看起来没什么问题,但是肯定还是要重装的。

    二、简单的例子:

      书中将Tensorflow实现Softmax Regression识别手写数字比作编程中的"Hello World",可见其重要性,所以还是认认真真的写了一遍。

      使用的数据库是MNIST的数据库。可以直接在官网中下到,下载完成后不需要解压。也可以使用Python代码进行下载。

    1 from tensorflow.examples.tutorials.mnist import input_data
    2 mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
    View Code

      这样就可以把数据集放在代码存放位置的 MNIST_data 目录下,里面就是四个数据集,和在官网直接下载是一样的。

      之后就开是用到Tensorflow了。

      数据集的每一张是28*28像素大小的灰度图片,所以有一个784的一维向量,所有的数据集在一起就是一个55000*784的Tensor,然后将label也进行编码,得到 5500 * 10 的labels的Tensor。

      处理好数据集之后接着就是对于算法的设计了,这里采用的是:Softmax Regression算法。好吧,百度找了一圈,都是关于公式的证明什么的,无非就是想说明,将分类问题转换为概率问题,取最大概率,从而判断其分类, 之后就是一堆公式的转换了。数学公式比较难写下来,我要在准备一下,所以先上代码了。

    # -*- coding: utf-8 -*-
    """
    Created on Thu Jan 11 14:03:39 2018
    
    @author: TulLing
    """
    
    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
    
    sess =tf.InteractiveSession()
    x = tf.placeholder(tf.float32,[None,784])
    W = tf.Variable(tf.zeros([784,10]))
    b = tf.Variable(tf.zeros([10]))
    
    # =============================================================================
    # print(x)
    # print(w)
    # print(b)
    # =============================================================================
    # Y
    y = tf.nn.softmax(tf.matmul(x,W) + b)
    # print(y)
    #Y的导数
    y_ = tf.placeholder(tf.float32,[None,10])
    #损失函数的定义
    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,bathc_ys = mnist.train.next_batch(100)
        train_step.run({x:batch_xs,y_: bathc_ys})
    correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))
  • 相关阅读:
    RN-Android构建失败:Caused by: org.gradle.api.ProjectConfigurationException: A problem occurred configuring root project 'AwesomeProject'.
    Android更新包下载成功后不出现安装界面
    真机调试: The application could not be installed: INSTALL_FAILED_TEST_ONLY
    react native 屏幕尺寸转换
    Android Studio生成签名文件,自动签名,以及获取SHA1和MD5值
    React Native安卓真机调试
    git提交代码报错Permission denied, please try again
    The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.
    命令行设置快捷命令
    Linux 常用指令
  • 原文地址:https://www.cnblogs.com/hixkill/p/8270764.html
Copyright © 2011-2022 走看看