我正在寻找建立一个时间序列模型(使用 TCN 或 LSTM)不同的系列,每个系列都有系列特定功能. 我的输入数组是维度, 在哪里是时间步数。
我也有特点,在所有时间序列中都是恒定的。具体来说,假设我正在使用天气数据预测城市级别的冰淇淋销售,并且我还想使用 GDP 增长作为预测指标。GDP增长是全国性的。一个简单的方法可能是增加和, 的维度加 1. 那么我下一个时期的预测输出将是,这是不好的,因为每个城市都有一个 GDP 预测,而实际上,各个城市的 GDP 增长是普遍的(在全国范围内衡量时)。我想我想要两个输出——一个形状, 和另一个形状,一个标量(如果有维度)。
这是一个虚拟示例,其中时间是一个全局变量,但它在所有系列中都是恒定的。(让我们暂时假设时间不是外生的,而是包含在多元预测中的东西)。
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model
from keras.layers import Input, Conv1D, Dense
from keras.optimizers import Adam
time = np.array(range(100))
brk = np.array((time>40) & (time < 60)).reshape(100,1)
B = np.array([5, -5]).reshape(1,2)
np.dot(brk, B)
y = np.c_[np.sin(time), np.sin(time)] + np.random.normal(scale = .2, size=(100,2))+ np.dot(brk, B)
plt.plot(time, y[:,0])
plt.plot(time, y[:,1])
# Temporal convolutional network
n_filters = 2
filter_width = 3
dilation_rates = [2**i for i in range(5)]
inp = Input(shape=(None, 2))
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(2)(x)
model = Model(inputs = inp, outputs = x)
model.compile(optimizer = Adam(), loss='mean_squared_error')
model.summary()
def shift5(arr, num, fill_value=np.nan):
result = np.empty_like(arr)
if num > 0:
result[:num] = fill_value
result[num:] = arr[:-num]
elif num < 0:
result[num:] = fill_value
result[:num] = arr[-num:]
else:
result = arr
return result
X = y.reshape(2,100,1)
X = np.concatenate([X, np.concatenate([time.reshape(100,1),time.reshape(100,1)], axis = 1).reshape(2,100, 1)],
axis = 2)
X_tr = X[:,:95,:]
X_te = X[:,5:,:]
history = model.fit(X_tr, X_te,
batch_size=2,
epochs=10,
verbose = 1)
我将如何修改此架构以具有两个输入和两个输出,输入和输出都具有本地和全局组件?
