gpt4 book ai didi

vector - 用零展开 Tensorflow 中的 Vector 和空间元素

转载 作者:行者123 更新时间:2023-12-01 07:44:23 26 4
gpt4 key购买 nike

我想将向量元素彼此隔开并用零填充:

  a = [1, 5, 7, ..., 3]

带有两个零的 a 的空格元素:

  b = [1, 0, 0, 5, 0, 0, 7, 0, 0, ... , 3, 0, 0]

我用来分隔元素的零的数量应该是灵活的。

在 Tensorflow 的图形上执行此操作的最佳方法是什么?

最佳答案

你能按照解释的那样做吗in this post (接受答案的第二个例子)?

基本上,我会首先将 b 创建为零向量,然后计算指向 b 的索引,用于 a 中的所有值,并且然后使用链接帖子中的代码将 a 的值分配给这些索引。

可能是这样的:

a = tf.constant(np.array([1, 3, 5, 7]))
dim = a.get_shape()[0].value
n = 2
b = tf.fill(dims=[dim*(n+1)], value=0.0)
new_indices = np.array([i + i*n for i in range(0, dim)])
# now call referenced code with new_indices to update `b`

我不确定这本身是最好的方法,但它可以完成工作。

关于vector - 用零展开 Tensorflow 中的 Vector 和空间元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42391598/

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