2017-05-07 2 views
8

두 개의 열이있는 데이터 세트가 있습니다. 각 열에는 문서 세트가 들어 있습니다. Col A의 문서와 Col B의 문서를 일치시켜야합니다. 이것은 감독 된 분류 문제입니다. 따라서 교육 데이터에는 문서가 일치하는지 여부를 나타내는 라벨 열이 포함됩니다.보조 입력이있는 Keras의 LSTM 모델

이 문제를 해결하기 위해 f1-f25 (2 개의 문서를 비교하여)라는 일련의 기능을 만든 다음이 기능에 대한 바이너리 분류자를 교육했습니다. 이 접근법은 상당히 잘 작동하지만, 이제는이 문제에 대한 Deep Learning 모델 (특히 LSTM 모델)을 평가하고 싶습니다.

저는 파이썬에서 keras 라이브러리를 사용하고 있습니다. keras 문서 및 기타 자습서 온라인을 통해 진행 후, 나는 다음과 같이 관리해야 : 나는 훈련 데이터에이 점수를 때

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

# Each document contains a series of 200 words 
# The necessary text pre-processing steps have been completed to transform 
    each doc to a fixed length seq 
main_input1 = Input(shape=(200,), dtype='int32', name='main_input1') 
main_input2 = Input(shape=(200,), dtype='int32', name='main_input2') 

# Next I add a word embedding layer (embed_matrix is separately created  
for each word in my vocabulary by reading from a pre-trained embedding model) 
x = Embedding(output_dim=300, input_dim=20000, 
input_length=200, weights = [embed_matrix])(main_input1) 
y = Embedding(output_dim=300, input_dim=20000, 
input_length=200, weights = [embed_matrix])(main_input2) 

# Next separately pass each layer thru a lstm layer to transform seq of 
vectors into a single sequence 
lstm_out_x1 = LSTM(32)(x) 
lstm_out_x2 = LSTM(32)(y) 

# concatenate the 2 layers and stack a dense layer on top 
x = keras.layers.concatenate([lstm_out_x1, lstm_out_x2]) 
x = Dense(64, activation='relu')(x) 
# generate intermediate output 
auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(x) 

# add auxiliary input - auxiliary inputs contains 25 features for each document pair 
auxiliary_input = Input(shape=(25,), name='aux_input') 

# merge aux output with aux input and stack dense layer on top 
main_input = keras.layers.concatenate([auxiliary_output, auxiliary_input]) 
x = Dense(64, activation='relu')(main_input) 
x = Dense(64, activation='relu')(x) 

# finally add the main output layer 
main_output = Dense(1, activation='sigmoid', name='main_output')(x) 

model = Model(inputs=[main_input1, main_input2, auxiliary_input], outputs= main_output) 
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 

model.fit([x1, x2,aux_input], y, 
     epochs=3, batch_size=32) 

그러나, 나는 같은 확률값을 얻을. 모든 경우 점수. 이 문제는 AUX 입력을 제거 할 때 의미있는 출력을 생성하기 때문에 AUX 입력이 공급되는 방식과 관련이있는 것으로 보입니다. 보조 입력을 네트워크의 다른 위치에 삽입하려고했습니다. 그러나 어떻게 든 나는 이것을 작동시킬 수 없었다.

모든 포인터?

+0

의도적인지는 모르지만 보조 출력은 (1,)뿐입니다. 그것은 당신이 정말로 기대하는 것입니까? 하나의 결과로 25 개의 보조 입력을 병합 하시겠습니까? - 최종 부품 만 훈련하는 동안 보조 출력이 "훈련 할 수 없도록"하기 전에 모델이 있습니까? –

+0

음. 이것은 바이너리 분류 모델이므로 최종 출력은 (1,)입니다. 보조 출력이 달라야합니까? 나는 단순히 보조 입력으로 25 가지 기능의 additonal 세트를 먹이기 때문에 (25,) 모양 – Dataminer

+0

더 많은 신기원을 사용해 보셨습니까? –

답변

0

글쎄, 몇 달 동안 열려 있고 사람들은 그것을 투표합니다.
최근 신용 카드 기본값을 예측하는 데 사용할 수있는 this dataset을 사용하여 매우 유사한 작업을 수행했으며 고객 (성별, 교육 수준, 결혼 상태 등)의 범주 데이터와 지불 내역을 시계열로 포함합니다. 그래서 시계열을 비 시리즈 데이터와 병합해야했습니다. 내 솔루션은 LSTM을 밀집하여 사용자의 솔루션과 매우 유사 했으므로 문제에 대한 접근 방식을 채택하려고합니다. 나에게 도움이 된 것은 조밀 한 층이 보조 입력에 있다는 것입니다.

또한 공유 레이어를 사용하면 동일한 가중치를 사용하여 두 문서를 모두 읽을 수 있습니다. 데이터에 대한 테스트에 대한 나의 제안은 :

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

# Each document contains a series of 200 words 
# The necessary text pre-processing steps have been completed to transform 
    each doc to a fixed length seq 
main_input1 = Input(shape=(200,), dtype='int32', name='main_input1') 
main_input2 = Input(shape=(200,), dtype='int32', name='main_input2') 

# Next I add a word embedding layer (embed_matrix is separately created  
for each word in my vocabulary by reading from a pre-trained embedding model) 
x1 = Embedding(output_dim=300, input_dim=20000, 
input_length=200, weights = [embed_matrix])(main_input1) 
x2 = Embedding(output_dim=300, input_dim=20000, 
input_length=200, weights = [embed_matrix])(main_input2) 

# Next separately pass each layer thru a lstm layer to transform seq of 
vectors into a single sequence 
# Comment Manngo: Here I changed to shared layer 
# Also renamed y as input as it was confusing 
# Now x and y are x1 and x2 
lstm_reader = LSTM(32) 
lstm_out_x1 = lstm_reader(x1) 
lstm_out_x2 = lstm_reader(x2) 

# concatenate the 2 layers and stack a dense layer on top 
x = keras.layers.concatenate([lstm_out_x1, lstm_out_x2]) 
x = Dense(64, activation='relu')(x) 
x = Dense(32, activation='relu')(x) 
# generate intermediate output 
# Comment Manngo: This is created as a dead-end 
# It will not be used as an input of any layers below 
auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(x) 

# add auxiliary input - auxiliary inputs contains 25 features for each document pair 
# Comment Manngo: Dense branch on the comparison features 
auxiliary_input = Input(shape=(25,), name='aux_input') 
auxiliary_input = Dense(64, activation='relu')(auxiliary_input) 
auxiliary_input = Dense(32, activation='relu')(auxiliary_input) 

# OLD: merge aux output with aux input and stack dense layer on top 
# Comment Manngo: actually this is merging the aux output preparation dense with the aux input processing dense 
main_input = keras.layers.concatenate([x, auxiliary_input]) 
main = Dense(64, activation='relu')(main_input) 
main = Dense(64, activation='relu')(main) 

# finally add the main output layer 
main_output = Dense(1, activation='sigmoid', name='main_output')(main) 

# Compile 
# Comment Manngo: also define weighting of outputs, main as 1, auxiliary as 0.5 
model.compile(optimizer=adam, 
       loss={'main_output': 'w_binary_crossentropy', 'aux_output': 'binary_crossentropy'}, 
       loss_weights={'main_output': 1.,'auxiliary_output': 0.5}, 
       metrics=['accuracy']) 

# Train model on main_output and on auxiliary_output as a support 
# Comment Manngo: Unknown information marked with placeholders ____ 
# We have 3 inputs: x1 and x2: the 2 strings 
# aux_in: the 25 features 
# We have 2 outputs: main and auxiliary; both have the same targets -> (binary)y 


model.fit({'main_input1': __x1__, 'main_input2': __x2__, 'auxiliary_input' : __aux_in__}, {'main_output': __y__, 'auxiliary_output': __y__}, 
       epochs=1000, 
       batch_size=__, 
       validation_split=0.1, 
       callbacks=[____]) 

내가 시도 할 수 있도록 내가 데이터를 가지고 있지 않기 때문에이 할 수있는 방법을 많이 알고하지 않습니다. 그럼에도 불구하고 이것은 내 최고의 기회입니다.
명백한 이유로 위 코드를 실행하지 않았습니다.

관련 문제