Skip to content
Advertisement

how do I fit a time-series multi head model?

I try to create a model by concatenating 2 models together. The models I want to use, shall handle time series, and I’m experimenting with Conv1D layers. As these have an 3D input shape batch_shape + (steps, input_dim) and the Keras TimeseriesGenerator is providing such, I’m happy being able to make use of it when handling single head models.

JavaScript

This works fine for each of the models:)

Now I would like to experiment with concatenating both models to one (as I think it might enable me later when adding additional ‘heads’ to train them in parallel, and I guess using such models might be easier as handling a lot of separated once) and get a dual-head model, which can easily be created like this

JavaScript

This dual_head_model has an input_shape of 2 time 3D [(None, 5, 2), (None, 5, 2)] and looks finally this way enter image description here

Unfortunately I don’t know how to fit it :( and hope you will be able to provide me a solution on how to generate the needed shape of data. I tried to provide the previously used generators as list model_dual_head.fit([train_gen_AB, train_gen_CD], epochs=1, verbose=1), and also lists of the raw input data frames model_dual_head.fit(x=[Xtrain_AB, Xtrain_CD], y=[yTrain_AB, yTrain_CD], epochs=1, verbose=1), but it seems not to be in the right shape.

Thanks in advance

Wasili


based on Jacks comment I tried the using following code

JavaScript

but unfortunatelly it does not work, as the input_shape is not the same

JavaScript

I adapted the parenthesis in the generator based on Jacks note and get a different error now.

JavaScript
JavaScript

I thought about using a plain index iterating the generators which does fix the shape topic, but this results in a NonType error

JavaScript
JavaScript

The Notebook can be reached here


Finally Jacks input helped to find the solution. There was just missing to zip both generators to be able to iterated through them :)

JavaScript
JavaScript

Advertisement

Answer

I do not know if this already exists, but I believe you can create a new Generator that merges the two datasets. Assuming the two Generators are in lockstep, this should work:

JavaScript

This now gives a generator that yields both inputs as a tuple, and the common label as one data item. This could be a lambda, which would save the trouble of creating an entire Generator class.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement