0

Compare the following code snippets. I implemented a simple keras model like this

inp = layers.Input((10,2))
x = layers.Flatten()(inp)
x = layers.Dense(5)(x)
m = models.Model(inputs=inp, outputs=x)

For one reason or another, I need to have my model in an objective way. So no problem, it's easy to reimplement that into:

class MyModel(tf.keras.Model):
   def __init__(self, inp_shape, out_size = 5):
       super(MyModel, self).__init__()
       self.inp = layers.InputLayer(input_shape=inp_shape)
       self.flatten = layers.Flatten()
       self.dense = layers.Dense(out_size)

   def call(self, a):
       x = self.inp(a)
       x = self.flatten(x)
       x = self.dense(x)
       return x

However in the second case when I try to run:

m = MyModel((10,2))
m.summary()

I get:

ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.

I don't quite get why? Shouldn't the above be equivalent?

0

2 Answers 2

1

The reason for this is that when you create an object of this model you are just creating its layers and not its graph. So in short the output from layer 1 is not going in layer 2 cause those are entirely separate attributes of the class but when you call the model those separate attributes combines and form the graph.

Sign up to request clarification or add additional context in comments.

Comments

0

When you define a model in tf. keras with subclassed API, you need to build the model first by calling build or run the model on some data.

m = MyModel((10,2))
m.build(input_shape=(10, 2)) # < -- build the model 
m.summary()

That said, you don't also need to define self.inp while building the model with subclassed API. The .summary() may not look right to you for the subclassed model, you may need to check this instead.

7 Comments

Isn't that a bit repetitive, as I define input already here: MyModel((10,2)) ?
Not exactly. Building model in seq and func API are like data structure whereas in subclass API is simply a piece of python code. That’s why Input layer or a specification layer only part of the non-subclassed api model.
You shouldn’t or don't need to define the spec layer inside subclassed api model.
Which one is spec layer?
BTW the whole point of me trying to print the summary was to see if the output shapes match, however here I get: output shape "multiple", which does not match functional API summary that correctly printed Dense output shape as 5.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.