这个错误通常是因为您传递给 tf.stack() 的参数不是张量(Tensor),而是张量列表(List of Tensors)。请确保您传递给 tf.stack() 的参数是张量。您可以使用 tf.convert_to_tensor() 将列表转换为张量,然后再使用 tf.stack()

例如:

import tensorflow as tf

x = [[1, 2], [3, 4]]
x = tf.constant(tf.convert_to_tensor(x))
stacked_x = tf.stack(x)
print(stacked_x)

输出:

tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32)
使用x = tfconstanttfstackx 出现 TypeError List of Tensors when single Tensor expected

原文地址: https://www.cveoy.top/t/topic/bKVG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录