【问题标题】:How to feed an LSTM/GRU model multiple independent Time Series?如何为 LSTM/GRU 模型提供多个独立的时间序列?
【发布时间】:2021-10-06 14:54:43
【问题描述】:

为了简单地解释一下:我有 53 口产油井测量,每口井每天都测量 6 年,我们记录了多个变量(压力、产水量、产气量......等等 em>),我们的主要组成部分(我们要研究和预测的部分)是石油生产速度。我如何使用所有数据来训练我的 LSTM/GRU 模型,知道油井是独立的,并且每个油井的测量都是在同一时间完成的?

【问题讨论】:

    标签: deep-learning time-series lstm recurrent-neural-network forecasting


    【解决方案1】:

    如果您想假设井是独立的,则无需了解“每个 [井] 的测量已在同一时间完成”。 (为什么你认为这些知识很有用?)

    因此,如果孔被认为是独立的,请将它们视为单独的样本。像往常一样将它们分成训练集、验证集和测试集。在训练集上训练一个通常的 LSTM 或 GRU。

    顺便说一句,您可能希望使用注意力机制而不是循环网络。它更容易训练并且通常会产生可比较的结果。

    即使是卷积网络也可能足够好。如果您怀疑存在长程相关性,请参阅 WaveNet 等方法。

    【讨论】:

    • 我是 LSTM 知识的新手,所以我将它们堆叠在一起,就好像它们是一个数据集一样?这不会对时间序列造成干扰吗?
    • @AbderrahmaneTaibi 将它们视为单独的样本。不作为一个样本。这并不特定于循环网络,这是一般的深度学习内容。就像您在图像数据集中有多个图像一样。您不会沿通道维度或沿时间/空间维度连接它们。您将它们视为单独的样本。
    【解决方案2】:

    这些井测量听起来像是特定且独立的事件。我在金融部门工作。我们总是使用 LSTM 查看不同的股票,每只股票都有特定的时间序列,而不是 10 只股票混在一起。这是一些分析特定​​股票的代码。修改代码以满足您的需要。

    from pandas_datareader import data as wb
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.pylab import rcParams
    from sklearn.preprocessing import MinMaxScaler
    
    start = '2019-06-30'
    end = '2020-06-30'
    
    tickers = ['GOOG']
    
    thelen = len(tickers)
    
    price_data = []
    for ticker in tickers:
        prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Open','Adj Close']]
        price_data.append(prices.assign(ticker=ticker)[['ticker', 'Open', 'Adj Close']])
    
    #names = np.reshape(price_data, (len(price_data), 1))
    
    df = pd.concat(price_data)
    df.reset_index(inplace=True)
    
    for col in df.columns: 
        print(col) 
        
    #used for setting the output figure size
    rcParams['figure.figsize'] = 20,10
    #to normalize the given input data
    scaler = MinMaxScaler(feature_range=(0, 1))
    #to read input data set (place the file name inside  ' ') as shown below
    
    
    df['Adj Close'].plot()
    plt.legend(loc=2)
    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.show()
    
    ntrain = 80
    df_train = df.head(int(len(df)*(ntrain/100)))
    ntest = -80
    df_test = df.tail(int(len(df)*(ntest/100)))
    
    
    #importing the packages 
    from sklearn.preprocessing import MinMaxScaler
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, LSTM
    
    #dataframe creation
    seriesdata = df.sort_index(ascending=True, axis=0)
    new_seriesdata = pd.DataFrame(index=range(0,len(df)),columns=['Date','Adj Close'])
    length_of_data=len(seriesdata)
    for i in range(0,length_of_data):
        new_seriesdata['Date'][i] = seriesdata['Date'][i]
        new_seriesdata['Adj Close'][i] = seriesdata['Adj Close'][i]
    #setting the index again
    new_seriesdata.index = new_seriesdata.Date
    new_seriesdata.drop('Date', axis=1, inplace=True)
    #creating train and test sets this comprises the entire data’s present in the dataset
    myseriesdataset = new_seriesdata.values
    totrain = myseriesdataset[0:255,:]
    tovalid = myseriesdataset[255:,:]
    #converting dataset into x_train and y_train
    scalerdata = MinMaxScaler(feature_range=(0, 1))
    scale_data = scalerdata.fit_transform(myseriesdataset)
    x_totrain, y_totrain = [], []
    length_of_totrain=len(totrain)
    for i in range(60,length_of_totrain):
        x_totrain.append(scale_data[i-60:i,0])
        y_totrain.append(scale_data[i,0])
    x_totrain, y_totrain = np.array(x_totrain), np.array(y_totrain)
    x_totrain = np.reshape(x_totrain, (x_totrain.shape[0],x_totrain.shape[1],1))
    
    
    #LSTM neural network
    lstm_model = Sequential()
    lstm_model.add(LSTM(units=50, return_sequences=True, input_shape=(x_totrain.shape[1],1)))
    lstm_model.add(LSTM(units=50))
    lstm_model.add(Dense(1))
    lstm_model.compile(loss='mean_squared_error', optimizer='adadelta')
    lstm_model.fit(x_totrain, y_totrain, epochs=10, batch_size=1, verbose=2)
    #predicting next data stock price
    myinputs = new_seriesdata[len(new_seriesdata) - (len(tovalid)+1) - 60:].values
    myinputs = myinputs.reshape(-1,1)
    myinputs  = scalerdata.transform(myinputs)
    tostore_test_result = []
    for i in range(60,myinputs.shape[0]):
        tostore_test_result.append(myinputs[i-60:i,0])
    tostore_test_result = np.array(tostore_test_result)
    tostore_test_result = np.reshape(tostore_test_result,(tostore_test_result.shape[0],tostore_test_result.shape[1],1))
    myclosing_priceresult = lstm_model.predict(tostore_test_result)
    myclosing_priceresult = scalerdata.inverse_transform(myclosing_priceresult)
        
    totrain = df_train
    tovalid = df_test
    
    #predicting next data stock price
    myinputs = new_seriesdata[len(new_seriesdata) - (len(tovalid)+1) - 60:].values
    
    
    #  Printing the next day’s predicted stock price. 
    print(len(tostore_test_result));
    print(myclosing_priceresult);
    

    最终结果:

    1
    [[1396.532]]
    

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 2016-03-29
      • 2019-09-01
      • 2017-02-10
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      相关资源
      最近更新 更多