代码之家  ›  专栏  ›  技术社区  ›  Sharif Amlani

为什么LSTM模型在多个模型运行中产生不同的预测?

  •  2
  • Sharif Amlani  · 技术社区  · 7 年前

    我使用长期短期记忆(LSTM)来生成预测。我注意到,每次运行LSTM模型时,它都会使用相同的数据生成稍微不同的预测。我想知道为什么会这样,如果我做错了什么?

    谢谢您

    from numpy import array
    from keras.models import Sequential
    from keras.layers import LSTM
    from keras.layers import Dense
    from keras.layers import Flatten
    from keras.layers import TimeDistributed
    from keras.layers.convolutional import Conv1D
    from keras.layers.convolutional import MaxPooling1D
    
    # split a univariate sequence into samples
    def split_sequence(sequence, n_steps):
        X, y = list(), list()
        for i in range(len(sequence)):
            # find the end of this pattern
            end_ix = i + n_steps
            # check if we are beyond the sequence
            if end_ix > len(sequence)-1:
                break
            # gather input and output parts of the pattern
            seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
            X.append(seq_x)
            y.append(seq_y)
        return array(X), array(y)
    
    def LSTM_Model(Data, N_Steps, Epochs):
        # define input sequence
        raw_seq = Data
    
        # choose a number of time steps
        n_steps_og = N_Steps
    
        # split into samples
        X, y = split_sequence(raw_seq, n_steps_og)
    
        # reshape from [samples, timesteps] into [samples, subsequences, timesteps, features]
        n_features = 1
        n_seq = 2
        n_steps = 2
        X = X.reshape((X.shape[0], n_seq, n_steps, n_features))
    
    
        # define model
        model = Sequential()
        model.add(TimeDistributed(Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None, n_steps, n_features)))
        model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
        model.add(TimeDistributed(Flatten()))
        model.add(LSTM(50, activation='relu'))
        model.add(Dense(1))
        model.compile(optimizer='adam', loss='mse')
    
    
       # fit model
       model.fit(X, y, epochs=Epochs, verbose=2)
       #Create Forcasting data
       #Now take the last 4 days of the Model data for the forcast
       Forcast_data = Data[len(new_data) - n_steps_og:]
    
       # demonstrate prediction
       x_input = array(Forcast_data)
       x_input = x_input.reshape((1, n_seq, n_steps, n_features))
       yhat = float(model.predict(x_input, verbose=0))
       return(yhat)
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Null    7 年前

    许多这样的方法都是用系数的随机权重初始化的。然后他们寻找一个好的局部最小到某种损失函数。这意味着他们(希望)只会找到许多近乎最优的解决方案中的一个,但不太可能找到单一的非常好的解决方案,甚至不可能重复找到相同的解决方案。因此,只要你的预测略有不同,你的结果就是典型的。

    这更像是一个通用的机器学习问题,而不是针对Python,但我希望这能有所帮助。