Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

python - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...