我正在使用 Keras 功能 API 进行具有多变量输入和单输出的时间序列预测:(x_1, x_2, y)
.
因此,我有两个具有多个层的网络分支,我在处理后将它们连接起来。
代码如下所示:
# Define Model Input Sets
inputA = Input(shape=(4, 1))
inputB = Input(shape=(4, 1))
# Build Model Branch 1
branch_1 = layers.LSTM(8, activation='tanh', dropout=0.2, return_sequences=True)(inputA)
branch_1 = layers.Dense(8, activation='tanh')(branch_1)
branch_1 = layers.Dropout(0.2)(branch_1)
branch_1 = layers.LSTM(6, activation='tanh', dropout=0.2, return_sequences=True)(branch_1)
branch_1 = layers.Dense(6, activation='tanh')(branch_1)
branch_1 = layers.Dropout(0.2)(branch_1)
branch_1 = Model(inputs=inputA, outputs=branch_1)
# Build Model Branch 2
branch_2 = layers.LSTM(8, activation='tanh', dropout=0.2, return_sequences=True)(inputB)
branch_2 = layers.Dense(8, activation='tanh')(branch_2)
branch_2 = layers.Dropout(0.2)(branch_2)
branch_2 = layers.LSTM(6, activation='tanh', dropout=0.2, return_sequences=True)(branch_2)
branch_2 = layers.Dense(6, activation='tanh')(branch_2)
branch_2 = layers.Dropout(0.2)(branch_2)
branch_2 = Model(inputs=inputB, outputs=branch_2)
# Combine Model Branches
combined = layers.concatenate([branch_1.output, branch_2.output])
# apply a FC layer and then a regression prediction on the combined outputs
comb = layers.Dense(2, activation='tanh')(combined)
comb = layers.Dense(1, activation="linear")(comb)
# Accept the inputs of the two branches and then output a single value
model = Model(inputs=[branch_1.input, branch_2.input], outputs=comb)
model.compile(loss='mae', optimizer='adam', metrics=['accuracy'])
# Training
model.fit([x_1_train, x_2_train], y_train, epochs=ep, batch_size=bs)
现在,由于 LSTM 层需要一个 3D numpy 数组,我相应地重塑了我的输入数据:
# Data
x_1 = data[['Temp']].values
x_2 = data[['Precip']].values
y = data[['GWL']].values
# Reshape Data
x_1 = np.reshape(x_1, (len(x_1), x_1.shape[1], 1)) # 3D tensor with shape (batch_size, timesteps, input_dim)
x_2 = np.reshape(x_2, (len(x_2), x_2.shape[1], 1))
y = np.reshape(y, (len(y), 1, 1))
现在我的输入数据是形状:
x_1: (4000, 4, 1)
x_2: (4000, 4, 1)
y: (4000, 1, 1)
因为我还在输入数据上使用了 4 个时间步长的移动窗口/回溯。
这就是问题所在。因为移动window/lookback
不适用于我的输出,当然。
所以我认为这就是我在运行网络时收到此错误的原因:
“检查目标时出错:预期 dense_6 的形状为 (4, 1),但数组的形状为 (1, 1)”
因为当我不应用移动窗口时它可以工作。但是我需要它,所以我不知道该怎么做。
有人可以帮忙吗??