python - 为什么 tf.Variable 可迭代但不能迭代

标签 python python-2.7 tensorflow iterable

为什么这是真的:

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.

最佳答案

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

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

关于python - 为什么 tf.Variable 可迭代但不能迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39807356/

相关文章:

python - Raspberry Pi 中的硬币计数器 (RPi-GPIO)

python - 如何用python连接远程mysql服务器

python - jupyter如何访问安装在正确路径中的新tensorflow模块?

尽管 cuda 的 deviceQuery 返回 "PASS"结果,GPU : no known devices, 上的 tensorflow

python - 如何使用列名列表从旧数据帧创建新的 pandas 数据帧

python - 如何从 django 模型 DateTimeField 获取本地日期时间?

Python 说我向我的函数传递了太多参数?

python - Tensorflow 总结 : adding a variable which does not belong to computational graph

python - 如何在pyqt中以默认名称和扩展名保存文件?

Python scapy "NameError: global name ' LOOPBACK_NAME'未定义”