在 Tensorflow 中,是否有直接的方法从标量张量构造数组张量?

In Tensorflow, is there a direct way to construct an array tensor from a scalar tensor?

例如,类似于:

x = 12
y = np.array([[x, x], [0, 0]])

在Tensorflow中是否有直接构造给定标量张量x的数组张量y?不使用 tf.expand_dims 和 tf.pad.

x = tf.placeholder(tf.int32)
y = ?

非常感谢。

您可以使用 tf.convert_to_tensor() 函数来执行此操作:

x = tf.placeholder(tf.int32)  # NOTE: You should probably pass `shape=[]` as well.
y = tf.convert_to_tensor([[x, x], [0, 0]])

另请注意,TensorFlow 将在任何需要 tf.Tensor 作为输入的函数的输入上隐式调用 tf.convert_to_tensor(),因此您可以将 [[x, x], [0, 0]] 直接传递给许多函数.