gpt4 book ai didi

python - 具有矢量值的 Tensorflow 稀疏张量到密集张量

转载 作者:太空宇宙 更新时间:2023-11-04 02:11:29 26 4
gpt4 key购买 nike

我有一些稀疏索引:

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

每个指标对应的值为:

[[0.1 0.2 0.3]
[0.4 0.5 0.6]
[0.7 0.8 0.9]
[1.0 1.1 1.2]
[1.3 1.4 1.5]
[1.6 1.7 1.8]]

如何在tensorflow中将6x3值张量转换为3x3x3稠密张量? indices 中未指定的索引值为零向量 [0. 0. 0.]。稠密张量就是这样:

[[[0.1 0.2 0.3]
[0.4 0.5 0.6]
[0.0 0.0 0.0]]

[[0.7 0.8 0.9]
[1.0 1.1 1.2]
[1.3 1.4 1.5]]

[[1.6 1.7 1.8]
[0.0 0.0 0.0]
[0.0 0.0 0.0]]]

最佳答案

你可以用 tf.scatter_nd 做到这一点:

import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
indices = tf.constant(
[[0, 0],
[0, 1],
[1, 0],
[1, 1],
[1, 2],
[2, 0]])
values = tf.constant(
[[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
[0.7, 0.8, 0.9],
[1.0, 1.1, 1.2],
[1.3, 1.4, 1.5],
[1.6, 1.7, 1.8]])
out = tf.scatter_nd(indices, values, [3, 3, 3])
print(sess.run(out))

输出:

[[[0.1 0.2 0.3]
[0.4 0.5 0.6]
[0. 0. 0. ]]

[[0.7 0.8 0.9]
[1. 1.1 1.2]
[1.3 1.4 1.5]]

[[1.6 1.7 1.8]
[0. 0. 0. ]
[0. 0. 0. ]]]

关于python - 具有矢量值的 Tensorflow 稀疏张量到密集张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53644034/

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