You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is how I fixed it for my code. I highly suspect there's a better way to fix this, but in short three things needed to be fixed:
Change 'end_token_id' to 'stop_token_ids'
Wrap the token id argument in a list
add '.to_tensor()' to the encoder input tokens'
(Note, I extended mine from Spanish only to French, so my encoders are named "fr_..." instead of "spa_")
def decode_sequences(input_sentences):
batch_size = 1
# Tokenize the encoder input.
encoder_input_tokens = ops.convert_to_tensor(eng_tokenizer(input_sentences))
if len(encoder_input_tokens[0]) < MAX_SEQUENCE_LENGTH:
pads = ops.full((1, MAX_SEQUENCE_LENGTH - len(encoder_input_tokens[0])), 0)
#encoder_input_tokens = ops.concatenate([encoder_input_tokens.to_tensor(), pads], 1) # <-- Original
encoder_input_tokens = ops.concatenate([encoder_input_tokens.to_tensor(), pads], 1) # <-- Add ".to_tensor()" at the base of this
# Define a function that outputs the next token's probability given the
# input sequence.
def next(prompt, cache, index):
logits = transformer([encoder_input_tokens, prompt])[:, index - 1, :]
# Ignore hidden states for now; only needed for contrastive search.
hidden_states = None
return logits, hidden_states, cache
# Build a prompt of length 40 with a start token and padding tokens.
length = 40
start = ops.full((batch_size, 1), fr_tokenizer.token_to_id("[START]"))
pad = ops.full((batch_size, length - 1), fr_tokenizer.token_to_id("[PAD]"))
prompt = ops.concatenate((start, pad), axis=-1)
generated_tokens = nlp.samplers.GreedySampler()(
next,
prompt,
# end_token_id = fr_tokenizer.token_to_id("[END]") #<-- Original
stop_token_ids=[fr_tokenizer.token_to_id("[END]")], #<-- Change argument name and wrap in list
index=1, # Start sampling after start token.
)
generated_sentences = fr_tokenizer.detokenize(generated_tokens)
return generated_sentences
Issue Type
Bug
Source
binary
Keras Version
3.2.1
Custom Code
No
OS Platform and Distribution
Colab
Python version
3.10.12
GPU model and memory
Colab T4 GPU
Current Behavior?
I've been trying to run the English-to-Spanish translation with KerasNLP but I get stuck at the prediction/eval part:
The model trains successfully, though.
Standalone code to reproduce the issue or tutorial link
Relevant log output
The text was updated successfully, but these errors were encountered: