我正在使用keras __mod__
函数来创建样本和目标,但想在输出中添加时间延迟(即,将目标移动一些时间步长)。我在文档中看到没有本地支持添加延迟。如何手动添加?到目前为止,这是我的代码:
TimeseriesGenerator
y的第一批输出为import numpy as np
from keras.preprocessing.sequence import TimeseriesGenerator
X = np.arange(100000).reshape(10000, 10)
y = np.arange(50000).reshape(10000, 5)
timesteps = 50
step = 1
delay = 20
batch_size = 20
gener = TimeseriesGenerator(X, y, timesteps, sampling_rate=1,
stride=step, start_index=0, end_index=None, shuffle=False, reverse=False, batch_size=batch_size)
,但我希望输出偏移20个时间步长。因此,第一批的y实际上应该以
[[250. 251. 252. 253. 254.], [...]
答案 0 :(得分:0)
您可以例如通过使用numpy.roll覆盖TimeseriesGenerator类以实现此功能
class TSGen(keras.preprocessing.sequence.TimeseriesGenerator):
def __init__(self, delay, **kwargs):
self.targets = kwargs['targets']
self.delay = delay
kwargs['targets'] = np.roll(kwargs['targets'], np.negative(delay))
super().__init__(**kwargs)
def __len__(self):
return int(np.ceil((len(self.targets) - self.delay - self.length) / self.batch_size))
delay参数指定跳过多少步。
a1=np.array([[1,2,3,4,5,6,7,8,9]]).reshape(-1,1)
a2=np.array([[10,20,30,40,50,60,70,80,90]]).reshape(-1,1)
b = np.concatenate((a1,a2), axis=1)
在b上使用标准的TimeseriesGenerator将会产生
[[1 2 3]] => [40]
[[2 3 4]] => [50]
[[3 4 5]] => [60]
[[4 5 6]] => [70]
[[5 6 7]] => [80]
[[6 7 8]] => [90]
使用新的类,例如这样
ts_gen = TSGen(2, data=b[:,0],targets=b[:,1],length=3,batch_size=1,stride=1,sampling_rate=1)
将产生以下内容:
[[1 2 3]] => [60]
[[2 3 4]] => [70]
[[3 4 5]] => [80]
[[4 5 6]] => [90]