zoukankan      html  css  js  c++  java
  • TF——深度学习逻辑回归源码(加各种用到算法的解析)

    解析在注释里:

     1 import tensorflow as tf
     2 from tensorflow.examples.tutorials.mnist import input_data
     3 import os
     4 os.environ["CUDA_VISIBLE_DEVICES"]="0"
     5 mnist = input_data.read_data_sets("./data/MNIST_data/", one_hot=True)
     6 learning_rate=0.01
     7 training_epochs=25
     8 batch_size=100  #一次训练所选取的样本数。
     9 display_step=1
    10 
    11 if __name__ =='__main__':
    12     x=tf.placeholder(tf.float32,[None,784])
    13     y=tf.placeholder(tf.float32,[None,10])
    14 
    15     print("PPPPPP")
    16     batch_xs, batch_ys = mnist.train.next_batch(batch_size)
    17     print(batch_xs.shape)
    18     print("LLLLLLLLLLLL")
    19     print(batch_ys.shape)
    20 
    21     '''Variable 初始化'''
    22     W=tf.Variable(tf.zeros([784,10]))
    23     b=tf.Variable(tf.zeros([10]))
    24 
    25     '''逻辑回归模型'''
    26     pred=tf.nn.softmax(tf.matmul(x,W)+b)
    27 
    28     '''构造代价函数'''
    29     '''
    30     tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
    31     reduce_mean(input_tensor,
    32                     输入的待降维的tensor
    33                     axis=None,
    34                     指定的轴,如果不指定,则计算所有元素的均值
    35                     keep_dims=False,
    36                     是否降维度
    37                     name=None,
    38                     操作的名称
    39                     reduction_indices=None
    40                     用来指定轴
    41                     )
    42     '''
    43     cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))
    44 
    45     '''梯度下降最小值'''
    46     optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
    47 
    48     init = tf.global_variables_initializer()
    49     with tf.Session() as sess:
    50         sess.run(init)
    51 
    52         '''训练模型'''
    53         for epoch in range(training_epochs):
    54             avg_cost = 0
    55         total_batch = int(mnist.train.num_examples / batch_size)
    56         for i in range(total_batch):
    57             batch_xs, batch_ys = mnist.train.next_batch(batch_size)
    58             _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})
    59             # Conpute average loss
    60             avg_cost += c / total_batch
    61         if (epoch + 1) % display_step == 0:
    62             print("第:", '%04d步' % (epoch + 1), "标准差:", "{:.09f}".format(avg_cost))
    63 
    64 
    65         '''测试模型'''
    66         '''
    67         argmax(f(x))是使得 f(x)取得最大值所对应的变量点x(或x的集合)
    68         test = np.array([
    69             [1, 2, 3],
    70              [2, 3, 4], 
    71              [5, 4, 3], 
    72              [8, 7, 2]])
    73             np.argmax(test, 0)   #输出:array([3, 3, 1]
    74             np.argmax(test, 1)   #输出:array([2, 2, 0, 0]
    75 
    76             axis = 0:
    77               axis=0时比较每一列的元素,将每一列最大元素所在的索引记录下来,最后输出每一列最大元素所在的索引数组。
    78             axis = 1:
    79       axis=1的时候,将每一行最大元素所在的索引记录下来,最后返回每一行最大元素所在的索引数组。
    80         '''
    81         correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    82         accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    83         print("准确的:", accuracy.eval({x: mnist.test.images[:3000], y: mnist.test.labels[:3000]}))
  • 相关阅读:
    markdown 书写文档的框架
    使用snap
    如何让pandas表格直接转换为markdown表格
    我们需要怎样去写博客
    jupyter notebook 远程访问
    安装tensorflowGPU版本
    Data Science Project
    使用python处理地理数据:Geopandas
    python移植环境
    Jupyter notbook& REVEAL.JS& nbconvert 使用jupyter notebook制作slides
  • 原文地址:https://www.cnblogs.com/smartisn/p/12573646.html
Copyright © 2011-2022 走看看