【发布时间】:2022-01-20 19:47:19
【问题描述】:
我找不到如何用另一个张量数据替换部分张量数据。挖了一点之后,我看到很多报告说张量是不可分配的数据;建议了一些解决方法,例如 (https://github.com/tensorflow/tensorflow/issues/14132#issuecomment-483002522)。
让我举一个简单的例子来说明我在寻找什么。我有两批如下:
x=·tf.random.uniform((2,3,2))
y= tf.random.uniform((2,3,2))
print (x)
print ('===================')
print (y)
上面两批的输出如下:
<tf.Tensor: shape=(2, 3, 2), dtype=float32, numpy=
array([[[0.17130184, 0.5413419 ],
[0.6435019 , 0.82179713],
[0.79388785, 0.9519701 ]],
[[0.46769345, 0.9812336 ],
[0.5796915 , 0.29866755],
[0.0442245 , 0.86057484]]], dtype=float32)>
===================
<tf.Tensor: shape=(2, 3, 2), dtype=float32, numpy=
array([[[0.82299507, 0.8277409 ],
[0.24233484, 0.4353037 ],
[0.23145556, 0.00768614]],
[[0.83972216, 0.03451204],
[0.46768224, 0.44939125],
[0.7840742 , 0.99360645]]], dtype=float32)>
我想将 x 批次中每个数组的第一行替换为 y 批次中的对应数组。
我期待这样的结果:
<tf.Tensor: shape=(2, 3, 2), dtype=float32, numpy=
array([[[0.82299507, 0.8277409 ], # copied from the y batch
[0.6435019 , 0.82179713],
[0.79388785, 0.9519701 ]],
[[0.83972216, 0.03451204], # copied from the y batch
[0.5796915 , 0.29866755],
[0.0442245 , 0.86057484]]], dtype=float32)>
将批次转换为 NumPy 时以下工作(但这不是我想要的,我想直接使用张量)
x = x.numpy()
y = y.numpy()
x[:, 0:1 , : ] = y[:, 0:1 , :]
x
输出是 NumPy 数组,我可以再次将其转换为张量,但我想直接在张量上进行此类操作。
array([[[0.82299507, 0.8277409 ],
[0.6435019 , 0.82179713],
[0.79388785, 0.9519701 ]],
[[0.83972216, 0.03451204],
[0.5796915 , 0.29866755],
[0.0442245 , 0.86057484]]], dtype=float32)
非常感谢任何帮助。
【问题讨论】:
-
github.com/tensorflow/tensorflow/issues/… 你可能会发现 lokk at
assigntf 变量的方法很有用。 -
感谢您的帮助;但您的链接已包含在我原来的问题中。让我们等待可以在这方面提供帮助的人
标签: tensorflow keras deep-learning conv-neural-network tensor