简体   繁体   English

如何使用训练有素的Keras GRU模型预测新的数据系列?

[英]How to predict a new data series with a trained Keras GRU model?

I'm trying to use a trained Keras sequence model (GRU) to predict some new data samples, but have some problem creating the time series generator. 我正在尝试使用训练有素的Keras序列模型(GRU)来预测一些新的数据样本,但是在创建时间序列生成器时遇到一些问题。

In the training process, the validation set was predicted using model.predict_generator() , which used a Python generator created by keras.preprocessing.sequence.TimeseriesGenerator() ( link ) as input. 在训练过程中,使用model.predict_generator()预测验证集,该模型使用keras.preprocessing.sequence.TimeseriesGenerator()链接 )创建的Python生成器作为输入。 I wanted to repeat the process with a new test set, only to find that TimeseriesGenerator() needs both data and targets as input. 我想用一个新的测试集重复该过程,只是发现TimeseriesGenerator()需要数据和目标作为输入。 But in this case, I expect to get the targets (ie y_test ) with the predict function. 但是在这种情况下,我希望通过预测函数获得目标(即y_test )。

A simplified version of my training code looks like this: 我的训练代码的简化版本如下所示:

training_generator = TimeseriesGenerator(X_train, y_train, length=timesteps * sampling_rate, sampling_rate=sampling_rate, batch_size=batch_size, shuffle=shuffle_data)
test_generator = TimeseriesGenerator(X_test, y_test, length=timesteps * sampling_rate, sampling_rate=sampling_rate, batch_size=batch_size, shuffle=shuffle_data)

model.fit_generator(generator=training_generator, epochs=epochs, use_multiprocessing=False, verbose=2)
y_test_pred = model.predict_generator(generator=test_generator)

I also thought about writing a custom generator by myself, but then it would be really hard to validate the equivalence between this generator and the official time series generator. 我还考虑过自己编写一个自定义生成器,但是要验证该生成器与官方时间序列生成器之间的等效性确实非常困难。

Is there any way to use TimeseriesGenerator() without giving targets ? 有没有办法使用TimeseriesGenerator()而无需给出targets

Thank you for your help! 谢谢您的帮助!

A simple workaround would be to generate dummy targets, since predict_generator will ignore them: 一个简单的解决方法是生成虚拟目标,因为predict_generator将忽略它们:

X_test =  your new test data
y_dummy = np.zeros((X_test.shape[0], ))
test_generator = TimeseriesGenerator(X_test, y_dummy, length=timesteps * sampling_rate, sampling_rate=sampling_rate, batch_size=batch_size, shuffle=shuffle_data)
y_test_pred = model.predict_generator(generator=test_generator)

You should adjust the shape of y_dummy if your labels are multidimensional. 如果标签是多维的,则应调整y_dummy的形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM