Question: Complete the translate function below by replacing all None placeholders with proper parameters. This function will take care of the following steps: Process the sentence
Complete the translate function below by replacing all None placeholders with proper parameters.
This function will take care of the following steps:
Process the sentence to translate and encode it
Set the initial state of the decoder
Get predictions of the next token starting with the token for a maximum of iterations in case the token is never returned
Return the translated text as a string the logit of the last iteration this helps measure how certain was that the sequence was translated in its totality and the translation in token format.
def translatemodel text, maxlength temperature:
Translate a given sentence from English to Portuguese
Args:
model tfkeras.Model: The trained translator
text string: The sentence to translate
maxlength int optional: The maximum length of the translation. Defaults to
temperature float optional: The temperature that controls the randomness of the predicted tokens. Defaults to
Returns:
tuplestr npfloat, tfTensor: The translation, logit that predicted token and the tokenized translation
# Lists to save tokens and logits
tokens, logits
### START CODE HERE ###
# PROCESS THE SENTENCE TO TRANSLATE
# Convert the original string into a tensor
text tfNoneNonetfNone
# Vectorize the text using the correct vectorizer
context NoneNonetotensor
# Get the encoded context pass the context through the encoder
# Hint: Remember you can get the encoder by using model.encoder
context None.NoneNone
# INITIAL STATE OF THE DECODER
# First token should be SOS token with shape
nexttoken tfNoneNone None None
# Initial hidden and cell states should be tensors of zeros with shape UNITS
state tfNoneNone None tfNoneNone None
# You are done when you draw a EOS token as next token initial state is False
done None
# Iterate for maxlength iterations
for None in NoneNone:
# Generate the next token
try:
nexttoken, logit state, done None
decoderNone,
contextNone,
nexttokenNone,
doneNone,
stateNone,
temperatureNone
except:
raise ExceptionProblem generating the next token"
# If done then break out of the loop
if None:
None
# Add nexttoken to the list of tokens
None.NoneNone
# Add logit to the list of logits
None.NoneNone
### END CODE HERE ###
# Concatenate all tokens into a tensor
tokens tfconcattokens axis
# Convert the translated tokens into text
translation tfsqueezetokenstotexttokens idtoword
translation translation.numpydecode
return translation, logits tokens
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
