首页 > 解决方案 > 为什么我的准确率总是0.0000e+00,而且损失巨大?

问题描述

我对机器学习很陌生,这是我第一个使用 tensorflow 和 keras 的项目。我试图预测给定数据集的数值,但我的模型正在运行。

x_train, x_test, y_train, y_test = train_test_split(dates, prices, test_size=0.33)


x_train = np.reshape(x_train,(x_train.shape[0], 1, x_train.shape[1]))
model = Sequential()
model.add(LSTM(30, return_sequences=True, input_shape= (x_train.shape[0], 1)))
# model.add(Flatten())
model.add(Dropout(0.25))
model.add(Dense(100,activation = 'relu'))
model.add(Dropout(0.25))
model.add(Dense(1,activation='softmax'))


model.compile(optimizer='adam',loss='mean_squared_error', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=5)

这是我上面的模型,但每当它运行时,它都会在下面显示:

Epoch 1/5
WARNING:tensorflow:Model was constructed with shape (None, 1686, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 1686, 1), dtype=tf.float32, name='lstm_input'), name='lstm_input', description="created by layer 'lstm_input'"), but it was called on an input with incompatible shape (None, 1, 1).
WARNING:tensorflow:Model was constructed with shape (None, 1686, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 1686, 1), dtype=tf.float32, name='lstm_input'), name='lstm_input', description="created by layer 'lstm_input'"), but it was called on an input with incompatible shape (None, 1, 1).
53/53 [==============================] - 2s 2ms/step - loss: 1314.1159 - accuracy: 0.0000e+00
Epoch 2/5
53/53 [==============================] - 0s 2ms/step - loss: 1332.1348 - accuracy: 0.0000e+00
Epoch 3/5
53/53 [==============================] - 0s 2ms/step - loss: 1307.5851 - accuracy: 0.0000e+00
Epoch 4/5
53/53 [==============================] - 0s 2ms/step - loss: 1327.0625 - accuracy: 0.0000e+00
Epoch 5/5
53/53 [==============================] - 0s 2ms/step - loss: 1314.4220 - accuracy: 0.0000e+00
<tensorflow.python.keras.callbacks.History at 0x7f1cdfc56668>

有谁知道如何解决这个问题并提高准确性和损失?非常感谢您的帮助。

标签: pythontensorflowmachine-learningkeras

解决方案


如果你试图预测一个数值,那么你不是在做分类问题,而是在做回归问题。因此,具有 1 个神经元的密集层应该没有激活功能。


推荐阅读