tf的小函数-tf.where,tf.gather

  • tf.sign和tf.reduce_sum
    https://blog.csdn.net/qq_15111861/article/details/81318963
    tf.sign就是sign函数

  • tf.reduce_max
    https://blog.csdn.net/lllxxq141592654/article/details/85345864
    注意keepdim和axis
    if keepdim==false
    axis=0,意思是我要消除第0维,所以找每一列的最大值来充当原来维度的唯一值,(维度数减1)
    axis=1,意思是我要消除第1维,所以找每一行的最大值来充当原来维度的唯一值,(维度数减1)

  • tf.get_variable_scope().reuse_variables()的作用
    https://blog.csdn.net/qq_34914551/article/details/86516705
    某个变量已经存在了,不能这样再次生成这个变量。其实就是因为第一个输入进去生成了一系列的变量,这些变量只能用于第一个输入送来的数据。现在第二个输入也想用第一个输入对应的参数,这是不行的。

  • tf.where
    returned in a 2-D tensor,将返回其中为true的元素的索引。
    https://www.cnblogs.com/lyc-seu/p/8565997.html
    coordinates——坐标

  • tf.not_equal
    返回 (x! = y) 元素的真值.
    该函数将返回一个 bool 类型的张量.

  • tf.gather th.gather_nd
    https://www.jianshu.com/p/dd2b8d290fa5

我们使用这个函数的一般是想完成这样一个功能:T是一个二维tensor,我们想要根据另外一个二维tensor value的最后一维最大元素的下标选出tensor T 中最后一维最大的元素,组成一个新的一维的tensor,那么就可以首先选出最后一维度的下标[1,2,3],然后将将其扩展成[[0,1],[1,2],[2,3]],然后使用这个函数选择即可。

max_indicies = tf.argmax(T, 1)
import tensorflow as tf

sess = tf.InteractiveSession()

values = tf.constant([[0, 0, 0, 1],
                      [0, 1, 0, 0],
                      [0, 0, 1, 0]])

T = tf.constant([[0, 1, 2 ,  3],
                 [4, 5, 6 ,  7],
                 [8, 9, 10, 11]])

max_indices = tf.argmax(values, axis=1)
#If T.get_shape()[0] is None, you can replace it with tf.shape(T)[0].
result = tf.gather_nd(T, tf.stack((tf.range(T.get_shape()[0], 
                                            dtype=max_indices.dtype),
                                   max_indices),
                                  axis=1))
print(result.eval())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值