Error when building BasicDecoder TensorFlow Addons

Viewed 236

I am building a encoder-decoder using Tensorflow addons BasicDecoder and a Training Sampler, however I get an error while building the model. While trying to find my error y I tried also with other examples from forums and I still get the same error. (specifically I tried this other code)

import tensorflow as tf
from tensorflow import keras
import numpy as np
import tensorflow_addons as tfa
 
OUTPUT_CHARS = "0123456789-"
INPUT_CHARS = 'ADFJMNOSabceghilmnoprstuvy01234567890, '
encoder_embedding_size = 32
decoder_embedding_size = 32
units = 128

encoder_inputs = keras.layers.Input(shape=[None], dtype=np.int32)
decoder_inputs = keras.layers.Input(shape=[None], dtype=np.int32)
sequence_lengths = keras.layers.Input(shape=[], dtype=np.int32)
encoder_embeddings = keras.layers.Embedding(
    len(INPUT_CHARS) + 1, encoder_embedding_size)(encoder_inputs)
decoder_embedding_layer = keras.layers.Embedding(
    len(INPUT_CHARS) + 2, decoder_embedding_size)
decoder_embeddings = decoder_embedding_layer(decoder_inputs)
 
encoder = keras.layers.LSTM(units, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_embeddings)
encoder_state = [state_h, state_c]
 
sampler = tfa.seq2seq.sampler.TrainingSampler()
decoder_cell = keras.layers.LSTMCell(units)
output_layer = keras.layers.Dense(len(OUTPUT_CHARS) + 1)
decoder = tfa.seq2seq.basic_decoder.BasicDecoder(decoder_cell,
                                             sampler,
                                             output_layer=output_layer)
final_outputs, final_state, final_sequence_lengths = decoder(
decoder_embeddings, initial_state=encoder_state) #The error occurs in this line
Y_proba = keras.layers.Activation("softmax")(final_outputs.rnn_output)
model = keras.models.Model(inputs=[encoder_inputs, decoder_inputs],
                       outputs=[Y_proba])
optimizer = keras.optimizers.Nadam()
model.compile(loss="sparse_categorical_crossentropy", optimizer=optimizer,
          metrics=["accuracy"])

The error I get is: TypeError: type of argument "training" must be one of (bool, NoneType); got tensorflow.python.framework.ops.Tensor instead

My tensorflow versions are the following:

tensorflow=2.2.0

tensorflow_addons=0.10.0

I am pretty new to tensorflow and I really don't know where the error is. Any guidance will be appreciated.

The whole error is:

File "", line 39, in <module> decoder_embeddings, initial_state=encoder_state)

  File "...\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 922, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)

  File "...\anaconda3\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 262, in wrapper
    return converted_call(f, args, kwargs, options=options)

  File "...\anaconda3\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 418, in converted_call
    return _call_unconverted(f, args, kwargs, options, False)

  File "...\anaconda3\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 346, in _call_unconverted
    return f(*args, **kwargs)

  File "...\anaconda3\lib\site-packages\tensorflow_addons\seq2seq\decoder.py", line 171, in call
    decoder_init_kwargs=init_kwargs,

  File "...\anaconda3\lib\site-packages\typeguard\__init__.py", line 839, in wrapper
    check_argument_types(memo)

  File "...\anaconda3\lib\site-packages\typeguard\__init__.py", line 700, in check_argument_types
    raise exc from None

  File "...\anaconda3\lib\site-packages\typeguard\__init__.py", line 698, in check_argument_types
    check_type(description, value, expected_type, memo)

  File "...\anaconda3\lib\site-packages\typeguard\__init__.py", line 593, in check_type
    checker_func(argname, value, expected_type, memo)

  File "...\anaconda3\lib\site-packages\typeguard\__init__.py", line 417, in check_union
    format(argname, typelist, qualified_name(value)))

TypeError: type of argument "training" must be one of (bool, NoneType); got tensorflow.python.framework.ops.Tensor instead
1 Answers

According to tfa BasicDecoder documentation, at every call decoder should know if it is training or inference (default value = None, not sure why it is designed like that).

Give a try to:

  final_outputs, final_state, final_sequence_lengths = decoder(decoder_embeddings, initial_state=encoder_state, training=True)
Related