Jul-14-2019, 12:17 PM
I am having a hardtime understanding call methods in python. I have this class object
In this class object two Dense layer attributes self.W1 and self.W1 were created and then a features and a hidden_with_time_axis attribute is called on W1 and W2.
I know that when we initiate the line self.W1 = tf.keras.layers.Dense(units) its creating a Dense neural network layer of n units. But under call method they are doing this self.W1(features). What is the purpose of this and what is the intentional behaviour?
I tried to make a sample class object for understanding classes
But I still dont understand the use of a call object and when and where to use it? Can anyone explain it in a simple manner?.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Attention(tf.keras.Model): def __init__( self , units): super (Attention, self ).__init__() self .W1 = tf.keras.layers.Dense(units) self .W2 = tf.keras.layers.Dense(units) self .V = tf.keras.layers.Dense( 1 ) def call( self , features, hidden): hidden_with_time_axis = tf.expand_dims(hidden, 1 ) score = tf.nn.tanh( self .W1(features) + self .W2(hidden_with_time_axis)) attention_weights = tf.nn.softmax( self .V(score), axis = 1 ) context_vector = attention_weights * features context_vector = tf.reduce_sum(context_vector, axis = 1 ) return context_vector, attention_weights |
I know that when we initiate the line self.W1 = tf.keras.layers.Dense(units) its creating a Dense neural network layer of n units. But under call method they are doing this self.W1(features). What is the purpose of this and what is the intentional behaviour?
I tried to make a sample class object for understanding classes
1 2 3 4 5 6 7 8 9 |
class Foo: def __init__( self , units): self .units = units def __call__( self ): print ( 'called ' + self .units) a = Foo( 3 ) b = Foo(a) |