【问题标题】:Assign value to tensor fields which satisfy condition in Tensorflow为满足 Tensorflow 条件的张量字段赋值
【发布时间】:2018-05-20 15:01:18
【问题描述】:

我用numpy中的例子来介绍一下问题:

arr  - some numpy array
a, b - constants
arr[arr < a] = b

现在,我想实现相同的功能,但在 Tensorflow 中,arr 将是一个张量。运行前张量值未知。

如您所见,答案将澄清为张量内的特定位置分配值,以及对张量执行条件化并检索满足条件的索引。

【问题讨论】:

标签: python numpy tensorflow


【解决方案1】:

正如@xdurch0 所提到的,tf.where() 通过适当的调节和广播应该可以解决问题:

b_broadcast = tf.ones(tf.shape(arr), dtype=arr.dtype) * b
arr = tf.where(tf.less(arr, a), b_broadcast, arr)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2021-09-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多