TensorFlow 2.0:如何更新张量?

问题描述 投票:3回答:1

在TensorFlow 1.x中,为了更新张量,我会使用tf.scatter_update来仅更新张量的相关部分。

我们怎样才能在TF 2.0中做同样的事情?

python tensorflow tensorflow2.0
1个回答
1
投票

你可以使用tf.tensor_scatter_nd_update()

import tensorflow as tf
import numpy as np 

tensor = tf.convert_to_tensor(np.ones((2, 2)), dtype=tf.float32)
indices = tf.constant([[0, 0]])
updates = tf.constant([0.0])

tf.tensor_scatter_nd_update(tensor, indices, updates).numpy()
# array([[0., 1.],
#        [1., 1.]], dtype=float32)
© www.soinside.com 2019 - 2024. All rights reserved.