tensorflow - 如何移动张量中的值

标签 tensorflow

我有带值的形状 [batch_size, A] 的张量 T 和带移位参数的形状 [batch_size] 的张量 S。

我想将 T[b] 中的值向右移动 S[b] 个位置,应删除 T[b] 的最后一个 S[b] 元素,并将新元素设置为 0。

所以基本上想做这样的事情:

for i in range(batch_size):
  T[i] = zeros[:S[i]] + T[i, :A-S[i]]

例子:

For:
T = [[1, 2, 3], [4, 5, 6]]
S = [1, 2]

Return:
T' = [[0, 1, 2], [0, 0, 4]]

有什么简单的方法吗?

最佳答案

您可以为此目的使用 tf.concat 和 tf.stack:

T_shift = tf.zeros((batch_size, A), tf.float32)
tmp = []

for i in xrange(batch_size):
    tmp.append(tf.concat([T_shift[i, :S[i, 0]],T[i, :17 - S[i,0]]], axis = 0))
T_shift = tf.stack(tmp)

关于tensorflow - 如何移动张量中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48215077/

相关文章:

python - 命名自定义层中层的权重

python-3.x - 在 Python3 中使用 Keras 优化 CNN 的架构

python - tf.nn.depthwise_conv2d 太慢了。正常吗?

python-2.7 - 无效参数错误 : You must feed a value for placeholder tensor 'input_1/X' with dtype float

python - 使用 tf.data 和 mode.fit 时 1DConv 输入的维度出错

python - 如何在tensorflow 2.0中更新镜像变量?

tensorflow - Keras 自定义合并两个张量

python - InvalidArgumentError : input depth must be evenly divisible by filter depth: 4 vs 3

python - Tensorflow translate.py导入错误: No module named translate

python - Tensorflow 1.11 支持 python 3.7 吗?