Keras model.predict总是预测1

时间:2019-04-17 15:19:49

标签: python keras artificial-intelligence

我正在某个人工智能项目上,我想预测比特币趋势,但是在使用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。

谢谢!

1 个答案:

答案 0 :(得分:2)

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

但是,在网络的最后一层(javax.validation.constraints.NotBlank)中,您只有一个神经元(这足以解决回归问题),但是您已选择使用softmax激活函数。 softmax函数用于多类分类问题,以将输出标准化为概率分布。如果您有单个输出神经元并应用softmax,则最终结果将始终为1.0,因为它是概率分布的唯一参数。

总而言之,对于回归问题,您不使用激活函数,因为网络已准备好输出预测值。