【问题标题】:Is this possible with tf.tensor_scatter_nd_add这可能与 tf.tensor_scatter_nd_add
【发布时间】:2020-10-17 21:06:16
【问题描述】:

以下使用 tf.tensor_scatter_nd_add 的简单示例给我带来了问题。

B = tf.tensor_scatter_nd_add(A, indices, updates)

张量 A 为 (1,4,4)

A = [[[1. 1. 1. 1.],
      [1. 1. 1. 1.],
      [1. 1. 1. 1.],
      [1. 1. 1. 1.]]]

想要的结果是张量 B:

B = [[[1. 1. 1. 1.],
      [1. 2. 3. 1.],
      [1. 4. 5. 1.],
      [1. 1. 1. 1.]]]

即我想将这个较小的张量添加到张量 A 的 4 个内部元素中

updates = [[[1, 2],
            [3, 4]]]

张量流 2.1.0。我尝试了多种构建索引的方法。对 tensor_scatter_nd_add 的调用返回一个错误,指出内部尺寸不匹配。

更新张量是否需要与 A 的形状相同?

【问题讨论】:

    标签: tensorflow deep-learning tensorflow2.0 tf.keras keras-2


    【解决方案1】:

    涡虫,

    尝试通过以下方式传递索引和更新:使用形状 (n) 进行更新,使用形状 (n,3) 进行索引,其中 n 是更改项目的数量。 索引应指向您要更改的单个单元格:

    A = tf.ones((1,4,4,), dtype=tf.dtypes.float32)
    updates =  tf.constant([1., 2., 3., 4])
    indices = tf.constant([[0,1,1], [0,1,2], [0,2,1], [0,2,2]])
    tf.tensor_scatter_nd_add(A, indices, updates)
    
    <tf.Tensor: shape=(1, 4, 4), dtype=float32, numpy=
    array([[[1., 1., 1., 1.],
            [1., 2., 3., 1.],
            [1., 4., 5., 1.],
            [1., 1., 1., 1.]]], dtype=float32)>
    

    【讨论】:

    • @Planaria,很高兴它有帮助。请接受并投票
    • 当然是坡。刚刚做了。 (请确保我正确投票)。我真是个菜鸟! :)
    猜你喜欢
    • 2011-04-10
    • 2018-10-21
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多