Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 What is the use of call method and when to use it?
I am having a hardtime understanding call methods in python. I have this class object

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
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

class Foo:
    def __init__(self, units):
    def __call__(self):
        print ('called '+self.units)

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?.
It's a way to create functions out of class instances.

In the expression bar = foo(), the parentheses are an operator. Specifically, they are the function call operator. Normally you only see this with functions:

def foo(x):
    return 2 * x + 1
bar = foo(5)
#        ^ function call operator
The __call__ method of a class allows you to override that operator, just as the __add__ method allows you to override the + operator. This allows your class to operate as if it were a function. A typical example is to make an efficient factorial function by storing previously calculated values:

class Factorial(object):

    def __init__(self):
        self.facts = [1, 1]

    def __call__(self, x):
            return self.facts[x]
        except IndexError:
            while len(self.facts) <= x:
                self.facts.append(self.facts[-1] * len(self.facts))
            return self.facts[-1]
You can now create an instance of the factorial object and use it as a function:

>>> factorial = Factorial() >>> factorial(5) # adds items to factorial.facts 120 >>> factorial(3) # Use factorial.facts[3] 6
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  I'm trying to figure out whether this is a method or function call 357mag 2 220 Jul-04-2019, 01:43 AM
Last Post: ichabod801
  Method not defined diskt 1 113 Jul-03-2019, 12:55 PM
Last Post: noisefloor
  API Call Gets 404 Error jimbone30 0 110 Jun-28-2019, 04:06 AM
Last Post: jimbone30
  call func from dict mcmxl22 3 256 Jun-21-2019, 05:20 AM
Last Post: snippsat
  Method not regocnized Lass86 3 182 Jun-19-2019, 09:03 AM
Last Post: Lass86
  API call returning list value of 'None' jimbone30 5 261 Jun-14-2019, 07:42 PM
Last Post: jimbone30
  Call a Function contained in a Tuple SamGer 7 316 Jun-14-2019, 05:00 PM
Last Post: ThomasL
  variable call back into an array yamifm0f 3 223 Jun-07-2019, 02:44 PM
Last Post: heiner55
  web3 method rembrandt 3 150 Jun-04-2019, 05:43 PM
Last Post: buran
  Why call import twice? Winfried 5 542 May-24-2019, 09:05 AM
Last Post: Winfried

Forum Jump:

Users browsing this thread: 1 Guest(s)