Keras model.predict 总是预测为1

我正在进行一个人工智能项目,想要预测比特币的趋势,但在使用 Keras 的 model.predict 函数和我的测试集时,预测结果总是等于1,因此我的图表中的线总是直的。

import csvimport matplotlib.pyplot as pltimport numpy as npimport pandas as pdfrom cryptory import Cryptoryfrom keras.models import Sequential, Model, InputLayerfrom keras.layers import LSTM, Dropout, Densefrom sklearn.preprocessing import MinMaxScalerdef format_to_3d(df_to_reshape):    reshaped_df = np.array(df_to_reshape)    return np.reshape(reshaped_df, (reshaped_df.shape[0], 1, reshaped_df.shape[1]))crypto_data = Cryptory(from_date = "2014-01-01")bitcoin_data = crypto_data.extract_coinmarketcap("bitcoin")sc = MinMaxScaler()for col in bitcoin_data.columns:    if col != "open":        del bitcoin_data[col]training_set = bitcoin_data;training_set = sc.fit_transform(training_set)# Split the data into train, validate and testtrain_data = training_set[365:]# Split the data into x and yx_train, y_train = train_data[:len(train_data)-1], train_data[1:]model = Sequential()model.add(LSTM(units=4, input_shape=(None, 1))) # 128 -- neurons**?# model.add(Dropout(0.2))model.add(Dense(units=1, activation="softmax"))  # activation function could be differentmodel.compile(optimizer="adam", loss="mean_squared_error")  # mse could be used for loss, look into optimisermodel.fit(format_to_3d(x_train), y_train, batch_size=32, epochs=15)test_set = bitcoin_datatest_set = sc.transform(test_set)test_data = test_set[:364]input = test_datainput = sc.inverse_transform(input)input = np.reshape(input, (364, 1, 1))predicted_result = model.predict(input)print(predicted_result)real_value = sc.inverse_transform(input)plt.plot(real_value, color='pink', label='Real Price')plt.plot(predicted_result, color='blue', label='Predicted Price')plt.title('Bitcoin Prediction')plt.xlabel('Time')plt.ylabel('Prices')plt.legend()plt.show()

训练集的表现如下所示:

1566/1566 [==============================] - 3s 2ms/step - loss: 0.8572Epoch 2/151566/1566 [==============================] - 1s 406us/step - loss: 0.8572Epoch 3/151566/1566 [==============================] - 1s 388us/step - loss: 0.8572Epoch 4/151566/1566 [==============================] - 1s 388us/step - loss: 0.8572Epoch 5/151566/1566 [==============================] - 1s 389us/step - loss: 0.8572Epoch 6/151566/1566 [==============================] - 1s 392us/step - loss: 0.8572Epoch 7/151566/1566 [==============================] - 1s 408us/step - loss: 0.8572Epoch 8/151566/1566 [==============================] - 1s 459us/step - loss: 0.8572Epoch 9/151566/1566 [==============================] - 1s 400us/step - loss: 0.8572Epoch 10/151566/1566 [==============================] - 1s 410us/step - loss: 0.8572Epoch 11/1566/1566 [==============================] - 1s 395us/step - loss: 0.8572Epoch 12/151566/1566 [==============================] - 1s 386us/step - loss: 0.8572Epoch 13/151566/1566 [==============================] - 1s 385us/step - loss: 0.8572Epoch 14/151566/1566 [==============================] - 1s 393us/step - loss: 0.8572Epoch 15/151566/1566 [==============================] - 1s 397us/step - loss: 0.8572

我应该打印一个包含真实价格和预测价格的图表,真实价格显示正常,但预测价格只是一条直线,因为 model.predict 总是返回值1。

提前感谢!


回答:

您试图预测价格值,也就是说,您要解决的是回归问题,而不是分类问题。

然而,在您的网络的最后一层(model.add(Dense(units=1, activation="softmax"))),您使用了一个神经元(这对于回归问题来说是合适的),但您选择了使用 softmax 激活函数。softmax 函数用于多类分类问题,将输出标准化为概率分布。如果您只有一个输出神经元并应用 softmax,最终结果将始终为1.0,因为它是概率分布的唯一参数。

总之,对于回归问题,您不应该使用激活函数,因为网络已经输出预测值。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注