我想要的是在張量的末尾添加一定數量的零,例如 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])>
有什么辦法可以做到嗎?
uj5u.com熱心網友回復:
您可以嘗試使用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)>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/346290.html