Keras 函数 API 输入形状错误

Keras Functional API Input shape Errors

我正在使用 Keras Functional 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。

这就是问题所在。因为移动 window/lookback 当然不适用于我的输出。

所以我假设这就是为什么我在 运行 网络时收到此错误:

"Error when checking target: expected dense_6 to have shape (4, 1) but got array with shape (1, 1) "

因为它在我不应用移动时有效 window。但是我需要它,所以我不知道该怎么做。

有人能帮忙吗??

你应该使用 model.summary() 查看你的层和模型的输出形状,然后相应地调整模型 and/or 目标,问题是输出之间存在不匹配模型和目标。

例如,如果您将 LSTMreturn_sequences=True 一起使用,则该 LSTM 的输出是 3D,它被送入仅在最后一个维度上运行的 Dense , 也输出 3D 形状。也许那不是你想要的。您可以将 return_sequences=False 设置为更接近输出的 LSTM 层,或者如果您确实需要它们来输出序列,则只是展平,因此 Dense 层输出 2D 形状,在这种情况下,您应该将目标重塑为 (4000, 1).