nmt icon indicating copy to clipboard operation
nmt copied to clipboard

TypeError: __call__() got an unexpected keyword argument 'training'

Open Abonia1 opened this issue 5 years ago • 6 comments

Hello I have issue in using dynamic_decode in tfa as below with tensorflow 2.X

I really appreciate if anyone have already resolved this issue as I have spent last 4 hours in it but still no solution out there. image

Abonia1 avatar May 17 '20 10:05 Abonia1

I have the same issue and stuck with it. Did you fix that?

iamatsundere avatar Jul 03 '20 08:07 iamatsundere

Hey, did you solve it?

mukurgupta avatar Jul 15 '20 10:07 mukurgupta

Same problem as well. In my case I was using keras sub classing for creating the model.

dr-alberto avatar Nov 14 '20 20:11 dr-alberto

SOLUTION FOR THIS ISSUE

In my case I'm subclassing the keras Model class, just as an example:

class Basic(tf.keras.Model):
    def __init__(self):
        super().__init__()
        
        self.dense1 = tf.keras.layers.Dense(32, input_shape=(10,), activation='relu')
        self.dense2 = tf.keras.layers.Dense(2, activation='sigmoid')
        
    def __call__(self, inputs):
        x = self.dense1(inputs)
        x = self.dense2(x)
        return x

model = Basic()    
model.compile(loss="categorical_crossentropy", optimizer='Adam', metrics=["accuracy"])
model.fit(X_encoded, y)

Running this I got this error: TypeError: __call__() got an unexpected keyword argument 'training' The only thing I needed was to add training=False parameter to the __call__ function, so the code would be:

class Basic(tf.keras.Model):
    def __init__(self):
        super().__init__()
        
        self.dense1 = tf.keras.layers.Dense(32, input_shape=(10,), activation='relu')
        self.dense2 = tf.keras.layers.Dense(1, activation='sigmoid')
        
    def __call__(self, inputs, training=False):
        x = self.dense1(inputs)
        x = self.dense2(x)
        return x

model = Basic()    
model.compile(loss="categorical_crossentropy", optimizer='Adam', metrics=["accuracy"])
model.fit(X_encoded, y)

Hope this helps

dr-alberto avatar Nov 15 '20 05:11 dr-alberto

Thanks!..I did the same

priya170807 avatar Dec 17 '20 02:12 priya170807

I think you need to define your custom call function as :

def call(self, inputs):
    <do stuff>
    return output

If you use __call__, you are essentially overloading the core python codes..

chaithyagr avatar Aug 05 '21 08:08 chaithyagr