簡體   English   中英

如何構建LSTM神經網絡進行分類

[英]How to structure an LSTM neural network for classification

我的數據在兩個人之間有各種對話。 每個句子都有某種類型的分類。 我試圖使用NLP網來對話的每個句子進行分類。 我嘗試了一個卷積網並得到了不錯的結果(不是突破性的)。 我認為,由於這是一次來回的對話,而LSTM網可能會產生更好的結果,因為之前所說的可能會對后面的內容產生很大的影響。

RNN網的類型

如果我遵循上面的結構,我會假設我做了多對多。 我的數據看起來像。

X_train = [[sentence 1],  
           [sentence 2],
           [sentence 3]]
Y_train = [[0],
           [1],
           [0]]

已使用word2vec處理數據。 然后我按如下方式設計我的網絡..

model = Sequential()      
model.add(Embedding(len(vocabulary),embedding_dim,
          input_length=X_train.shape[1]))
model.add(LSTM(88))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentropy',
              metrics['accuracy'])
model.fit(X_train,Y_train,verbose=2,nb_epoch=3,batch_size=15)

我假設這個設置將一次輸入一批句子。 但是,如果在model.fit中,shuffle不等於false接收洗牌批次,那么為什么LSTM網在這種情況下甚至有用? 從對該主題的研究來看,為了實現多對多結構,還需要改變LSTM層

model.add(LSTM(88,return_sequence=True))

輸出層需要......

model.add(TimeDistributed(Dense(1,activation='sigmoid')))

切換到此結構時,輸入大小出錯。 我不確定如何重新格式化數據以滿足此要求,以及如何編輯嵌入層以接收新數據格式。

任何投入將不勝感激。 或者如果您對更好的方法有任何建議,我很高興聽到它們!

你的第一次嘗試很好。 改組發生在句子之間,只是在他們之間改變訓練樣本,這樣他們就不會總是以相同的順序進入。 句子里面的單詞沒有改組。

或者我可能沒有正確理解這個問題?

編輯

在更好地理解了這個問題之后,這是我的主張。

數據准備:您可以用n句子(他們可以重疊)對您的語料庫進行切片。 然后你應該有一個像(number_blocks_of_sentences, n, number_of_words_per_sentence)這樣的形狀(number_blocks_of_sentences, n, number_of_words_per_sentence)所以基本上是一個包含n句子塊的2D數組列表。 n不應該太大,因為LSTM在訓練時無法處理序列中的大量元素(消失梯度)。 您的目標應該是一個形狀數組(number_blocks_of_sentences, n, 1)因此還包含一個2D數組列表,其中包含句子塊中每個句子的類。

型號:

n_sentences = X_train.shape[1]  # number of sentences in a sample (n)
n_words = X_train.shape[2]      # number of words in a sentence

model = Sequential()
# Reshape the input because Embedding only accepts shape (batch_size, input_length) so we just transform list of sentences in huge list of words
model.add(Reshape((n_sentences * n_words,),input_shape = (n_sentences, n_words)))
# Embedding layer - output shape will be (batch_size, n_sentences * n_words, embedding_dim) so each sample in the batch is a big 2D array of words embedded 
model.add(Embedding(len(vocabaulary), embedding_dim, input_length = n_sentences * n_words ))
# Recreate the sentence shaped array
model.add(Reshape((n_sentences, n_words, embedding_dim))) 
# Encode each sentence - output shape is (batch_size, n_sentences, 88)
model.add(TimeDistributed(LSTM(88)))
# Go over lines and output hidden layer which contains info about previous sentences - output shape is (batch_size, n_sentences, hidden_dim)
model.add(LSTM(hidden_dim, return_sequence=True))
# Predict output binary class - output shape is (batch_size, n_sentences, 1)
model.add(TimeDistributed(Dense(1,activation='sigmoid')))
...

這應該是一個好的開始。

我希望這有幫助

暫無
暫無

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

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