There is less inputs received while giving the exact number of inputs required
I am testing an encoder decoder lstm model . the training phase went well but the testing phase after building the testing model is giving me an error while trying to input to the decoder layer , here's the error:
ValueError: Layer "model_decoder_testing" expects 3 input(s), but it received 1 input tensors. Inputs received: []
my script is : import numpy as np
``` import tensorflow as tf np.random.seed(0) from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense, Input, LSTM from tensorflow.keras.utils import plot_model
np.random.seed(1)
from tokenizers import Tokenizer import json import os
devices = tf.config.experimental.list_physical_devices('GPU') tf.config.experimental.set_memory_growth(devices[0], True) os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
def one_hot_decode(encoded_seq): return [np.argmax(vector) for vector in encoded_seq]
Even though we define the encoder and decoder models we still need to dynamically provide the decoder_input_data as follows:
it begins with a special symbol start
it will continue with an input created by the decoder at previous time step
in other words, decoder's output at time step t will be used decoder's input at time step t+1
def decode_sequence(batch_input, tokenizer, id2token, token2id): # Encode the input as state vectors. 695
states_value = encoder_model.predict(batch_input, verbose = 0) # Generate empty target sequence of length 1. target_seq = np.zeros((1, max_deco_input_len, embedding_weights.shape[1])) # one batch, sent length, word embedding dim # Populate the first token of target sequence with the start token. target_seq[:, 0, :] = embedding_weights[token2id[''],:]
decoded_seq = list()
stop_condition = False
# Sampling loop for the length of the sequence
for j in range(len(batch_input[0])):
if stop_condition:
break
# decode the input to a token/output prediction + required states for context vector Update input states (context vector)
# with the outputed states
output_tokens, h, c = decoder_model.predict_step([target_seq] + states_value)
print(output_tokens.shape)
# convert the token/output prediction to a token/output
sampled_token_index = np.argmax(output_tokens[0, -1, :])
sampled_token = id2token[sampled_token_index]
# add the predicted token/output to output sequence
decoded_seq.append(sampled_token)
# Exit condition: either hit max length
# or find <END> token.
if sampled_token == '<END>':
stop_condition = True
# Update the input target sequence with the predicted token/output
target_seq[:, j + 1, :] = embedding_weights[sampled_token_index,:]
states_value = [h, c]
return decoded_seq
def decode(decoded_seq): ''' Clips the gradients' values between minimum and maximum.
Arguments:
encoding -- a list containing the tokens
Returns:
decoding -- a string summerizing all tokens.
'''
decoding = ''
for token in decoded_seq:
if token == '</w>':
decoding = decoding + ' '
else:
decoding = decoding + token
return decoding
def calculating_combined_embeddings(embedding_weights, batch_input, tokenizer, token2id): ''' Creates a tensor of shape (batch size, sent length, word embedding dim) as input to the encoder by calculating the combined effect of tokens' weights as a whole word
Arguments:
embedding_weights -- a 2D array of embbedings
batch_input -- a list of lists of tokens for input sentences
tokenizer -- the BPE tokenizer to tokenize batch input
token2id -- a dictionary the translates tokens to their respective ids
Returns:
batch_input_tensor -- a tensor of shape. 695