【发布时间】:2022-12-23 02:50:07
【问题描述】:
我刚刚实现了一个 LSTM,
但我不确定我是否正确解释了结构。
在这种情况下 testPredict = model.predict(Xtest) 是序列的最后一个值,因此最终(在反转 MinMaxscaler 之后)变量 last_value = (testPredict[-1]) 是关于未来的预测吗?
from IPython.core.debugger import set_trace
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import time
import yfinance as yf
import sklearn
from sklearn.preprocessing import MinMaxScaler
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import LSTM, Dense, Dropout, Flatten
from sklearn.metrics import mean_squared_error
from keras.layers import ConvLSTM2D
from keras.layers import Bidirectional
from keras.models import model_from_json
df = yf.download(tickers="BTC-USD", period="20wk", interval="60m")
df = df[["Close"]]
df["returns"] = df.Close.pct_change()
df["log_returns"] = np.log(1 + df["returns"])
df.dropna(inplace=True)
X = df[["Close", "log_returns"]].values
scaler = MinMaxScaler(feature_range=(0, 1)).fit(X)
X_scaled = scaler.transform(X)
y = [x[0] for x in X_scaled]
split = int(len(X_scaled) * 0.8)
X_train = X_scaled[:split]
X_test = X_scaled[split : len(X_scaled)]
y_train = y[:split]
y_test = y[split : len(y)]
assert len(X_train) == len(y_train)
assert len(X_test) == len(y_test)
n = 24 #analyze the last 24 prices
Xtrain = []
ytrain = []
Xtest = []
ytest = []
for i in range(n, len(X_train)):
Xtrain.append(X_train[i - n : i, : X_train.shape[1]])
ytrain.append(y_train[i])
for i in range(n, len(X_test)):
Xtest.append(X_test[i - n : i, : X_test.shape[1]])
ytest.append(y_test[i])
val = np.array(ytrain[0])
val = np.c_[val, np.zeros(val.shape)]
scaler.inverse_transform(val)
Xtrain, ytrain = (np.array(Xtrain), np.array(ytrain))
Xtrain = np.reshape(Xtrain, (Xtrain.shape[0], Xtrain.shape[1], Xtrain.shape[2]))
Xtest, ytest = (np.array(Xtest), np.array(ytest))
Xtest = np.reshape(Xtest, (Xtest.shape[0], Xtest.shape[1], Xtest.shape[2]))
model = Sequential()
model.add(LSTM(8, return_sequences=True, input_shape=(Xtrain.shape[1], Xtrain.shape[2])))
#model.add(Bidirectional(LSTM(8, return_sequences=True, input_shape=(Xtrain.shape[1], Xtrain.shape[2]))))
model.add(LSTM(4))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(Xtrain, ytrain, epochs=100, validation_data=(Xtest, ytest), batch_size=16, verbose=1)
trainPredict = model.predict(Xtrain)
testPredict = model.predict(Xtest)
trainPredict = np.c_[trainPredict, np.zeros(trainPredict.shape)]
testPredict = np.c_[testPredict, np.zeros(testPredict.shape)]
trainPredict = scaler.inverse_transform(trainPredict)
trainPredict = [x[0] for x in trainPredict]
testPredict = scaler.inverse_transform(testPredict)
testPredict = [x[0] for x in testPredict]
trainScore = mean_squared_error([x[0][0] for x in Xtrain], trainPredict, squared=False)
#print("Train Score: %.2f RMSE" % (trainScore))
testScore = mean_squared_error([x[0][0] for x in Xtest], testPredict, squared=False)
#print("Test Score: %.2f RMSE" % (testScore))
########################################################################################################################
last_value = (testPredict[-1])
【问题讨论】:
标签: tensorflow keras lstm