Skip to main content
 首页 » 编程设计

python之Tensorflow 磁贴抛出 TypeError : List of Tensors when single Tensor expected

2025年01月19日26傻小

正在尝试使用这段代码生成一个包含一系列数字的扩展数组,但这会在 d = tf.tile(k, [m])

行中引发错误
import tensorflow as tf 
 
min_rating = tf.constant(0, tf.int64) 
max_rating = tf.constant(12, tf.int64) 
m = max_rating - min_rating + 1 
k = tf.range(m, dtype=tf.int64) 
d = tf.tile(k, [m]) 
 
with tf.Session() as sess: 
    a = sess.run([d]) 
    print a 

错误日志如下:

    d = tf.tile(k, [m]) 
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3740, in tile 
    name=name) 
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 493, in apply_op 
    raise err 
TypeError: List of Tensors when single Tensor expected 

我找不到任何引用资料。 tf.range 是否返回一个张量序列?

请您参考如下方法:

[m] 行 d = tf.tile(k, [m]) 是错误所指的“张量列表”。我猜你将 m 括起来是为了使 tf.tile 的 multiples 参数成为一维张量。才发现 [m] 只是一个张量列表。您可能想使用 tf.reshape 制作一维张量,即将错误行更改为:

d = tf.tile(k, tf.reshape(m, [1]))