我正在进行一个人工智能项目,想要预测比特币的趋势,但在使用 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,因为它是概率分布的唯一参数。
总之,对于回归问题,您不应该使用激活函数,因为网络已经输出预测值。