【发布时间】:2019-08-10 07:24:49
【问题描述】:
我遇到了一个问题,我想用多个时间序列预测一个时间序列。我的输入是(batch_size, time_steps, features),我的输出应该是(1, time_steps, features)
我不知道如何平均 N。
这是一个虚拟示例。首先,输出是200个时间序列的线性函数的虚拟数据:
import numpy as np
time = 100
N = 2000
dat = np.zeros((N, time))
for i in range(time):
dat[i,:] = np.sin(list(range(time)))*np.random.normal(size =1) + np.random.normal(size = 1)
y = dat.T @ np.random.normal(size = N)
现在我将定义一个时间序列模型(使用一维卷积网络):
from keras.models import Model
from keras.layers import Input, Conv1D, Dense, Lambda
from keras.optimizers import Adam
from keras import backend as K
n_filters = 2
filter_width = 3
dilation_rates = [2**i for i in range(5)]
inp = Input(shape=(None, 1))
x = inp
for dilation_rate in dilation_rates:
x = Conv1D(filters=n_filters,
kernel_size=filter_width,
padding='causal',
activation = "relu",
dilation_rate=dilation_rate)(x)
x = Dense(1)(x)
model = Model(inputs = inp, outputs = x)
model.compile(optimizer = Adam(), loss='mean_squared_error')
model.predict(dat.reshape(N, time, 1)).shape
Out[43]: (2000, 100, 1)
输出的形状错误!接下来,我尝试使用平均层,但出现了这个奇怪的错误:
def av_over_batches(x):
x = K.mean(x, axis = 0)
return(x)
x = Lambda(av_over_batches)(x)
model = Model(inputs = inp, outputs = x)
model.compile(optimizer = Adam(), loss='mean_squared_error')
model.predict(dat.reshape(N, time, 1)).shape
Traceback (most recent call last):
File "<ipython-input-3-d43ccd8afa69>", line 4, in <module>
model.predict(dat.reshape(N, time, 1)).shape
File "/home/me/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1169, in predict
steps=steps)
File "/home/me/.local/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 302, in predict_loop
outs[i][batch_start:batch_end] = batch_out
ValueError: could not broadcast input array from shape (100,1) into shape (32,1)
32 来自哪里? (顺便说一句,我在真实数据中得到了相同的数字,而不仅仅是在 MWE 中)。
但主要问题是:如何构建一个在输入批次维度上取平均值的网络?
【问题讨论】:
标签: python tensorflow machine-learning keras time-series