Keras LSTM 密集层多维输入
Keras LSTM dense layer multidimensional input
我正在尝试创建一个 keras LSTM 来预测时间序列。我的 x_train 形状像 3000,15,10(示例、时间步长、特征),y_train 像 3000,15,1 我正在尝试构建多对多模型(每个 10 个输入特征序列生成 1 个输出/序列)。
我使用的代码是这样的:
model = Sequential()
model.add(LSTM(
10,
input_shape=(15, 10),
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
100,
return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))
model.compile(loss="mse", optimizer="rmsprop")
model.fit(
X_train, y_train,
batch_size=512, nb_epoch=1, validation_split=0.05)
但是,我在使用时无法拟合模型:
model.add(Dense(1, activation='linear'))
>> Error when checking model target: expected dense_1 to have 2 dimensions, but got array with shape (3000, 15, 1)
或以这种方式格式化时:
model.add(Dense(1))
model.add(Activation("linear"))
>> Error when checking model target: expected activation_1 to have 2 dimensions, but got array with shape (3000, 15, 1)
我已经尝试在添加致密层之前展平模型 (model.add(Flatten())
),但这只是给了我 ValueError: Input 0 is incompatible with layer flatten_1: expected ndim >= 3, found ndim=2
。这让我感到困惑,因为我认为我的数据实际上是 3 维的,不是吗?
代码源自https://github.com/Vict0rSch/deep_learning/tree/master/keras/recurrent
由于您更新了您的 keras 版本并且您的错误消息发生了变化,以下是在我的机器上可以使用的版本 (Keras 2.0.x)
这个有效:
model = Sequential()
model.add(LSTM(10,input_shape=(15, 10), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM( 100, return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))
这也有效:
model = Sequential()
model.add(LSTM(10,input_shape=(15, 10), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM( 100, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(1,return_sequences=True, activation='linear'))
测试:
x = np.ones((3000,15,10))
y = np.ones((3000,15,1))
编译和训练:
model.compile(optimizer='adam',loss='mse')
model.fit(x,y,epochs=4,verbose=2)
在 keras < 2.0
的情况下:您需要使用 TimeDistributed
包装器以便按元素将其应用于序列。
在 keras >= 2.0
的情况下:默认情况下按元素应用 Dense
图层。
我正在尝试创建一个 keras LSTM 来预测时间序列。我的 x_train 形状像 3000,15,10(示例、时间步长、特征),y_train 像 3000,15,1 我正在尝试构建多对多模型(每个 10 个输入特征序列生成 1 个输出/序列)。
我使用的代码是这样的:
model = Sequential()
model.add(LSTM(
10,
input_shape=(15, 10),
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
100,
return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))
model.compile(loss="mse", optimizer="rmsprop")
model.fit(
X_train, y_train,
batch_size=512, nb_epoch=1, validation_split=0.05)
但是,我在使用时无法拟合模型:
model.add(Dense(1, activation='linear'))
>> Error when checking model target: expected dense_1 to have 2 dimensions, but got array with shape (3000, 15, 1)
或以这种方式格式化时:
model.add(Dense(1))
model.add(Activation("linear"))
>> Error when checking model target: expected activation_1 to have 2 dimensions, but got array with shape (3000, 15, 1)
我已经尝试在添加致密层之前展平模型 (model.add(Flatten())
),但这只是给了我 ValueError: Input 0 is incompatible with layer flatten_1: expected ndim >= 3, found ndim=2
。这让我感到困惑,因为我认为我的数据实际上是 3 维的,不是吗?
代码源自https://github.com/Vict0rSch/deep_learning/tree/master/keras/recurrent
由于您更新了您的 keras 版本并且您的错误消息发生了变化,以下是在我的机器上可以使用的版本 (Keras 2.0.x)
这个有效:
model = Sequential()
model.add(LSTM(10,input_shape=(15, 10), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM( 100, return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))
这也有效:
model = Sequential()
model.add(LSTM(10,input_shape=(15, 10), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM( 100, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(1,return_sequences=True, activation='linear'))
测试:
x = np.ones((3000,15,10))
y = np.ones((3000,15,1))
编译和训练:
model.compile(optimizer='adam',loss='mse')
model.fit(x,y,epochs=4,verbose=2)
在 keras < 2.0
的情况下:您需要使用 TimeDistributed
包装器以便按元素将其应用于序列。
在 keras >= 2.0
的情况下:默认情况下按元素应用 Dense
图层。