I am trying to train rasa core using my custom dialog learning strategy. As a starting point, I am planning to implement using encoder decoder architecture. I have trained the network model using keras as per the documentation (Sequence to sequence - training - Keras Documentation) with my custom dataset and obtained the model.
But when I tried to integrate it into the model_architecture of the keras_policy of rasa, I get the following error. I have attached the code here as follws,
class RestaurantPolicy(KerasPolicy):
def model_architecture(self, input_shape, output_shape):
input_texts = []
target_texts = []
flatten_input_texts = []
flatten_target_texts = []
input_intent_sv = set()
target_intent_sv = set()
with open('data/stories.md', 'r', encoding='utf-8') as dataset:
lines = f.read().split('\n')
for line in lines[: min(num_samples, len(lines) - 1)]:
input_text, target_text = line.split('\t')
# We use "tab" as the "start sequence" character
# for the targets, and "\n" as "end sequence" character.
target_text = '\t' + target_text + '\n'
input_texts.append(input_text)
target_texts.append(target_text)
for char in input_text:
if char not in input_characters:
input_characters.add(char)
for char in target_text:
if char not in target_characters:
target_characters.add(char)
input_intent_sv = sorted(list(input_intent_sv))
target_intent_sv = sorted(list(target_intent_sv))
num_encoder_tokens = len(input_intent_sv)
num_decoder_tokens = len(target_intent_sv)
max_encoder_seq_length = max([len(txt) for txt in flatten_input_texts])
max_decoder_seq_length = max([len(txt) for txt in flatten_target_texts])
print('Number of samples:', len(input_texts))
print('Number of unique input tokens:', num_encoder_tokens)
print('Number of unique output tokens:', num_decoder_tokens)
print('Max sequence length for inputs:', max_encoder_seq_length)
print('Max sequence length for outputs:', max_decoder_seq_length)
input_token_index = dict(
[(intent_sv, i) for i, intent_sv in enumerate(input_intent_sv)])
target_token_index = dict(
[(intent_sv, i) for i, intent_sv in enumerate(target_intent_sv)])
encoder_input_data = np.zeros(
(len(flatten_input_texts), max_encoder_seq_length, num_encoder_tokens),
dtype='float32')
decoder_input_data = np.zeros(
(len(flatten_input_texts), max_decoder_seq_length, num_decoder_tokens),
dtype='float32')
decoder_target_data = np.zeros(
(len(flatten_input_texts), max_decoder_seq_length, num_decoder_tokens),
dtype='float32')
for i, (input_text, target_text) in enumerate(zip(flatten_input_texts, flatten_target_texts)):
for t, char in enumerate(input_text):
encoder_input_data[i, t, input_token_index[char]] = 1.
encoder_input_data[i, t + 1:, input_token_index[' ']] = 1.
for t, char in enumerate(target_text):
# decoder_target_data is ahead of decoder_input_data by one timestep
decoder_input_data[i, t, target_token_index[char]] = 1.
if t > 0:
# decoder_target_data will be ahead by one timestep
# and will not include the start character.
decoder_target_data[i, t - 1, target_token_index[char]] = 1.
decoder_input_data[i, t + 1:, target_token_index[' ']] = 1.
decoder_target_data[i, t:, target_token_index[' ']] = 1.
# Define an input sequence and process it.
encoder_inputs = Input(shape=(None, num_encoder_tokens))
encoder = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
# We discard `encoder_outputs` and only keep the states.
encoder_states = [state_h, state_c]
# Set up the decoder, using `encoder_states` as initial state.
decoder_inputs = Input(shape=(None, num_decoder_tokens))
# We set up our decoder to return full output sequences,
# and to return internal states as well. We don't use the
# return states in the training model, but we will use them in inference.
decoder = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder(decoder_inputs,
initial_state=encoder_states)
decoder_dense = Dense(num_decoder_tokens, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# Define the model that will turn
# `encoder_input_data` & `decoder_input_data` into `decoder_target_data`
model = Model(np.array([encoder_inputs, decoder_inputs]), np.array(decoder_outputs))
# Run training
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
metrics=['accuracy'])
logger.debug(model.summary())
return model
It throws the following error,
rasa.core.policies.keras_policy - Fitting model with 21562 total samples and a validation split of 0.2
Traceback (most recent call last):
File "C:\Apps\NAME\Scripts\rasa-script.py", line 11, in <module>
load_entry_point('rasa', 'console_scripts', 'rasa')()
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\__main__.py", line 76, in main
cmdline_arguments.func(cmdline_arguments)
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\cli\train.py", line 77, in train
kwargs=extract_additional_arguments(args),
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\train.py", line 40, in train
kwargs=kwargs,
File "c:\apps\NAME\lib\asyncio\base_events.py", line 579, in run_until_complete
return future.result()
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\train.py", line 87, in train_async
kwargs,
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\train.py", line 169, in _train_async_internal
kwargs=kwargs,
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\train.py", line 203, in _do_training
kwargs=kwargs,
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\train.py", line 331, in _train_core_with_validated_data
kwargs=kwargs,
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\core\train.py", line 66, in train
agent.train(training_data, **kwargs)
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\core\agent.py", line 713, in train
self.policy_ensemble.train(training_trackers, self.domain, **kwargs)
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\core\policies\ensemble.py", line 90, in train
policy.train(training_trackers, domain, **kwargs)
File "C:\Users\NAME\AppData\Roaming\Python\Python37\site-packages\rasa\core\policies\keras_policy.py", line 210, in train
**self._train_params
File "c:\apps\NAME\lib\site-packages\keras\engine\training.py", line 952, in fit
batch_size=batch_size)
File "c:\apps\NAME\lib\site-packages\keras\engine\training.py", line 751, in _standardize_user_data
exception_prefix='input')
File "c:\apps\NAME\lib\site-packages\keras\engine\training_utils.py", line 102, in standardize_input_data
str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[0, 0, 1, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0]],
[[0, 0, 1, ..., 0, 0, 0],...
These are the information which I have got, while training,
Training Core model...
Using TensorFlow backend.
Number of samples: 7395
Number of unique input tokens: 56
Number of unique output tokens: 62
Max sequence length for inputs: 124
Max sequence length for outputs: 206
Layer (type) Output Shape Param # Connected to
input_1 (InputLayer) (None, None, 56) 0
input_2 (InputLayer) (None, None, 62) 0
lstm_1 (LSTM) [(None, 256), (None, 320512 input_1[0][0]
lstm_2 (LSTM) [(None, None, 256), 326656 input_2[0][0]
lstm_1[0][1]
lstm_1[0][2]
dense_1 (Dense) (None, None, 62) 15934 lstm_2[0][0]
Total params: 663,102
Trainable params: 663,102
Non-trainable params: 0
Could I get help to solve this issue?