Search code examples
pythontensorflowkerasdata-preprocessing

Get the actually used targets, not the input ones, from the TimeseriesGenerator in keras


Let's say I create the following time series generator with keras:

from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
gen = TimeseriesGenerator(data=[1,2,3,4,5,6,7,8,9], targets=[1,2,3,4,5,6,7,8,9], length=2, batch_size=1, start_index=5)

Since it has the setting start_index=5 it will skip the first 5 data points, so that gen contains only these actually usable data:

# first are the data points [x_n, x_m] and then is the corresponding label/target [y_n]
print(gen[0])
print(gen[1])
>> (array([[6, 7]]), array([8]))
>> (array([[7, 8]]), array([9]))

What I would like to have is an easy way to extract all the actually usable targets/labels/ground truth, so something like

print(gen.actual_targets)
>> [8,9]

But the closest I have come to was

print(gen.targets)
>> [1, 2, 3, 4, 5, 6, 7, 8, 9]

which gives only the input targets, not the really used ones. So, how can I get out of the generator the actually usable targets? Thanks


Solution

  • Maybe you can use TimeseriesGenerator.start_index and TimeseriesGenerator.end_index:

    gen.targets[gen.start_index:gen.end_index + 1]
    
    [8, 9]