Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why tf.Variable is iterable but can't be iterated

Why this is true:

from collections import Iterable
import tensorflow as tf

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

True

while this

iter(v)

gives

TypeError: 'Variable' object is not iterable.
like image 911
Pekka Avatar asked Dec 22 '25 16:12

Pekka


1 Answers

I found below method in Tensorflow's Variable 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

like image 65
Ali Ismayilov Avatar answered Dec 24 '25 05:12

Ali Ismayilov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!