TensorFlow 2.0: How to update tensors?

旧巷老猫 提交于 2020-06-27 12:59:05

问题


In TensorFlow 1.x, to update a tensor, I would use tf.scatter_update, to only update the relevant part of the tensor.

How can we do the same thing in TF 2.0?


回答1:


You can use 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)


来源:https://stackoverflow.com/questions/55652981/tensorflow-2-0-how-to-update-tensors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!