Mario Bonsembiante
Mario Bonsembiante

Reputation: 53

How to generate sequence data with keras with multiple input?

I'm writing a VAE for sequence to sequence problem in keras. The decoder is an auto-regressive model so I have two different inputs, one for the encoder and the same (shifted by 1, but this is not the problem) for the decoder. I want also to do data augmentation so I decide to use the fit_generator() method but I have some problem on returning the two inputs.

I have tried to return a list of two input vector, like this

class DataGenerator(Sequence):
    def __init__(....

    def __getitem__(self, index):
        data = create_data()
        return  [data, data]

or a dictionary like this

return {"encoder_input_name" : "data, decoder_input_name" : data } 

where data is a numpy tensor of shape (batch_size, max_sequence_len, input_dimention).

I can't just use the same input layer because later the two input will be a little different, as I said the decoder input will be shifted by one with a different first element and other reasons.

When I return the list [data, data] or I have this error:

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

When I return the dictionary I have this error:

batch_size = x.shape[0]
AttributeError: 'str' object has no attribute 'shape'

How can I solve this issue?

Thank you very much!

EDIT

I changed the output of __getitem__ to [inpuut_1, input_2], [] and it worked.

Upvotes: 4

Views: 3344

Answers (1)

today
today

Reputation: 33460

You should return a tuple from generator/Sequence instance. The first element of the tuple is a list of input arrays (or just one array if your model has one input layer), and the second element is a list of output arrays (or just one array if your model has one output layer).

Therefore, __getitem__ should return something like this:

def __getitem__(self, index):
    # ...
    return  [inp_arr1, inp_arr2, ...], [out_arr1, out_arr2, ...]  # IMPORTANT: this is a tuple

Upvotes: 6

Related Questions