tensorflow MNIST fully_connected_feed.py失败:range()至少需要2个参数(给定1个)

1 tensorflow

我在其中一个张量流教程中运行该示例时遇到了问题.该教程说运行我只需要输入python fully_connected_feed.py.当我这样做时,它会获取输入数据,但随后失败,如下所示:

Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Traceback (most recent call last):
  File "fully_connected_feed.py", line 225, in <module>
    tf.app.run()
  File "/Users/me/anaconda/lib/python2.7/site-packages/tensorflow/python/platform/default/_app.py", line 11, in run
    sys.exit(main(sys.argv))
  File "fully_connected_feed.py", line 221, in main
    run_training()
  File "fully_connected_feed.py", line 141, in run_training
    loss = mnist.loss(logits, labels_placeholder)
  File "/Users/me/tftmp/mnist.py", line 96, in loss
    indices = tf.expand_dims(tf.range(batch_size), 1)
TypeError: range() takes at least 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

认为这个错误是因为会话设置和/或张量评估存在问题.这是导致问题的mnist.py中的函数:

def loss(logits, labels):
  """Calculates the loss from the logits and the labels.

  Args:
    logits: Logits tensor, float - [batch_size, NUM_CLASSES].
    labels: Labels tensor, int32 - [batch_size].

  Returns:
    loss: Loss tensor of type float.
  """
  # Convert from sparse integer labels in the range [0, NUM_CLASSSES)
  # to 1-hot dense float vectors (that is we will have batch_size vectors,
  # each with NUM_CLASSES values, all of which are 0.0 except there will
  # be a 1.0 in the entry corresponding to the label).
  batch_size = tf.size(labels)
  labels = tf.expand_dims(labels, 1)
  indices = tf.expand_dims(tf.range(batch_size), 1)
  concated = tf.concat(1, [indices, labels])
  onehot_labels = tf.sparse_to_dense(
      concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)
  cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, onehot_labels,
                                                          name='xentropy')
  loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
  return loss
Run Code Online (Sandbox Code Playgroud)

如果我将loss函数中的所有代码放在一个with tf.Session():块中,它就会超过这个错误.但是,我后来得到了关于未初始化变量的其他错误,所以我猜测会话设置或初始化或其他什么方面的主要问题.作为张量流的新手,我有点不知所措.有任何想法吗?

[注意:我根本没有编辑过代码,只是从tensorflow教程下载并尝试按照说明运行,python fully_connected_feed.py]

mrr*_*rry 6

出现这个问题是因为在GitHub上的TensorFlow源代码的最新版本中,tf.range()更新为更宽松的参数(以前它需要两个参数;现在它具有与Python的range()内置函数相同的语义),并且fully_connected_feed.py示例具有已更新以利用此功能.

但是,如果您尝试针对TensorFlow的二进制分发运行此版本,则会出现此错误,因为更改tf.range()尚未合并到二进制包中.

最简单的解决方案是下载旧版本mnist.py.或者,您可以从源代码构建以使用最新版本的教程.