【问题标题】:Why tf.Variable is iterable but can't be iterated为什么 tf.Variable 可迭代但不能迭代
【发布时间】:2017-02-09 23:12:03
【问题描述】:

为什么会这样:

from collections import Iterable
import tensorflow as tf

v = tf.Variable(1.0)
print(isinstance(v, Iterable))

True

此时

iter(v)

给予

TypeError: 'Variable' object is not iterable.

【问题讨论】:

    标签: python python-2.7 tensorflow iterable


    【解决方案1】:

    我在 Tensorflow 的变量类中找到了以下方法:

    def __iter__(self):
        """Dummy method to prevent iteration. Do not call.
        NOTE(mrry): If we register __getitem__ as an overloaded operator,
        Python will valiantly attempt to iterate over the variable's Tensor from 0
        to infinity.  Declaring this method prevents this unintended behavior.
        Raises:
          TypeError: when invoked.
        """
        raise TypeError("'Variable' object is not iterable.")
    

    https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/variables.py#L366

    【讨论】:

      猜你喜欢
      • 2011-02-05
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 2015-09-16
      • 2018-08-31
      • 1970-01-01
      相关资源
      最近更新 更多