2017-03-02 939 views
1

在我看到的所有神經網絡分類示例中,他們都有訓練數據,其中每個訓練數據有一個類別作爲主要類別或每個標籤輸入數據。神經網絡分類:他們總是必須每個訓練數據有一個標籤

您可以提供具有多個標籤的培訓數據嗎?例如:帶有「貓」和「鼠標」的圖片。

我明白(也許錯了),如果你在輸出層使用softmax進行概率/預測,它往往試圖選擇一個(最大化辨別力)。我猜這會傷害/阻止學習和預測輸入數據的多個標籤。

有什麼方法/神經網絡的體系結構,其中有多個標籤在訓練數據和多個輸出預測?或者已經是這種情況,我錯過了一些重要的理解。請澄清。

回答

1

大多數例子每個輸入有一個類,所以沒有你沒有錯過任何東西。然而,有可能做多類分類,在文獻中有時稱爲聯合分類。

你用softmax建議的幼稚實現將會很困難,因爲最後一層的輸出必須加起來爲1,所以越多的類你就越難弄清楚網絡想說什麼。

您可以更改體系結構來實現您想要的。對於每個類,您都可以有一個二進制softmax分類器,它從倒數第二層分支出來,或者您可以使用一個sigmoid,即使每個神經元輸出介於0和1之間,也不必加起來一個。注意使用sigmoid可能會使訓練更加困難。

或者,您可以爲每個班級培訓多個網絡,然後在最後將其組合成一個分類系統。這取決於你設想的任務有多複雜。

+0

二進制softmax和'一元'sigmoid幾乎是相同的東西,並在輸出中使用sigmoid不會讓事情變得更加困難。 – lejlot

+0

謝謝盧卡,你的解釋清楚而有幫助。但是,有一點仍然存在。我的直覺與@lejlot相同,因爲只有兩種可能性(是/否)。你能否澄清爲什麼你認爲softmax會使它變得更容易或相反,爲什麼sigmoid會使它更難?以前的文獻或實驗數據? –

0

有什麼方法/神經網絡的架構,其中有多個標籤在訓練數據和多個輸出的預測?

答案是YES。爲了簡要回答你的問題,我在一個高級神經網絡庫Keras的背景下給出了一個例子。

讓我們考慮以下模型。我們想要預測Twitter上有多少轉發和喜歡新聞標題。模型的主要輸入將是標題本身,作爲一系列詞語,但爲了增添趣味,我們的模型還會有一個輔助輸入,可以接收額外的數據,例如標題發佈日的時間等。

enter image description here

from keras.layers import Input, Embedding, LSTM, Dense, merge 
from keras.models import Model 

# headline input: meant to receive sequences of 100 integers, between 1 and 10000. 
# note that we can name any layer by passing it a "name" argument. 
main_input = Input(shape=(100,), dtype='int32', name='main_input') 

# this embedding layer will encode the input sequence 
# into a sequence of dense 512-dimensional vectors. 
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input) 

# a LSTM will transform the vector sequence into a single vector, 
# containing information about the entire sequence 
lstm_out = LSTM(32)(x) 

auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out) 

auxiliary_input = Input(shape=(5,), name='aux_input') 
x = merge([lstm_out, auxiliary_input], mode='concat') 

# we stack a deep fully-connected network on top 
x = Dense(64, activation='relu')(x) 
x = Dense(64, activation='relu')(x) 
x = Dense(64, activation='relu')(x) 

# and finally we add the main logistic regression layer 
main_output = Dense(1, activation='sigmoid', name='main_output')(x) 

這定義具有兩個輸入和兩個輸出的模型:

model = Model(input=[main_input, auxiliary_input], output=[main_output, auxiliary_output]) 

現在,讓編譯和訓練模型如下:

model.compile(optimizer='rmsprop', 
       loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'}, 
       loss_weights={'main_output': 1., 'aux_output': 0.2}) 

# and trained it via: 
model.fit({'main_input': headline_data, 'aux_input': additional_data}, 
      {'main_output': labels, 'aux_output': labels}, 
      nb_epoch=50, batch_size=32) 

參考: Multi-input and multi-output models in Keras

+0

我欣賞細節和流程圖Wasi。請澄清一下,您的標題輸入是否允許最多100個單詞,每個單詞的索引編號介於1到10,000之間?那是編碼嗎? –

+0

@SriramGopalakrishnan是的,這將是輸入,但有一個嵌入層,將文字轉換爲較低維度。 –