简体   繁体   English

TensorFlow 2.0:如何更新张量?

[英]TensorFlow 2.0: How to update tensors?

In TensorFlow 1.x, to update a tensor, I would use tf.scatter_update , to only update the relevant part of the tensor. 在TensorFlow 1.x中,为了更新张量,我会使用tf.scatter_update来仅更新张量的相关部分。

How can we do the same thing in TF 2.0? 我们怎样才能在TF 2.0中做同样的事情?

You can use tf.tensor_scatter_nd_update() : 您可以使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM