【问题标题】:tensorflow.python.framework.ops.EagerTensor' object does not support item assignment TypeErrortensorflow.python.framework.ops.EagerTensor' 对象不支持项赋值 TypeError
【发布时间】:2021-08-06 14:21:20
【问题描述】:

在编写 Yolo version1 损失函数时,我在这行代码中遇到了这个错误:

box_predictions[..., 2:4] = tf.Variable((tf.math.sign(box_predictions[..., 2:4])) * (tf.math.abs(box_predictions[..., 2:4])))

即使使用这些代码行,它也会显示相同的错误:

box11 = tf.math.sign(box_predictions[..., 2:4])
box12 = tf.math.abs(box_predictions[..., 2:4])
box_predictions[..., 2:4] = box11 * box12

【问题讨论】:

  • 错误发生在哪一行?
  • 在这行box_predictions[..., 2:4] = tf.Variable((tf.math.sign(box_predictions[..., 2:4])) * (tf.math.abs(box_predictions[..., 2:4])))

标签: python tensorflow deep-learning yolo


【解决方案1】:

您似乎正在尝试更新您的张量。这在张量流中没有实现。 见https://www.tensorflow.org/api_docs/python/tf/tensor_scatter_nd_update

因此需要使用 tf.tensor_scatter_nd_update 函数

【讨论】:

    【解决方案2】:

    非常感谢@ablanch5,

    另一种解决方案是将其转换为数组numpy,然后将其重新转换为张量,它工作正常,代码如下:

    box_predictions_np = box_predictions.numpy()
    box_predictions_np[..., 2:4] = (tf.math.sign(box_predictions[..., 2:4]))* (tf.math.abs(box_predictions[..., 2:4]))
    box_predictions = tf.convert_to_tensor(box_predictions_np, dtype=tf.float32) 
    

    您的解决方案也很有效,但您应该小心索引。 谢谢,

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2014-02-18
      相关资源
      最近更新 更多