tensorflow / nmt

TensorFlow Neural Machine Translation Tutorial
Apache License 2.0
6.35k stars 1.96k forks source link

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

Open Abonia1 opened 4 years ago

Abonia1 commented 4 years ago

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

iamatsundere commented 4 years ago

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

mukurgupta commented 3 years ago

Hey, did you solve it?

dr-alberto commented 3 years ago

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

dr-alberto commented 3 years ago

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

priya170807 commented 3 years ago

Thanks!..I did the same

chaithyagr commented 2 years ago

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