TFboy养成记 tf.cast,tf.argmax,tf.reduce_sum

referrence: 莫烦视频

先介绍几个函数

1.tf.cast()

TFboy

英文解释:

TFboy

也就是说cast的直译,类似于映射,映射到一个你制定的类型。

 

2.tf.argmax 原型:

TFboy

含义:返回最大值所在的坐标。(谁给翻译下最后一句???)

TFboy

 ps:谁给解释下axis最后一句话?

 TFboy

例子:

TFboy

TFboy

 

 3.tf.reduce_mean() 原型:

TFboy

含义:一句话来说就是对制定的reduction_index进行均值计算。

TFboy

注意,reduction_indices为0时,是算的不同的[]的同一个位置上的均值

为1是是算的[]内的均值。

TFboy
 1 import numpy as np
 2 import tensorflow as tf
 3 tt = tf.reduce_mean([1,2,3,4])
 4 ttt = tf.reduce_mean([[1.0,2,3,4,5],[6,7,8,9,10],[1,1,1,1,1]],reduction_indices=[1])
 5 t = tf.reduce_mean([[1.0,2,3,4,5],[6,7,8,9,10],[1,1,1,1,1]],reduction_indices=[0])
 6 sess = tf.Session()
 7 sess.run(tf.initialize_all_variables())
 8 
 9 print (sess.run(t))
10 print (sess.run(tt))
TFboy

 

 TFboy

 tf.equal:

含义比较两个tensor的值,如果在一个下表下一样,那么返回的tensor在这个下表上就为true.这个函数一般可以与cast在一起去(cast到float32上)计算一些准确率

TFboy


tensorflow 中 reduce_sum 理解

定义如下:

TFboy
reduce_sum(
    input_tensor,
    axis=None,
    keep_dims=False,
    name=None,
    reduction_indices=None
)
TFboy

reduce_sum 是 tensor 内部求和的工具。其参数中:

1. input_tensor 是要求和的 tensor

2. axis 是要求和的 rank,如果为 none,则表示所有 rank 都要仇和

3. keep_dims 求和后是否要降维

4. 这个操作的名称,可能在 graph 中 用

5. 已被淘汰的,被参数 axis 替代

 

示例如下:

x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x, 0)  # 对 tensor 的 0 级进行求和,[1,1,1] + [1,1,1] =  [2, 2, 2]
tf.reduce_sum(x, 1)  # 对 tensor 的 1 级进行仇和,[1+1+1, 1+1+1] = [3, 3]
tf.reduce_sum(x, 1, keep_dims=True)  # 对第 1 级进行求和,但不降维, [[3], [3]]
tf.reduce_sum(x, [0, 1])  # 0 级和 1级都要求和,6
tf.reduce_sum(x)  # 因为 x 只有 2 级,所以结果同上一个,6

相关文章:

  • 2021-12-14
  • 2021-07-07
猜你喜欢
  • 2021-06-22
  • 2021-08-15
  • 2021-07-05
  • 2021-12-19
  • 2021-05-23
  • 2021-07-08
  • 2022-02-18
相关资源
相似解决方案