簡體   English   中英

Keras LSTM - 具有嵌入層的多對多

[英]Keras LSTM - Many to many with embedding layer

我正在訓練 LSTM 來預測事件的發生。 對於每一天,我都有一個像 [1,0,1] 這樣的向量來表示第一個和第三個事件發生了,而第二個事件沒有發生。

我想將這個問題擴展到適用於多人,每個人都有不同的agent_id 這意味着我需要以某種方式展示我的 model 和agent_id作為一個特征。 雖然我不確定這是否是最好的方法,但我將向量的第一個條目設置為agent_id ,因此它看起來像 [123456, 1, 0,1]。

現在 LSTM model 所做的是為每個事件 output 計算它在第二天發生的概率。 那么我如何看待輸入/輸出將是:[agent_id,今天事件 1 發生了嗎?今天事件 2 發生了嗎? 事件 3 今天發生了嗎?] -> LSTM -> [事件 1 明天發生的概率,事件 2 明天發生的概率,事件 3 明天發生的概率]

現在輸入的長度比 output 長。據我了解,從這篇文章的答案https://stats.stackexchange.com/questions/305863/how-to-train-lstm-model-on-multiple -time-series-data ,我需要一個可以改變輸入大小的嵌入層,以便 LSTM 為我提供所需的 output。

為此,我嘗試執行以下操作:

from keras.models import Sequential
from keras.layers import *
xin = Input(batch_shape=(batch_size, window_length), dtype='int32')
xemb = Embedding(x_traindict[123456].shape[2], x_traindict[123456].shape[2]-1)(xin) #from what I give in to what I want to get out # 3dim (batch,time,feat)
seq = LSTM(x_traindict[123456].shape[2]-1, return_sequences=True)(xemb)
mlp = TimeDistributed(Dense(y_traindict[123456].shape[1], activation='softmax'))(seq)
model = tf.keras.Model(inputs=xin, outputs=mlp)
model.compile(optimizer='Adam', loss='categorical_crossentropy')
print(f"batch size is {batch_size}, window_length = {window_length}, x_train.shape is {x_traindict[123456].shape} and y_train.shape is  {y_traindictalt[123456].shape}")
model.summary()
model.fit(x_traindict[123456], y_traindict[123456], epochs=20)
------------------------------------------------------------------------------------------------
batch size is 358, window_length = 7, x_train.shape is (358, 7, 149) and y_train.shape is  (358, 148)
Model: "model_7"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_12 (InputLayer)       [(358, 7)]                0         
                                                                 
 embedding_14 (Embedding)    (358, 7, 148)             22052     
                                                                 
 lstm_16 (LSTM)              (358, 7, 148)             175824    
                                                                 
 time_distributed_11 (TimeDi  (358, 7, 149)            22201     
 stributed)                                                      
                                                                 
=================================================================
Total params: 220,077
Trainable params: 220,077
Non-trainable params: 0
_________________________________________________________________

我的想法是嵌入將從 x_train 獲取輸入,包括agent_id ,並將學習將其編碼為 y_train 大小的輸入,不包括agent_id 然后 LSTM 將學習處理它從嵌入中接收到的內容以正確預測 y_train。 但是,上面的代碼給我以下錯誤:

ValueError: Exception encountered when calling layer "model_7" (type Functional).
    
    Input 0 of layer "lstm_16" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 7, 149, 148) 

我不明白這個錯誤。

我想問的是:

  1. 我的想法有意義嗎? 通過直接用事件實現agent_id ,LSTM 是否可以學習不同代理的時間序列預測?
  2. 如何修復我的代碼中的錯誤? 如果有幫助,我基本上是從第一個答案開始填寫這個帖子的模板: https://github.com/keras-team/keras/issues/2654

編輯:

  1. 我嘗試將 xin 更改為xin = Input(batch_shape=(window_length,), dtype='int32')但現在在我說 seq =...: Input 0 of layer "lstm_26" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (7, 133).的行中出現值錯誤Input 0 of layer "lstm_26" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (7, 133). Input 0 of layer "lstm_26" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (7, 133).
  2. 我還嘗試將 xin 更改為xin = Input(batch_shape=(window_length,number_of_transactions+1), dtype='int32')但這會產生ValueError: Input 0 of layer "model_11" is incompatible with the layer: expected shape=(None, 134), found shape=(None, 7, 134)

注意:我今天必須取一個新樣品。 134 替換了上面的 149 事件

  1. 是的,我認為這個想法是正確的。 通過將 agent-id 作為序列中的第一個元素,RNN 會將此信息編碼為 state,隨后用於預測事件的概率。 需要注意的一件事是 model 將嘗試僅給定第一個序列元素 - agent-id 來生成預測。

  2. 我認為你的問題是你在輸入調用中包含了批量大小,但是批量大小是隱含的並且不需要定義,所以xin = Input(batch_shape=(batch_size, window_length), dtype='int32')應該成為xin = Input(batch_shape=(window_length), dtype='int32')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM