Tensorflow tile抛出TypeError:预期单个Tensor时的张量列表

时间:2018-04-20 15:03:23

标签: python python-2.7 tensorflow

尝试使用此代码生成包含一系列数字的扩展数组,但这会在行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是否返回张量序列?

1 个答案:

答案 0 :(得分:1)

[m]行中的

d = tf.tile(k, [m])是错误所指的“张量列表”。我猜你用括号mmultiples tf.tile参数作为1-D Tensor。事实证明,[m]只是一个张量列表。您可能希望使用tf.reshape制作1-D Tensor,即将错误行更改为:

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