python - 为什么我的准确率总是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>
有谁知道如何解决这个问题并提高准确性和损失?非常感谢您的帮助。
解决方案
如果你试图预测一个数值,那么你不是在做分类问题,而是在做回归问题。因此,具有 1 个神经元的密集层应该没有激活功能。
推荐阅读
- javascript - 我无法理解这个 TabBarCustomButton 函数结构的部分内容,谁能解释一下?
- python - 使用时出现AttributeError;在 IBM Cloud Notebook 中插入代码 pandas DataFrame。该怎么办?
- python-3.x - 跳过空模块 - Sphinx Autodoc
- django - 删除组合模型的视图
- cluster-computing - 如何在 DBSCAN 中估计 k-dist 中的 K 参数以进行 eps 和 minpts 估计?
- python - 我想在 Django 中使用在 postgreSQL 中自动创建的 user_auth 表创建一个关系数据库
- reactjs - 使用redux时取消一个useEffect中的所有订阅和异步任务
- python - 为什么用 Z-Score 去除异常值仍然会遗漏一些值作为异常值?
- javascript - 无法使用时间戳更新布尔猫鼬对象
- sql - 报表显示分配给的用户及其各自的角色,同时显示分配给特定用户的角色数量