zoukankan      html  css  js  c++  java
  • Hello TensorFlow

    昨天把电脑充电器落公司了 没办法今天只能来公司看书了

    买spark编程基础顺便买了本TensorFlow实战= =

    昨天把作业写完了 今天就看看这本书吧2333

    昨晚走之前把东西都装差不多了

    今天来pip一下就直接用了

    机器瞎学

     1 from tensorflow.examples.tutorials.mnist import input_data
     2 import tensorflow as tf
     3 
     4 # 下载数据集
     5 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
     6 
     7 
     8 # 注册 session
     9 sess = tf.InteractiveSession()
    10 
    11 # 创建数据集Placeholder
    12 # None表示不限条数 784表示输入是784维的向量
    13 x = tf.placeholder(tf.float32, [None, 784])
    14 
    15 
    16 # 下面是公式 P49
    17 # w是输入数据
    18 # 784代表特征的维数
    19 # 10代表结果 因为是识别手写数字 所以一共10维代表0-9
    20 # b代表bias 数据本身的偏向
    21 w = tf.Variable(tf.zeros([784, 10]))
    22 b = tf.Variable(tf.zeros([10]))
    23 y = tf.nn.softmax(tf.matmul(x, w) + b)
    24 
    25 # y_是真实概率分布
    26 # cross_entropy是信息熵 数学之美里有介绍 在这里作为损失函数
    27 y_ = tf.placeholder(tf.float32, [None, 10])
    28 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
    29 
    30 # 梯度下降算法
    31 train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
    32 
    33 # 全局参数初始化
    34 tf.global_variables_initializer().run()
    35 
    36 # 每次取1000个样本进行训练
    37 # 书上是取100 训出来的结果是92.05%(虽然每次也都在变 但变化不大)
    38 # 改成1000之后训出来是92.57% 表示根本没啥差距= =
    39 for i in range(10000):
    40     batch_xs, batch_ys = mnist.train.next_batch(10000)
    41     train_step.run({x: batch_xs, y_: batch_ys})
    42 
    43 # 最后返回计算分类是否正确的操作 correct_prediction
    44 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    45 
    46 # 把correct_prediction转成float32(double不行吗真是的= =) 再求平均
    47 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    48 
    49 # 输出准确率
    50 print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
  • 相关阅读:
    CF1386C Joker
    P7486 「StOI2031」彩虹
    CF1516E Baby Ehab Plays with Permutations
    重拾莫比乌斯反演
    联合省选 2020 补题记录
    拉格朗日插值如何插出系数
    NOI Online 2021 补题
    Re:从0开始的多项式生活
    LOJ #6485. LJJ 学二项式定理
    P5591 小猪佩奇学数学
  • 原文地址:https://www.cnblogs.com/general10/p/9651325.html
Copyright © 2011-2022 走看看