我对机器学习很陌生,这是我使用 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>
有谁知道如何解决这个问题并提高准确性和损失? 非常感谢帮助。
答案 0 :(得分:0)
如果您试图预测一个数值,那么您不是在做分类问题,而是在做回归问题。因此,具有 1 个神经元的密集层应该没有激活函数。
答案 1 :(得分:0)
用这个替换你最后一层 Sequential 模型:
model.add(Dense(1, kernel_initializer='normal'))
回归任务不需要softmax作为激活函数,因为softmax主要用于多类分类