當前位置:首頁 » 比特幣問答 » lstm幅度keras比特幣

lstm幅度keras比特幣

發布時間: 2021-11-09 22:31:52

Ⅰ 如何在Python中用LSTM網路進行時間序列預測

時間序列模型

時間序列預測分析就是利用過去一段時間內某事件時間的特徵來預測未來一段時間內該事件的特徵。這是一類相對比較復雜的預測建模問題,和回歸分析模型的預測不同,時間序列模型是依賴於事件發生的先後順序的,同樣大小的值改變順序後輸入模型產生的結果是不同的。
舉個栗子:根據過去兩年某股票的每天的股價數據推測之後一周的股價變化;根據過去2年某店鋪每周想消費人數預測下周來店消費的人數等等

RNN 和 LSTM 模型

時間序列模型最常用最強大的的工具就是遞歸神經網路(recurrent neural network, RNN)。相比與普通神經網路的各計算結果之間相互獨立的特點,RNN的每一次隱含層的計算結果都與當前輸入以及上一次的隱含層結果相關。通過這種方法,RNN的計算結果便具備了記憶之前幾次結果的特點。

典型的RNN網路結構如下:

4. 模型訓練和結果預測
將上述數據集按4:1的比例隨機拆分為訓練集和驗證集,這是為了防止過度擬合。訓練模型。然後將數據的X列作為參數導入模型便可得到預測值,與實際的Y值相比便可得到該模型的優劣。

實現代碼

  • 時間間隔序列格式化成所需的訓練集格式

  • import pandas as pdimport numpy as npdef create_interval_dataset(dataset, look_back):

  • """ :param dataset: input array of time intervals :param look_back: each training set feature length :return: convert an array of values into a dataset matrix. """

  • dataX, dataY = [], [] for i in range(len(dataset) - look_back):

  • dataX.append(dataset[i:i+look_back])

  • dataY.append(dataset[i+look_back]) return np.asarray(dataX), np.asarray(dataY)


  • df = pd.read_csv("path-to-your-time-interval-file")

  • dataset_init = np.asarray(df) # if only 1 columndataX, dataY = create_interval_dataset(dataset, lookback=3) # look back if the training set sequence length

  • 這里的輸入數據來源是csv文件,如果輸入數據是來自資料庫的話可以參考這里

  • LSTM網路結構搭建

  • import pandas as pdimport numpy as npimport randomfrom keras.models import Sequential, model_from_jsonfrom keras.layers import Dense, LSTM, Dropoutclass NeuralNetwork():

  • def __init__(self, **kwargs):

  • """ :param **kwargs: output_dim=4: output dimension of LSTM layer; activation_lstm='tanh': activation function for LSTM layers; activation_dense='relu': activation function for Dense layer; activation_last='sigmoid': activation function for last layer; drop_out=0.2: fraction of input units to drop; np_epoch=10, the number of epoches to train the model. epoch is one forward pass and one backward pass of all the training examples; batch_size=32: number of samples per gradient update. The higher the batch size, the more memory space you'll need; loss='mean_square_error': loss function; optimizer='rmsprop' """

  • self.output_dim = kwargs.get('output_dim', 8) self.activation_lstm = kwargs.get('activation_lstm', 'relu') self.activation_dense = kwargs.get('activation_dense', 'relu') self.activation_last = kwargs.get('activation_last', 'softmax') # softmax for multiple output

  • self.dense_layer = kwargs.get('dense_layer', 2) # at least 2 layers

  • self.lstm_layer = kwargs.get('lstm_layer', 2) self.drop_out = kwargs.get('drop_out', 0.2) self.nb_epoch = kwargs.get('nb_epoch', 10) self.batch_size = kwargs.get('batch_size', 100) self.loss = kwargs.get('loss', 'categorical_crossentropy') self.optimizer = kwargs.get('optimizer', 'rmsprop') def NN_model(self, trainX, trainY, testX, testY):

  • """ :param trainX: training data set :param trainY: expect value of training data :param testX: test data set :param testY: epect value of test data :return: model after training """

  • print "Training model is LSTM network!"

  • input_dim = trainX[1].shape[1]

  • output_dim = trainY.shape[1] # one-hot label

  • # print predefined parameters of current model:

  • model = Sequential() # applying a LSTM layer with x dim output and y dim input. Use dropout parameter to avoid overfitting

  • model.add(LSTM(output_dim=self.output_dim,

  • input_dim=input_dim,

  • activation=self.activation_lstm,

  • dropout_U=self.drop_out,

  • return_sequences=True)) for i in range(self.lstm_layer-2):

  • model.add(LSTM(output_dim=self.output_dim,

  • input_dim=self.output_dim,

  • activation=self.activation_lstm,

  • dropout_U=self.drop_out,

  • return_sequences=True)) # argument return_sequences should be false in last lstm layer to avoid input dimension incompatibility with dense layer

  • model.add(LSTM(output_dim=self.output_dim,

  • input_dim=self.output_dim,

  • activation=self.activation_lstm,

  • dropout_U=self.drop_out)) for i in range(self.dense_layer-1):

  • model.add(Dense(output_dim=self.output_dim,

  • activation=self.activation_last))

  • model.add(Dense(output_dim=output_dim,

  • input_dim=self.output_dim,

  • activation=self.activation_last)) # configure the learning process

  • model.compile(loss=self.loss, optimizer=self.optimizer, metrics=['accuracy']) # train the model with fixed number of epoches

  • model.fit(x=trainX, y=trainY, nb_epoch=self.nb_epoch, batch_size=self.batch_size, validation_data=(testX, testY)) # store model to json file

  • model_json = model.to_json() with open(model_path, "w") as json_file:

  • json_file.write(model_json) # store model weights to hdf5 file

  • if model_weight_path: if os.path.exists(model_weight_path):

  • os.remove(model_weight_path)

  • model.save_weights(model_weight_path) # eg: model_weight.h5

  • return model

  • 這里寫的只涉及LSTM網路的結構搭建,至於如何把數據處理規范化成網路所需的結構以及把模型預測結果與實際值比較統計的可視化,就需要根據實際情況做調整了。

    Ⅱ Keras使用LSTM時輸入問題

    語言模型主要分為規則模型和統計模型兩種。統計語言模型是用概率統計的方法來揭示語言單位內在的統計規律,其中N-Gram簡單有效,被廣泛使用。N-Gram:該模型基於這樣一種假設,第n個詞的出現只與前面N-1個詞相關,而與其它任何詞都不相關,整句的概率就是各個詞出現概率的乘積。這些概率可以通過直接從語料中統計N個詞同時出現的次數得到。常用的是二元的Bi-Gram和三元的Tri-Gram。語言模型的性能通常用交叉熵和復雜度(Perplexity)來衡量。交叉熵的意義是用該模型對文本識別的難度,或者從壓縮的角度來看,每個詞平均要用幾個位來編碼。復雜度的意義是用該模型表示這一文本平均的分支數,其倒數可視為每個詞的平均概率。平滑是指對沒觀察到的N元組合賦予一個概率值,以保證詞序列總能通過語言模型得到一個概率值。通常使用的平滑技術有圖靈估計、刪除插值平滑、Katz平滑和Kneser-Ney平滑。

    Ⅲ 用Python和Keras做LSTM神經網路普通電腦可以嗎

    您好,最好用有NVIDIA GPU的電腦,使用CUDA跑LSTM。光用CPU跑LSTM會很慢!

    Ⅳ 如何用 Keras 調試LSTM超參數解決時間序列預測問題

    就一個abcd作為一條樣本即可,a b c d 的每一步都會計算loss的,所以拆開也沒啥用 另外你這個不是序列標注,因為你是要預測下一個,而不是給整體一個最佳序列

    Ⅳ keras輸入怎麼把2維數據怎麼變為3維,送到lstm

    np.reshape(x,(9000,16,48))

    x是你的數據 確保加粗的兩個參數乘積是768就行了,這里16是時間步數,48是每個時間步特徵向量的長度。

    熱點內容
    收到假eth幣 發布:2025-10-20 08:58:16 瀏覽:973
    暗黑破壞神2eth打孔 發布:2025-10-20 08:42:58 瀏覽:105
    BTC和CBT是一樣的嗎 發布:2025-10-20 08:42:57 瀏覽:233
    華碩trx40Pro供電 發布:2025-10-20 08:33:26 瀏覽:432
    曬人民幣編號的朋友圈 發布:2025-10-20 08:25:32 瀏覽:687
    doge格式 發布:2025-10-20 08:02:00 瀏覽:382
    以太坊會爆發嗎 發布:2025-10-20 08:01:59 瀏覽:772
    一台比特幣礦機的功率 發布:2025-10-20 07:39:24 瀏覽:925
    trx輔助帶 發布:2025-10-20 07:35:29 瀏覽:48
    比特幣哈希值有多少位 發布:2025-10-20 07:31:20 瀏覽:633