I am trying to follow this code but on another data set:https://www.tensorflow.org/tutorials/text/transformer#encoder_layer I needed to compile and fit the model. however, I get this error while running; I don't know what it means:
Models passed to `fit` can only have `training` and the first argument in `call` as positional arguments, found: ['tar', 'enc_padding_mask', 'look_ahead_mask', 'dec_padding_mask'].
Here it is the model:
class Transformer(tf.keras.Model):
def __init__(self, num_layers, d_model, num_heads, dff, input_vocab_size,
target_vocab_size, pe_input, pe_target, rate=0.1,**kwargs,):
super(Transformer, self).__init__(**kwargs)
self.encoder = Encoder(num_layers, d_model, num_heads, dff,
input_vocab_size, pe_input, rate)
self.decoder = Decoder(num_layers, d_model, num_heads, dff,
target_vocab_size, pe_target, rate)
self.final_layer = tf.keras.layers.Dense(target_vocab_size)
def get_config(self):
config = super().get_config().copy()
config.update({
'dff':self.dff,
'input_vocab_size':self.input_vocab_size,
'target_vocab_size':self.target_vocab_size,
'pe_input':self.pe_input,
'pe_target':self.pe_target,
#'vocab_size': self.vocab_size,
'num_layers': self.num_layers,
#'units': self.units,
'd_model': self.d_model,
'num_heads': self.num_heads,
'rate': self.rate,
})
return config
def call(self, inp, tar, training, enc_padding_mask,
look_ahead_mask, dec_padding_mask):
enc_output = self.encoder(inp, training, enc_padding_mask) # (batch_size, inp_seq_len, d_model)
# dec_output.shape == (batch_size, tar_seq_len, d_model)
dec_output, attention_weights = self.decoder(
tar, enc_output, training, look_ahead_mask, dec_padding_mask)
final_output = self.final_layer(dec_output) # (batch_size, tar_seq_len, target_vocab_size)
# return final_output, attention_weights
return tf.keras.Model(inputs=[inputs, dec_inputs], outputs=outputs, name=name)
and creating the model, compiling it, and fitting it as follows:
transformer = Transformer(num_layers, d_model, num_heads, dff,
input_vocab_size, target_vocab_size,
pe_input=input_vocab_size,
pe_target=target_vocab_size,
rate=dropout_rate)
transformer.compile(optimizer=optimizer, loss=loss_function, metrics=[accuracy])
transformer.fit(dataset, epochs=EPOCHS)
EDIT: Bases on @Geeocode updated the def function in transformer class to be:
def call(self, inp, tar, enc_padding_mask,look_ahead_mask, dec_padding_mask, training=False,):
enc_output = self.encoder(inp, training, enc_padding_mask) # (batch_size, inp_seq_len, d_model)
# dec_output.shape == (batch_size, tar_seq_len, d_model)
dec_output, attention_weights = self.decoder(
tar, enc_output, training, look_ahead_mask, dec_padding_mask)
final_output = self.final_layer(dec_output) # (batch_size, tar_seq_len, target_vocab_size)
return final_output, attention_weights
However, I still get the same error
The reason why you got the error is because self.call
only takes two variables an input
and a training
flag. If you have multiple input variables, they are passed as a tuple. Therefore, you can have a function definition similar to the following:
def call(self, input, training):
inp, tar, enc_padding_mask,look_ahead_mask, dec_padding_mask = input
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With