繁体   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