【问题标题】:How to implement elements comparsion in tensor by Tensorflow?Tensorflow如何在张量中实现元素比较?
【发布时间】:2018-06-25 20:45:14
【问题描述】:

在我的自定义损失函数中,我想实现这个特定的计算:

输入:

tensor A: placeholder([None, 1])
tensor B: placeholder([None, 1])

和A、B的形状一样。

输出:

tensor res: placeholder([None, 1]).

例如:

tensor   A: [0, 0, 1, 2, 2, 2, 3,...]
tensor   B: [4, 9, 2, 3, 5, 9, 4,...]
tensor res: [4, 4, 2, 3, 3, 3, 4,...]

首先res[0] = B[0],如果A[i] == A[i-1],那么res[i] = res[i-1];否则,res[i] = B[i]。所以我们得到 res=[4, 4, 2, 3, 3, 3, 4,...]。

这个问题我真的很困扰,不期待在tensorflow中。希望能回答,thx。

【问题讨论】:

    标签: tensorflow deep-learning loss-function


    【解决方案1】:

    我认为在 tensorflow 中没有直接的方法来处理这个问题。但是,您可以创建自己的操作或在 tensorflow 中以粗略的方式实现它

    1. condition - 创建一个数组,索引处为 1,通过条件,其余为 0。 (可以轻松并行化并在 tensorflow 中使用tf.while_loop)

    2. prefixedSum - 在condition 上使用prefix sum 创建此数组。 (没有直接实现,但this 可以提供帮助)

    3. reducedB - 使用condition 创建一个从prefixedSum 数组中选择索引的选定元素数组。 (可以使用tf.while_loop实现)

    4. ans - 使用来自prefixedSum 的索引创建最终数组,并在reducedB 中查找该索引。 (同样可以使用tf.while_loop实现)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 2020-03-23
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      相关资源
      最近更新 更多