【问题标题】:What is the prediction value of this LSTM neural network?这个LSTM神经网络的预测值是多少?
【发布时间】: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


    【解决方案1】:

    我给大家看一张图片结果来理解,不改变标签和值符合条件的事实,而是颠倒顺序来验证结果。工作模型需要足够的数据。

    从数据集中以相反的顺序为每个演员选择随机图片 ~ 6-7 以验证模型在预测输入的新收入时是否有效。

    对称形状可以告诉你响应的动作,但它不是未来的预测,在他的情况下,他选择与分数或 softmax 最匹配但没有 softmax 的预测结果,他也可以使用 np.argmax 处理序列输出(您需要查看模型输出)

    预测实时数据,您需要在创建某些报告时输入变量和历史数据,模型会学习范围内值的变化!

    [ 样本 ]:通过预测目标的示例,您可以直观地看到学习网络可以双向正确地做到这一点,并且您可以使用具有新输入反向顺序的网络是不好的检查,只有您没有任何数据。

    import os
    from os.path import exists
    
    import tensorflow as tf
    import tensorflow_io as tfio
    
    import matplotlib.pyplot as plt
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
    None
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
    config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
    print(physical_devices)
    print(config)
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Variables
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    PATH = os.path.join('F:\datasets\downloads\Actors\train\Pikaploy', '*.tif')
    PATH_2 = os.path.join('F:\datasets\downloads\Actors\train\Candidt Kibt', '*.tif')
    files = tf.data.Dataset.list_files(PATH)
    files_2 = tf.data.Dataset.list_files(PATH_2)
    
    list_file = []
    list_file_actual = []
    list_label = []
    list_label_actual = [ 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt' ]
    for file in files.take(5):
        image = tf.io.read_file( file )
        image = tfio.experimental.image.decode_tiff(image, index=0)
        list_file_actual.append(image)
        image = tf.image.resize(image, [32,32], method='nearest')
        list_file.append(image)
        list_label.append(1)
        
    for file in files_2.take(5):
        image = tf.io.read_file( file )
        image = tfio.experimental.image.decode_tiff(image, index=0)
        list_file_actual.append(image)
        image = tf.image.resize(image, [32,32], method='nearest')
        list_file.append(image)
        list_label.append(9)
    
    checkpoint_path = "F:\models\checkpoint\" + os.path.basename(__file__).split('.')[0] + "\TF_DataSets_01.h5"
    checkpoint_dir = os.path.dirname(checkpoint_path)
    loggings = "F:\models\checkpoint\" + os.path.basename(__file__).split('.')[0] + "\loggings.log"
    
    if not exists(checkpoint_dir) : 
        os.mkdir(checkpoint_dir)
        print("Create directory: " + checkpoint_dir)
        
    log_dir = checkpoint_dir
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    DataSet
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    dataset = tf.data.Dataset.from_tensor_slices((tf.constant(tf.cast(list_file, dtype=tf.int64), shape=(10, 1, 32, 32, 4), dtype=tf.int64), 
        tf.constant(list_label, shape=(10, 1, 1), dtype=tf.int64)))
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Model Initialize
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    model = tf.keras.models.Sequential([
        tf.keras.layers.InputLayer(input_shape=( 32, 32, 4 )),
        tf.keras.layers.Normalization(mean=3., variance=2.),
        tf.keras.layers.Normalization(mean=4., variance=6.),
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D((2, 2)),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Reshape((128, 225)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(192, activation='relu'),
        tf.keras.layers.Dense(10),
    ])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Optimizer
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    optimizer = tf.keras.optimizers.Nadam(
        learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
        name='Nadam'
    )
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Loss Fn
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
    lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=False,
        reduction=tf.keras.losses.Reduction.AUTO,
        name='sparse_categorical_crossentropy'
    )
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Model Summary
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : FileWriter
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    if exists(checkpoint_path) :
        model.load_weights(checkpoint_path)
        print("model load: " + checkpoint_path)
        input("Press Any Key!")
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Training
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    history = model.fit( dataset, batch_size=100, epochs=50 )
    
    plt.figure(figsize=(5,2))
    plt.title("Actors recognitions")
    for i in range(len(list_file)):
        img = tf.keras.preprocessing.image.array_to_img(
            list_file[i],
            data_format=None,
            scale=True
        )
        img_array = tf.keras.preprocessing.image.img_to_array(img)
        img_array = tf.expand_dims(img_array, 0)
        predictions = model.predict(img_array)
        
        predictions = predictions[-1:]
        
        score = tf.nn.softmax(predictions[0])
        plt.subplot(5, 2, i + 1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(list_file_actual[i])
        plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" +  str(list_label_actual[tf.math.argmax(score)]))
        
    plt.show()
    
    input('...')
    

    【讨论】:

    • 你又一次没有回答这个问题,而且你的代码也有重大缺陷,比如不是预测概率而是 logits(并且 from_logits 设置为 False,这是不正确的)。
    • 问题是“这个 LSTM 神经网络的预测值是多少?”他的尝试是忘记模型,请阅读我的解释“预测实时数据,您需要在创建某些报告时输入变量和历史数据,模型会学习范围内值的变化!”
    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2020-01-24
    • 1970-01-01
    • 2017-03-27
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多