首页 文章

Keras模型预测数字序列

提问于
浏览
3

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

  • 我的模型下面有什么问题,如何在模型不学习时进行调试

  • 如何确定要使用的图层类型

  • 在编译时我应该在什么基础上选择损失和优化器参数

我的输入训练数据形状如下(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抱怨,我重塑了训练/测试数据

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%

2 回答

  • 1

    试试这段代码:

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

    一般来说,真的很难帮助你,因为我们需要一种可以重复的例子来测试 . 但是,这是我的建议:

    使用您的NN的超参数,例如:激活功能,选择功能,层数,学习速率等 .

    更新:

    建议首先规范化您的数据 .

  • 2

    精度不是衡量模型性能的正确方法 . 你在这里尝试做的更多的是 regression 任务而不是 classification 任务 . 从你的损失函数可以看出同样的情况,你正在使用' mean_squared_error ' rather than something like ' categorical_crossentropy ' .

    此外,50个时期的训练时间太少 . 如果您查看日志(在原始问题中),您会看到每个时期的损失减少 . 你将需要继续训练更多的时代,直到你看到损失已经稳定并且没有进一步减少 .

    第三,在将数据传递给fit函数之前,您必须对数据进行标准化 . 这些值非常大,如果没有标准化,算法可能无法收敛 .

    如果您仍然需要解决此问题,并需要更多帮助,请在评论中告诉我,以便我可以帮助您完成代码 .

相关问题