Tensorflow学习知识点记录

╰+攻爆jí腚メ 2022-01-22 07:21 407阅读 0赞

Tensorflow

  • 快速理解tf.Session()
  • Tensorflow 中使用 Variable
  • Placeholder 传入值
  • tensorflow reduction_indices理解
  • An overview of gradient descent optimization algorithms 加速神经网络训练 (Speed Up Training)

快速理解tf.Session()

Session 是 Tensorflow 为了控制和输出文件的执行的语句. 运行 session.run() 可以获得你要得知的运算结果, 或者是你所要运算的部分。

  1. import tensorflow as tf
  2. # create two matrixes
  3. matrix1 = tf.constant([[3,3]])
  4. matrix2 = tf.constant([[2],
  5. [2]])
  6. product = tf.matmul(matrix1,matrix2)

因为 product 不是直接计算的步骤, 所以我们会要使用 Session 来激活 product 并得到计算结果. 有两种形式使用会话控制 Session 。

  1. # method 1
  2. sess = tf.Session()
  3. result = sess.run(product)
  4. print(result)
  5. sess.close()
  6. # [[12]]
  7. # method 2
  8. with tf.Session() as sess:
  9. result2 = sess.run(product)
  10. print(result2)
  11. # [[12]]

https://blog.csdn.net/weixin_40458355/article/details/80351641

Tensorflow 中使用 Variable

在 Tensorflow 中,定义了某字符串是变量,它才是变量,这一点是与 Python 所不同的。

定义语法:state = tf.Variable()

  1. import tensorflow as tf
  2. state = tf.Variable(0, name='counter')
  3. # 定义常量 one
  4. one = tf.constant(1)
  5. # 定义加法步骤 (注: 此步并没有直接计算)
  6. new_value = tf.add(state, one)
  7. # 将 State 更新成 new_value
  8. update = tf.assign(state, new_value)

如果你在 Tensorflow 中设定了变量,那么初始化变量是最重要的!!所以定义了变量以后, 一定要定义 init = tf.initialize_all_variables() .

  1. tf.global_variables_initializer()

到这里变量还是没有被激活,需要再在 sess 里, sess.run(init) , 激活 init 这一步.

  1. # 如果定义 Variable, 就一定要 initialize
  2. # init = tf.initialize_all_variables() # tf 马上就要废弃这种写法
  3. init = tf.global_variables_initializer() # 替换成这样就好
  4. # 使用 Session
  5. with tf.Session() as sess:
  6. sess.run(init)
  7. for _ in range(3):
  8. sess.run(update)
  9. print(sess.run(state))

注意:直接 print(state) 不起作用!!

一定要把 sess 的指针指向 state 再进行 print 才能得到想要的结果!

Placeholder 传入值

这一次我们会讲到 Tensorflow 中的 placeholder , placeholder 是 Tensorflow 中的占位符,暂时储存变量.

Tensorflow 如果想要从外部传入data, 那就需要用到tf.placeholder(), 然后以这种形式传输数据sess.run(***, feed_dict={input: **}).

  1. import tensorflow as tf
  2. # 在 Tensorflow 中需要定义 placeholder 的 type ,一般为 float32 形式
  3. input1 = tf.placeholder(tf.float32)
  4. input2 = tf.placeholder(tf.float32)
  5. # mul = multiply 是将input1和input2 做乘法运算,并输出为 output
  6. ouput = tf.multiply(input1, input2)

接下来, 传值的工作交给了sess.run() , 需要传入的值放在了feed_dict={}并一一对应每一个 input. placeholder 与 feed_dict={} 是绑定在一起出现的。

  1. with tf.Session() as sess:
  2. print(sess.run(ouput, feed_dict={input1: [7.], input2: [2.]}))
  3. # [ 14.]

tensorflow reduction_indices理解

在tensorflow的使用中,经常会使用tf.reduce_mean,tf.reduce_sum等函数,在函数中,有一个reduction_indices参数,表示函数的处理维度,直接上图,一目了然:
tf.reduce_mean求平均值 tf.reduce_sum 求和

在这里插入图片描述
需要注意的一点,在很多的时候,我们看到别人的代码中并没有reduction_indices这个参数,此时该参数取默认值None,将把input_tensor降到0维,也就是一个数。

An overview of gradient descent optimization algorithms 加速神经网络训练 (Speed Up Training)

http://ruder.io/optimizing-gradient-descent/
这篇文章探讨了有多少最流行的基于梯度的优化算法实际工作。

Table of contents:

  1. Gradient descent variants
  2. Batch gradient descent
  3. Stochastic gradient descent
  4. Mini-batch gradient descent
  5. Challenges
  6. Gradient descent optimization algorithms
  7. Momentum
  8. Nesterov accelerated gradient
  9. Adagrad
  10. Adadelta
  11. RMSprop
  12. Adam
  13. AdaMax
  14. Nadam
  15. AMSGrad
  16. Visualization of algorithms
  17. Which optimizer to choose?
  18. Parallelizing and distributing SGD
  19. Hogwild!
  20. Downpour SGD
  21. Delay-tolerant Algorithms for SGD
  22. TensorFlow
  23. Elastic Averaging SGD
  24. Additional strategies for optimizing SGD
  25. Shuffling and Curriculum Learning
  26. Batch normalization
  27. Early Stopping
  28. Gradient noise
  29. Conclusion
  30. References

发表评论

表情:
评论列表 (有 0 条评论,407人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mysql知识记录

    1、删除包含外键的表数据 在表中的列包含外键的时候,删除数据会报外键关联,无法删除数据的错误, 这时候如果想要删除数据,不管关联的外键,就要先将外键检查置为失效,删除之

    相关 vuex知识记录

    state: 单一状态树,每个应用将仅仅包含一个 store 实例。 getters: mutation: (同步操作) this.$store.commit('xxx'...