1
$\begingroup$

In a class named Generator, I have defined a model with the below method define_model:

def define_model(self):
    conv2d = Generator.__last_block()
    output = Activation('tanh')(conv2d)
    model = Model(self.input_layer, output)
    return model

In another file, I am writing the entry-point of my Python program, which calls the generator's define_model method:

generator = Generator()
generator_model = generator.define_model()

input_low_resolution = Input(shape=low_resolution_shape)
generated_high_resolution_images = generator_model(input_low_resolution)

As I'm reading a course, I however have a queston about this code: what does Keras do when it executes the line generated_high_resolution_images = generator_model(input_low_resolution)?

As far I can understand, it doesn't define a model (my model is already defined thanks to generator.define_model()!). And since the Keras methods train, fit or other aren't called, I deduce that this line doesn't train the model.

By the way, it's a really weird line for me, because it passes a parameter to a reference (the reference to the object is generator_model and the parameter is input_low_resolution). Normally we pass parameters to the reference's methods (constructor and other methods).

$\endgroup$

1 Answer 1

1
$\begingroup$

Keras does a lot of stuff when you call a model. I guess that the most important is that it defines the trainable variables and the graph based on the inputs and outputs. For instance actually making any dense layers, and not just the representations.

For instance, if you did mod1=generator_model(input_low_resolution) and mod2=generator_model(input_low_resolution), then if you train mod1 then mod2 would not be affected as they have different parameters. So the .define_model just makes a "handle" to your model, which when called actually "builds" it.

This functionality is very useful when you want to mix-and-match multiple layer configurations, or working with more complex structures like for instance GANs or ADDA.

I would argue that your implementation is not exactly canonical and actually I'd recommend looking at the tensorflow.keras.Model example. Here they introduce a very nice, canonical way of making your own model.

Furthermore maybe Keras Model class API and Getting started with the Keras functional API can be of help.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.