gpt4 book ai didi

machine-learning - Keras 模型预测数列

转载 作者:行者123 更新时间:2023-11-30 08:34:47 25 4
gpt4 key购买 nike

我正在尝试训练 Keras LSTM 模型来预测序列中的下一个数字。

  1. 我的下面的模型有什么问题,当模型无法学习时如何调试
  2. 如何决定使用哪些图层类型
  3. 编译时我应该根据什么选择损失和优化器参数

我的输入训练数据的形状如下 (16000, 10)

[
[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),如下所示

[[14965] [14741] [35831] [12389] ...]

由于 LSTM 提示,我 reshape 了训练/测试数据

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)

这是最终的训练/测试数据形状

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))

总的来说,这真的很难帮助你,因为我们需要一种可以测试的可重现的例子。不过,我的建议如下:

使用神经网络的超参数,例如:激活函数、opt 函数、层数、学习率等。

更新:

强烈建议首先标准化您的数据。

关于machine-learning - Keras 模型预测数列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46001464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com