簡體   English   中英

Keras model.predict總是預測1

[英]Keras model.predict always predicts 1

我正在某個人工智能項目上,我想預測比特幣趨勢,但是在使用我的test_set的Keras的model.predict函數時,預測始終等於1,因此圖中的線始終是直線。

import csv
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from cryptory import Cryptory
from keras.models import Sequential, Model, InputLayer
from keras.layers import LSTM, Dropout, Dense
from sklearn.preprocessing import MinMaxScaler


def 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 test
train_data = training_set[365:]

# Split the data into x and y
x_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 different
model.compile(optimizer="adam", loss="mean_squared_error")  # mse could be used for loss, look into optimiser

model.fit(format_to_3d(x_train), y_train, batch_size=32, epochs=15)

test_set = bitcoin_data
test_set = sc.transform(test_set)
test_data = test_set[:364]

input = test_data
input = 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.8572
Epoch 2/15
1566/1566 [==============================] - 1s 406us/step - loss: 0.8572
Epoch 3/15
1566/1566 [==============================] - 1s 388us/step - loss: 0.8572
Epoch 4/15
1566/1566 [==============================] - 1s 388us/step - loss: 0.8572
Epoch 5/15
1566/1566 [==============================] - 1s 389us/step - loss: 0.8572
Epoch 6/15
1566/1566 [==============================] - 1s 392us/step - loss: 0.8572
Epoch 7/15
1566/1566 [==============================] - 1s 408us/step - loss: 0.8572
Epoch 8/15
1566/1566 [==============================] - 1s 459us/step - loss: 0.8572
Epoch 9/15
1566/1566 [==============================] - 1s 400us/step - loss: 0.8572
Epoch 10/15
1566/1566 [==============================] - 1s 410us/step - loss: 0.8572
Epoch 11/15
1566/1566 [==============================] - 1s 395us/step - loss: 0.8572
Epoch 12/15
1566/1566 [==============================] - 1s 386us/step - loss: 0.8572
Epoch 13/15
1566/1566 [==============================] - 1s 385us/step - loss: 0.8572
Epoch 14/15
1566/1566 [==============================] - 1s 393us/step - loss: 0.8572
Epoch 15/15
1566/1566 [==============================] - 1s 397us/step - loss: 0.8572

我應該打印一個包含實際價格和預測價格的圖,真實價格會正確顯示,但是由於該模型,預測價格只是一條直線。預測僅包含值1。

提前致謝!

您正在嘗試預測價格值,也就是說,您的目的是解決回歸問題而不是分類問題。

但是,在網絡的最后一層( model.add(Dense(units=1, activation="softmax")) ),您只有一個神經元(足以解決回歸問題),但是您已經選擇了使用softmax激活功能。 softmax函數用於多類分類問題,以將輸出標准化為概率分布。 如果您有單個輸出神經元並應用softmax,則最終結果將始終為1.0,因為它是概率分布的唯一參數。

綜上所述,對於回歸問題,您不使用激活函數,因為網絡已打算輸出預測值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM