Skip to content

model.inputs and model.outputs are None when creating a sub-classed model #45202

@noamgot

Description

@noamgot

System information

  • Have I written custom code (as opposed to using a stock example script
    provided in TensorFlow)
    : no
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux 4.19.112+ x86_64 - running in Google Colab
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue
    happens on a mobile device
    : N/A
  • TensorFlow installed from (source or binary): binary (pip)
  • TensorFlow version (use command below): v2.3.0-0-gb36436b087 2.3.0
  • Python version: 3.6.9
  • Bazel version (if compiling from source): N/A
  • GCC/Compiler version (if compiling from source): N/A
  • CUDA/cuDNN version: N/A
  • GPU model and memory: N/A

Describe the problem

I created a model using the sub-classing method. As I wanted to examine the model inputs and outputs (using model.inputs and model.outputs), I found them to be None. On the contrary, when I created the same model using the Sequential API, I do manage to see the inputs and outputs (using the aforementioned inputs/outputs attributes).
I also checked these attributes after predicting with the model, and calling to model.compile - nothing works, the outputs are still None.

I'd like to know how (or if?) it's possible to examine a sub-classed model's inputs and outputs this way.

Source code / logs

You can play with the code in this Colab notebook:
https://colab.research.google.com/drive/1Oq0oF9aITXNGI9iI3mTsJkmDBoKacDB8?usp=sharing

The model creating code is taken from the official TF 2 quickstart guides (with some minor modifications):
https://www.tensorflow.org/tutorials/quickstart/beginner
https://www.tensorflow.org/tutorials/quickstart/advanced

example 1 - Sequential API:

import TensorFlow as tf

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(10)
])

print(model.inputs)
print(model.outputs)

The output:

[<tf.Tensor 'flatten_5_input:0' shape=(None, 28, 28, 1) dtype=float32>]
[<tf.Tensor 'dense_11/BiasAdd:0' shape=(None, 10) dtype=float32>]

example 2 - Sub-Classed Model:

from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model

class MyModel(Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.flatten = Flatten(input_shape=(28, 28, 1))
    self.d1 = Dense(128, activation='relu')
    self.d2 = Dense(10)

  def call(self, x):
    x = self.flatten(x)
    x = self.d1(x)
    return self.d2(x)

# Create an instance of the model
model = MyModel()
print(model.inputs)
print(model.outputs)

The output:

None
None

Note:
I noticed that when I run this code locally, I get [] instead of None, but the principle still exists.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions