为什么tf.Variable可迭代但不能迭代
Why tf.Variable is iterable but can't be iterated
为什么这是真的:
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 的变量 class 中找到了以下方法:
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
为什么这是真的:
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 的变量 class 中找到了以下方法:
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