【问题标题】:How to add a value of right zeros in tensorflow如何在张量流中添加右零值
【发布时间】:2021-12-17 16:58:36
【问题描述】:

我想要的是在张量的末尾添加一定数量的零,例如 3。这是一个示例(使用发明的 tf 函数):

tn = tf.constant([1, 2])
# out: <tf.Tensor: shape(2,), dtype=int32, numpy=array([1, 2])>

tn = tf.add_zeros(tn, 3, 'right')
# out: <tf.Tensor: shape(5,), dtype=int32, numpy=array([1, 2, 0, 0, 0])>

有什么办法可以做到吗?

【问题讨论】:

  • 在添加零之前tn 是否总是具有相同的形状?

标签: python tensorflow tensorflow2.0 tensor


【解决方案1】:

你可以试试tf.concat:

import tensorflow as tf

tn = tf.constant([1, 2])
# out: <tf.Tensor: shape(2,), dtype=int32, numpy=array([1, 2])>

tn = tf.concat([tn, tf.zeros((3), dtype=tf.int32)], axis=0)
print(tn)
tf.Tensor([1 2 0 0 0], shape=(5,), dtype=int32)

tf.pad

t = tf.constant([1, 2])
paddings = tf.constant([[0, 3]])
tf.pad(t, paddings, "CONSTANT") 
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([1, 2, 0, 0, 0], dtype=int32)>

【讨论】:

  • 谢谢!还有一个问题,所以我明白如果你不回答,如何将 ang 从 tf.Tensor(1, 2, 3, 4) 减少到 tf.Tensor(1, 2)?
  • 我通过使用自己的索引解决了它。抱歉是个愚蠢的问题
猜你喜欢
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2023-03-29
  • 2017-07-31
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多