gpt4 book ai didi

python - 当切片本身是 tensorflow 中的张量时如何进行切片分配

转载 作者:行者123 更新时间:2023-12-01 00:49:37 26 4
gpt4 key购买 nike

我想在 tensorflow 中进行切片分配。我知道我可以使用:

my_var = my_var[4:8].assign(tf.zeros(4))

以此为基础link .

正如您在 my_var[4:8] 中看到的,我们这里有特定的索引 4、8 用于切片然后赋值。

我的情况不同,我想根据张量进行切片,然后进行分配。

out = tf.Variable(tf.zeros(shape=[8,4], dtype=tf.float32))

rows_tf = tf.constant (
[[1, 2, 5],
[1, 2, 5],
[1, 2, 5],
[1, 4, 6],
[1, 4, 6],
[2, 3, 6],
[2, 3, 6],
[2, 4, 7]])

columns_tf = tf.constant(
[[1],
[2],
[3],
[2],
[3],
[2],
[3],
[2]])

changed_tensor = [[8.3356, 0., 8.457685 ],
[0., 6.103182, 8.602337 ],
[8.8974, 7.330564, 0. ],
[0., 3.8914037, 5.826657 ],
[8.8974, 0., 8.283971 ],
[6.103182, 3.0614321, 5.826657 ],
[7.330564, 0., 8.283971 ],
[6.103182, 3.8914037, 0. ]]

此外,这是 sparse_indices 张量,它是 rows_tfcolumns_tf 的串联,使得整个索引需要更新(如果有帮助:)

sparse_indices = tf.constant(
[[1 1]
[2 1]
[5 1]
[1 2]
[2 2]
[5 2]
[1 3]
[2 3]
[5 3]
[1 2]
[4 2]
[6 2]
[1 3]
[4 3]
[6 3]
[2 2]
[3 2]
[6 2]
[2 3]
[3 3]
[6 3]
[2 2]
[4 2]
[4 2]])

我想做的是做这个简单的作业:

out[rows_tf, columns_tf] = changed_tensor

为此我正在这样做:

out[rows_tf:column_tf].assign(changed_tensor)

但是,我收到此错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected begin, end, and strides to be 1D equal size tensors, but got shapes [1,8,3], [1,8,1], and [1] instead. [Op:StridedSlice] name: strided_slice/

这是预期的输出:

[[0.        0.        0.        0.       ]
[0. 8.3356 0. 8.8974 ]
[0. 0. 6.103182 7.330564 ]
[0. 0. 3.0614321 0. ]
[0. 0. 3.8914037 0. ]
[0. 8.457685 8.602337 0. ]
[0. 0. 5.826657 8.283971 ]
[0. 0. 0. 0. ]]

知道如何完成这个任务吗?

提前谢谢您:)

最佳答案

这个示例(从 tf 文档 tf.scatter_nd_update here 扩展而来)应该会有所帮助。

您希望首先将 row_indices 和 column_indices 组合成二维索引列表,这是 tf.scatter_nd_updateindices 参数。然后,您提供了一个预期值列表,即更新

ref = tf.Variable(tf.zeros(shape=[8,4], dtype=tf.float32))
indices = tf.constant([[0, 2], [2, 2]])
updates = tf.constant([1.0, 2.0])

update = tf.scatter_nd_update(ref, indices, updates)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print sess.run(update)
Result:

[[ 0. 0. 1. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 2. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]

专门针对您的数据,

ref = tf.Variable(tf.zeros(shape=[8,4], dtype=tf.float32))
changed_tensor = [[8.3356, 0., 8.457685 ],
[0., 6.103182, 8.602337 ],
[8.8974, 7.330564, 0. ],
[0., 3.8914037, 5.826657 ],
[8.8974, 0., 8.283971 ],
[6.103182, 3.0614321, 5.826657 ],
[7.330564, 0., 8.283971 ],
[6.103182, 3.8914037, 0. ]]
updates = tf.reshape(changed_tensor, shape=[-1])
sparse_indices = tf.constant(
[[1, 1],
[2, 1],
[5, 1],
[1, 2],
[2, 2],
[5, 2],
[1, 3],
[2, 3],
[5, 3],
[1, 2],
[4, 2],
[6, 2],
[1, 3],
[4, 3],
[6, 3],
[2, 2],
[3, 2],
[6, 2],
[2, 3],
[3, 3],
[6, 3],
[2, 2],
[4, 2],
[4, 2]])

update = tf.scatter_nd_update(ref, sparse_indices, updates)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print sess.run(update)

Result:

[[ 0. 0. 0. 0. ]
[ 0. 8.3355999 0. 8.8973999 ]
[ 0. 0. 6.10318184 7.33056402]
[ 0. 0. 3.06143212 0. ]
[ 0. 0. 0. 0. ]
[ 0. 8.45768547 8.60233688 0. ]
[ 0. 0. 5.82665682 8.28397083]
[ 0. 0. 0. 0. ]]

关于python - 当切片本身是 tensorflow 中的张量时如何进行切片分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56673546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com