Keras LSTM 未训练
Keras LSTM not training
我有一个 LSTM,其输出完全相同。我该如何解决这个问题?以下是参数。我很想得到一个笼统的答案,因为这将帮助我了解解决方案,以防我再次看到它。
batch_size = 32
X_train.shape, Y_train.shape, X_test.shape, Y_test.shape
>((1920, 30, 5), (1920, 6), (1696, 30, 5), (1696, 6))
data_dim = X_train.shape[2]
timesteps = X_train.shape[1]
# Expected input batch shape: (batch_size, timesteps, data_dim)
# Note that we have to provide the full batch_input_shape since the network is stateful.
# the sample of index i in batch k is the follow-up for the sample i in batch k-1.
model = Sequential()
model.add(LSTM(32,
return_sequences=True,
stateful=True,
kernel_regularizer=regularizers.l2(0.0001),
batch_input_shape=(batch_size, timesteps, data_dim)))
model.add(Dropout(0.4))
model.add(LSTM(32, return_sequences=True,
kernel_regularizer=regularizers.l2(0.0001),
stateful=True))
model.add(Dropout(0.4))
model.add(LSTM(32, stateful=True))
model.add(Dropout(0.4))
model.add(Dense(6, activation='softmax', use_bias=True))
rms = RMSprop(lr=0.001)
model.compile(loss='categorical_crossentropy',
optimizer=rms,
metrics=['accuracy'])
history = model.fit(X_train, Y_train,
batch_size=batch_size,
epochs=5,
shuffle=False,
validation_data=(X_test, Y_test))
训练后,我得到以下输出:
0b 1b 2b 3b 4b 5b
2017-06-30 0.077203 0.180573 0.314528 0.287455 0.110213 0.030026
2017-07-03 0.077225 0.180570 0.314542 0.287430 0.110204 0.030029
2017-07-04 0.077220 0.180586 0.314541 0.287423 0.110207 0.030023
2017-07-05 0.077193 0.180622 0.314523 0.287426 0.110221 0.030015
2017-07-06 0.077125 0.180695 0.314496 0.287435 0.110257 0.029992
它们都非常非常相似:(
编辑:忘记提及我使用了 sklearn MinMaxScaler 并将数据缩放到 (-7,7),因为这在过去似乎是有效的。这是正确的做法吗?
别担心;这是一个常见问题,要解决它,您必须为您的网络找到最佳参数。
很遗憾,我无法告诉您如何修复您的 aNN,但这里有一些想法,您可以尝试:
- 将激活函数改为ELU或ReLu
- 删除丢失并添加批量归一化
- 改变层数(尽量小)
- 增加批量大小
- 降低正则化系数
- @jdehesa 建议训练更长时间
- 添加更多数据...
我有一个 LSTM,其输出完全相同。我该如何解决这个问题?以下是参数。我很想得到一个笼统的答案,因为这将帮助我了解解决方案,以防我再次看到它。
batch_size = 32
X_train.shape, Y_train.shape, X_test.shape, Y_test.shape
>((1920, 30, 5), (1920, 6), (1696, 30, 5), (1696, 6))
data_dim = X_train.shape[2]
timesteps = X_train.shape[1]
# Expected input batch shape: (batch_size, timesteps, data_dim)
# Note that we have to provide the full batch_input_shape since the network is stateful.
# the sample of index i in batch k is the follow-up for the sample i in batch k-1.
model = Sequential()
model.add(LSTM(32,
return_sequences=True,
stateful=True,
kernel_regularizer=regularizers.l2(0.0001),
batch_input_shape=(batch_size, timesteps, data_dim)))
model.add(Dropout(0.4))
model.add(LSTM(32, return_sequences=True,
kernel_regularizer=regularizers.l2(0.0001),
stateful=True))
model.add(Dropout(0.4))
model.add(LSTM(32, stateful=True))
model.add(Dropout(0.4))
model.add(Dense(6, activation='softmax', use_bias=True))
rms = RMSprop(lr=0.001)
model.compile(loss='categorical_crossentropy',
optimizer=rms,
metrics=['accuracy'])
history = model.fit(X_train, Y_train,
batch_size=batch_size,
epochs=5,
shuffle=False,
validation_data=(X_test, Y_test))
训练后,我得到以下输出:
0b 1b 2b 3b 4b 5b
2017-06-30 0.077203 0.180573 0.314528 0.287455 0.110213 0.030026
2017-07-03 0.077225 0.180570 0.314542 0.287430 0.110204 0.030029
2017-07-04 0.077220 0.180586 0.314541 0.287423 0.110207 0.030023
2017-07-05 0.077193 0.180622 0.314523 0.287426 0.110221 0.030015
2017-07-06 0.077125 0.180695 0.314496 0.287435 0.110257 0.029992
它们都非常非常相似:(
编辑:忘记提及我使用了 sklearn MinMaxScaler 并将数据缩放到 (-7,7),因为这在过去似乎是有效的。这是正确的做法吗?
别担心;这是一个常见问题,要解决它,您必须为您的网络找到最佳参数。
很遗憾,我无法告诉您如何修复您的 aNN,但这里有一些想法,您可以尝试:
- 将激活函数改为ELU或ReLu
- 删除丢失并添加批量归一化
- 改变层数(尽量小)
- 增加批量大小
- 降低正则化系数
- @jdehesa 建议训练更长时间
- 添加更多数据...