abhishek jha
abhishek jha

Reputation: 1095

Explain keras code snippet

I have this piece of code :

from keras.models import Model
from keras.layers import Input, Dense

a = Input(shape=(32,))
b = Dense(32)(a)

in Dense(32)(a) I know that we are creating keras.layers.Dense object but what does (a) part to Dense(32) object we create?

Also how does python understand it internally?

Upvotes: 3

Views: 245

Answers (1)

rvinas
rvinas

Reputation: 11895

The part b = Dense(32)(a) creates a Dense layer that receives tensor a as input. It is done this way to allow using the same dense layer with different inputs (i.e. to allow sharing weights).

For example, consider the following snippet:

from keras.models import Model
from keras.layers import Input, Dense

a = Input(shape=(32,))
b = Input(shape=(32,))
dense = Dense(32)
c = dense(a)
d = dense(b)

Here, dense = Dense(32) instantiates a Dense layer, which is callable. You could think of it as if you were creating a function that you can call on different inputs (i.e. c = dense(a) and d = dense(b)). This is provides a very convenient way of sharing weights.

Upvotes: 4

Related Questions