Def initialize_base_network()

I cannot understand why the initialization is as follows:

first we define a function " initialize_base_network()" that takes no input argument, then we assign this function to the “base_network” variable which makes it a function that takes no arguments, but then somehow we are able to put “input_a” in the “base_network” function even though it should not accept any input since its a function with no input arguments! … what am I missing … why this is correct in python?! … below is the code part im talking about.

def initialize_base_network():
input = Input(shape=(28,28,), name=“base_input”)
x = Flatten(name=“flatten_input”)(input)
x = Dense(128, activation=‘relu’, name=“first_base_dense”)(x)
x = Dropout(0.1, name=“first_dropout”)(x)
x = Dense(128, activation=‘relu’, name=“second_base_dense”)(x)
x = Dropout(0.1, name=“second_dropout”)(x)
x = Dense(128, activation=‘relu’, name=“third_base_dense”)(x)

return Model(inputs=input, outputs=x)

base_network = initialize_base_network()

input_a = Input(shape=(28,28,), name=“left_input”)
vect_output_a = base_network(input_a)

Hi @Firas_Muin_Moh_d_Al!

The difference is we are not assigning the function, initialize_base_network to the variable, base_network, we are calling initialize_base_network() and assigning the result to the variable base_network. You can tell we’re calling the function because of the () after the name.

Initialize_base_network() returns a model that takes parameters and that’s what we’re assigning to base_network.

1 Like

Hi ,
I also don’t understand the syntax of this code.
initialize_base_network() function returns a Model that was already initialized with inputs and outputs values: Model(inputs=input, outputs=x).
this is assigned to a variable base_network.

how is it possible that the we call this variable as function later on?
vect_output_a = base_network(input_a) ?

the base_network stores Model Class that already got inputs and outputs.

I don’t understand this syntax.
thanks for any feedback on this.

Thanks for asking @Rafi_Lavi! There’s one subtlety that I think is the missing piece for you.

Python has feature where you can call an instance of an object as if it is a function, and when you do, it will call that object’s __call__ method. For Model objects, the call method calls the model on new inputs. So, when you see base_network(input_a), that’s basically calling base_network with the new input, input_a. You can read more details about model.call here: tf.keras.Model  |  TensorFlow v2.10.0

The lecture video, “Coding a Multi-input Siamese network” gives a pretty good overview of how all the pieces fit together. It doesn’t really cover this particular subtlety, but now that you have this piece of info, it might be worth going back and re-watching the video again to get a good solid grasp of the whole picture.

1 Like