votes up 5

Cannot iterate over a Tensor with unknown first dimension.

Package:
keras
github stars 52268
Exception Class:
TypeError

Raise code

shape = [dim.value for dim in self.shape.dims]

    if shape is None:
      raise TypeError('Cannot iterate over a Tensor with unknown shape.')
    if not shape:
      raise TypeError('Cannot iterate over a scalar.')
    if shape[0] is None:
      raise TypeError(
          'Cannot iterate over a Tensor with unknown first dimension.')
    return _KerasTensorIterator(self, shape[0])

  @property
  def name(self):
    """Returns the (non-unique, optional) name of this symbolic Keras value."""
    return self._name

  @cl

Ways to fix

votes up 1 votes down

KerasTensors are tensor-like objects that represent the symbolic inputs and outputs of Keras layers during Functional model construction

Error code:

from keras.engine import keras_tensor
import tensorflow as tf 

kt = keras_tensor.KerasTensor(type_spec=tf.TensorSpec(shape=(None,1), dtype=tf.float32)) #<--shape first value is None
iteration = iter(kt)
print(iteration)

When we iterate Kerastensor with the shape Null or None or the first value None, it will pop an error.

Fix code:

from keras.engine import keras_tensor
import tensorflow as tf 

kt = keras_tensor.KerasTensor(type_spec=tf.TensorSpec(shape=(1,2), dtype=tf.float32))
iteration = iter(kt)
print(iteration)
Jul 08, 2021 anonim answer
anonim 13.0k

Add a possible fix

Please authorize to post fix