在TensorFlow中,是否有一种直接的方法来构造标量张量的阵列张量



例如,类似的东西:

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

是否有直接构造给定标量张量x在张量量中的阵列张量?不使用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.convert_to_tensor()拨打到任何期望tf.Tensor作为输入的功能,因此您可以直接将[[x, x], [0, 0]]直接传递给许多函数。

最新更新