
Question:
Following <a href="https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html" rel="nofollow">this article</a>, I'm trying to implement a generative RNN. In the mentioned article, the training and validation data are passed as fully loaded np.array
s. But I'm trying to use the model.fit_generator
method and provide a generator instead.
I know that if it was a straightforward model, the generator should return:
def generator():
...
yield (samples, targets)
But this is a generative model which means there are two models involved:
encoder_inputs = Input(shape=(None,))
x = Embedding(num_encoder_tokens, embedding_dim)(encoder_inputs)
x.set_weights([embedding_matrix])
x.trainable = False
x, state_h, state_c = LSTM(embedding_dim, return_state=True)(x)
encoder_states = [state_h, state_c]
decoder_inputs = Input(shape=(None,))
x = Embedding(num_decoder_tokens, embedding_dim)(decoder_inputs)
x.set_weights([embedding_matrix])
x.trainable = False
x = LSTM(embedding_dim, return_sequences=True)(x, initial_state=encoder_states)
decoder_outputs = Dense(num_decoder_tokens, activation='softmax')(x)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2)
As mentioned before, I'm trying to use a generator:
model.fit_generator(generator(),
steps_per_epoch=500,
epochs=20,
validation_data=generator(),
validation_steps=val_steps)
But what should the generator()
return? I'm a little confused since there are two input collections and one target.
Since your model has two inputs and one output, the generator should return a tuple with two elements where the first element is a <strong>list</strong> containing two arrays, which corresponds to two input layers, and the second element is an array corresponding to output layer:
def generator():
...
yield [input_samples1, input_samples2], targets
Generally, in a model with M
inputs and N
outputs, the generator should return a tuple of two lists where the first one has M
arrays and the second one has N
arrays:
def generator():
...
yield [in1, in2, ..., inM], [out1, out2, ..., outN]