votes up 2

The layer has never been called and thus has no defined output shape.

Package:
keras
github stars 52268
Exception Class:
AttributeError

Raise code

"""   (or list of shape tuples, one tuple per output tensor).

    Raises:
        AttributeError: if the layer has no defined output shape.
        RuntimeError: if called in Eager mode.
    """
    if not self._inbound_nodes:
      raise AttributeError('The layer has never been called '
                           'and thus has no defined output shape.')
    all_output_shapes = set(
        [str(node.output_shapes) for node in self._inbound_nodes])
    if len(all_output_shapes) == 1:
      return self._inbound_nodes[0].output_shapes
    else:
      raise AttributeError('The layer "%s"'
😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 3 votes down

This exception is raised when the output_shape attribute is called on a Layer which has net been called.

looking at the error message it seems a little bit tricky and not clear. However to understand the main cause of this exception let's dig a little bit deeper and see where this exception happens. After following the link of this exception to the Keras source code we find out that the exception is raised here.

if not self._inbound_nodes:
  raise AttributeError('The layer has never been called and thus has no defined outpu   t shape.')  

This means This exception is raised when the _inbound_nodes attribute of the layer is None.

You might be asking yourself what is _inbound_nodes? What is Node anyways?

Well according to this documentation

A Node describes the connectivity between two layers. Each time a layer is connected to some new input, a node is added to `layer._inbound_nodes`. Each time the output of a layer is used by another layer, a node is added to `layer._outbound_nodes`

So in our case the issue is that _inbound_nodes is None which means the layer we are calling the out_shape attribute on doesn't have _inbound_nodes which in turn means the layer is not connected to other input layer before it.

Let's see a sample code to understand the issue a little bit clearer.

First install Tensorflow before calling the given sample code.

  • Upgrade pip
$ pip install --upgrade pip

  • Then install Tensorflow and numpy
$ pip install tensorflow numpy

  • Then run the following code.
import numpy as np
import tensorflow as tf
x = np.arange(0, 8, dtype=np.float32).reshape((1, 8))
x = tf.constant(value=x, dtype=tf.float32,)
dense = tf.keras.layers.Dense(units=2)
out = dense(x)
print(dense.output_shape)

The output error and its stack trace is shown bellow.

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-fbcb67ecb382> in <module>()  8 dense = tf.keras.layers.Dense(units=2)  9 out = dense(x) ---> 10 dense.output_shape 
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py in output_shape(self)  2226 """  2227 if not self._inbound_nodes: -> 2228 raise AttributeError(f'The layer "{self.name}" has never been called '  2229 'and thus has no defined output shape.')  2230 all_output_shapes = set( 
AttributeError: The layer "dense_9" has never been called and thus has no defined output shape.

How to fix it

In the above the sample code dense layer has no input layer which causes the exception when the output_shape is called. You might assume that input x is added to the layer call and it should have been working. However x is just a simple tensor and doesn't count as a layer. Therefore to fix this error we have to give our layer an input layer as follows.

x = tf.keras.layers.Input(shape=(8,))
dense = tf.keras.layers.Dense(units=2)
out = dense(x)
print(dense.output_shape)

(None, 2)

viola! our call to dense.output_shapehas produced an output and didn't throw an exception.

Also you should take note that calling dense.output_shape before calling dense(x) does cause the same exception. So look out for that too.

Dec 31, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix