tensorflow 2 - 如何在索引处直接更新 tf.Variable X 中的元素?
tensorflow 2 - how to directly update elements in tf.Variable X at indices?
有没有办法直接更新 tf.Variable
X 中索引处的元素,而无需创建与 X 具有相同形状的新张量?
tf.tensor_scatter_nd_update 创建一个新张量因此它似乎没有更新原始 tf.Variable.
This operation creates a new tensor by applying sparse updates to the input tensor.
tf.Variable assign 显然需要一个具有相同形状 X 的新张量 value
来更新 tf.Variable X。
assign(
value, use_locking=False, name=None, read_value=True
)
value A Tensor. The new value for this variable.
关于tf.tensor_scatter_nd_update
, you're right that it returns a new tf.tensor (and not tf.Variable). But about the assign
which is an attribute of tf.Variable,我觉得你有点看错文档了; value
只是您要分配给旧变量的特定索引的新项目。
据我所知,在 tensorflow all tensors are immutable like python numbers and strings; you can never update the contents of a tensor, only create a new one, source. And directly updating or manipulating of tf.tensor
or tf.Variable
such as numpy like item assignment is still not supported. Check the following Github issues to follow up the discussions: #33131, #14132.
在 numpy 中,我们可以执行您在评论框中显示的就地项目分配。
import numpy as np
a = np.array([1,2,3])
print(a) # [1 2 3]
a[1] = 0
print(a) # [1 0 3]
在 tf.Variable 中使用 assign
属性可以获得类似的结果。
import tensorflow as tf
b = tf.Variable([1,2,3])
b.numpy() # array([1, 2, 3], dtype=int32)
b[1].assign(0)
b.numpy() # array([1, 0, 3], dtype=int32)
稍后,我们可以将其转换为 tf.张量如下
b_ten = tf.convert_to_tensor(b)
b_ten.numpy() # array([1, 0, 3], dtype=int32)
我们也可以在 tf.tensor 中进行这样的项目分配,但我们需要将其转换为 tf.Variable首先,(我知道,不是很直观)。
tensor = [[1, 1], [1, 1], [1, 1]] # tf.rank(tensor) == 2
indices = [[0, 1], [2, 0]] # num_updates == 2, index_depth == 2
updates = [5, 10] # num_updates == 2
x = tf.tensor_scatter_nd_update(tensor, indices, updates)
x
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 1, 5],
[ 1, 1],
[10, 1]], dtype=int32)>
x = tf.Variable(x)
x
<tf.Variable 'Variable:0' shape=(3, 2) dtype=int32, numpy=
array([[ 1, 5],
[ 1, 1],
[10, 1]], dtype=int32)>
x[0].assign([5,1])
x
<tf.Variable 'Variable:0' shape=(3, 2) dtype=int32, numpy=
array([[ 5, 1],
[ 1, 1],
[10, 1]], dtype=int32)>
x = tf.convert_to_tensor(x)
x
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 5, 1],
[ 1, 1],
[10, 1]], dtype=int32)>
有没有办法直接更新 tf.Variable
X 中索引处的元素,而无需创建与 X 具有相同形状的新张量?
tf.tensor_scatter_nd_update 创建一个新张量因此它似乎没有更新原始 tf.Variable.
This operation creates a new tensor by applying sparse updates to the input tensor.
tf.Variable assign 显然需要一个具有相同形状 X 的新张量 value
来更新 tf.Variable X。
assign(
value, use_locking=False, name=None, read_value=True
)
value A Tensor. The new value for this variable.
关于tf.tensor_scatter_nd_update
, you're right that it returns a new tf.tensor (and not tf.Variable). But about the assign
which is an attribute of tf.Variable,我觉得你有点看错文档了; value
只是您要分配给旧变量的特定索引的新项目。
据我所知,在 tensorflow all tensors are immutable like python numbers and strings; you can never update the contents of a tensor, only create a new one, source. And directly updating or manipulating of tf.tensor
or tf.Variable
such as numpy like item assignment is still not supported. Check the following Github issues to follow up the discussions: #33131, #14132.
在 numpy 中,我们可以执行您在评论框中显示的就地项目分配。
import numpy as np
a = np.array([1,2,3])
print(a) # [1 2 3]
a[1] = 0
print(a) # [1 0 3]
在 tf.Variable 中使用 assign
属性可以获得类似的结果。
import tensorflow as tf
b = tf.Variable([1,2,3])
b.numpy() # array([1, 2, 3], dtype=int32)
b[1].assign(0)
b.numpy() # array([1, 0, 3], dtype=int32)
稍后,我们可以将其转换为 tf.张量如下
b_ten = tf.convert_to_tensor(b)
b_ten.numpy() # array([1, 0, 3], dtype=int32)
我们也可以在 tf.tensor 中进行这样的项目分配,但我们需要将其转换为 tf.Variable首先,(我知道,不是很直观)。
tensor = [[1, 1], [1, 1], [1, 1]] # tf.rank(tensor) == 2
indices = [[0, 1], [2, 0]] # num_updates == 2, index_depth == 2
updates = [5, 10] # num_updates == 2
x = tf.tensor_scatter_nd_update(tensor, indices, updates)
x
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 1, 5],
[ 1, 1],
[10, 1]], dtype=int32)>
x = tf.Variable(x)
x
<tf.Variable 'Variable:0' shape=(3, 2) dtype=int32, numpy=
array([[ 1, 5],
[ 1, 1],
[10, 1]], dtype=int32)>
x[0].assign([5,1])
x
<tf.Variable 'Variable:0' shape=(3, 2) dtype=int32, numpy=
array([[ 5, 1],
[ 1, 1],
[10, 1]], dtype=int32)>
x = tf.convert_to_tensor(x)
x
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 5, 1],
[ 1, 1],
[10, 1]], dtype=int32)>