Keras模型预测数字序列 [英] Keras model to predict number sequence
问题描述
我正在尝试训练Keras LSTM模型以预测序列中的下一个数字.
I am trying to train Keras LSTM model to predict next number in a sequence.
- 下面的模型有什么问题,当模型不学习时如何调试
- 如何确定要使用的图层类型
- 在编译时应基于什么选择损失参数和优化器参数
我输入的训练数据的形状如下(16000,10)
My input training data is of shape (16000, 10) like below
[
[14955 14956 14957 14958 14959 14960 14961 14962 14963 14964]
[14731 14732 14733 14734 14735 14736 14737 14738 14739 14740]
[35821 35822 35823 35824 35825 35826 35827 35828 35829 35830]
[12379 12380 12381 12382 12383 12384 12385 12386 12387 12388]
...
]
相应的输出训练数据具有如下所示的形状(16000,1)
Corresponding output training data is of shape (16000, 1) like below
[[14965] [14741] [35831] [12389] ...]
由于LSTM抱怨,我重塑了训练/测试数据
As LSTM is complaining, I reshaped training/test data
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
这是最终的训练/测试数据形状
Here is final training/test data shape
Total Samples: 20000
X_train: (16000, 10, 1)
y_train: (16000, 1)
X_test: (4000, 10, 1)
y_test: (4000, 1)
这是我的模特
# Model configuration
epochs = 2
batch_size = 32
hidden_neurons = 100
output_size = 1
# Create the model
model = Sequential()
model.add(LSTM(hidden_neurons, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(output_size))
model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size)
scores = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0)
print("Model Accuracy: %.2f%%" % (scores[1]*100))
这是我的输出
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_3 (LSTM) (None, 100) 40800
_________________________________________________________________
dense_3 (Dense) (None, 1) 101
=================================================================
Total params: 40,901
Trainable params: 40,901
Non-trainable params: 0
_________________________________________________________________
None
Epoch 1/2
16000/16000 [==============================] - 11s - loss: 533418575.3600 - acc: 0.0000e+00
Epoch 2/2
16000/16000 [==============================] - 10s - loss: 532474289.7280 - acc: 6.2500e-05
Model Accuracy: 0.00%
推荐答案
尝试以下代码:
epochs = 30
batch_size = 64
hidden_neurons = 32
output_size = 1
# Create the model
model = Sequential()
model.add(LSTM(hidden_neurons, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(output_size, activation = 'elu'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size)
scores = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0)
print("Model Accuracy: %.2f%%" % (scores[1]*100))
一般来说,要帮助您真的很困难,因为我们需要一种可以测试的可复制示例.但是,这是我的建议:
in general, it is really hard to help you, because we need a kind of reproducible example which we can test. However, here are my advises:
使用您的NN的超参数,例如:激活函数,opt函数,层数,学习率等.
play with hyper parameters of your NN, such as: activation functions, opt function, number of layers, learning rate and so on.
更新:
强烈建议先将您的数据标准化.
It is highly advisable to normalize your data first.
这篇关于Keras模型预测数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!