2017-05-07 97 views
8

Tôi có tập dữ liệu với 2 cột - Mỗi cột chứa một tập hợp các tài liệu. Tôi phải kết hợp tài liệu trong Col A với các tài liệu được cung cấp trong Col B. Đây là vấn đề phân loại được giám sát. Vì vậy, dữ liệu đào tạo của tôi chứa cột nhãn cho biết liệu tài liệu có khớp hay không.Mô hình LSTM trong Keras với Đầu vào Phụ trợ

Để giải quyết vấn đề, tôi đã tạo một bộ tính năng, giả sử f1-f25 (bằng cách so sánh 2 tài liệu) và sau đó đào tạo một trình phân loại nhị phân trên các tính năng này. Cách tiếp cận này hoạt động khá tốt, nhưng bây giờ tôi muốn đánh giá các mô hình Deep Learning về vấn đề này (đặc biệt là các mô hình LSTM).

Tôi đang sử dụng thư viện keras bằng Python. Sau khi đi qua các tài liệu keras và hướng dẫn khác có sẵn trên mạng, tôi đã cố gắng làm như sau:

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) 

Tuy nhiên, khi tôi điểm này trên các dữ liệu huấn luyện, tôi nhận được prob cùng. điểm cho tất cả các trường hợp. Vấn đề có vẻ là với cách đầu vào phụ trợ được nạp vào (vì nó tạo ra đầu ra có ý nghĩa khi tôi loại bỏ đầu vào aux.). Tôi cũng đã thử chèn đầu vào phụ ở các vị trí khác nhau trong mạng. Nhưng bằng cách nào đó tôi không thể làm được điều này.

Mọi con trỏ?

+0

Không chắc chắn nếu đó là dự định, nhưng auxiliary_output chỉ là (1,). Nó thực sự là những gì bạn mong đợi? Hợp nhất 25 đầu vào phụ chỉ với một kết quả? - Mô hình trước khi đầu ra phụ trợ có ý định "không thể đào tạo" trong khi bạn chỉ đào tạo phần cuối cùng? –

+0

Vâng vâng. Đây là một mô hình phân loại nhị phân để đầu ra cuối cùng là (1,). Liệu đầu ra phụ có khác nhau không? Tôi chỉ đơn giản là cho ăn trong bộ cộng hưởng của 25 tính năng như đầu vào phụ trợ và do đó hình dạng (25,) – Dataminer

+0

Bạn đã thử nhiều kỷ nguyên hơn chưa? –

Trả lời

0

Vâng, điều này mở trong vài tháng và mọi người sẽ bỏ phiếu.
Tôi đã làm điều gì đó tương tự gần đây bằng cách sử dụng this dataset có thể được sử dụng để dự đoán mặc định thẻ tín dụng và nó chứa dữ liệu phân loại của khách hàng (giới tính, trình độ học vấn, tình trạng hôn nhân vv) cũng như lịch sử thanh toán. Vì vậy, tôi đã phải hợp nhất chuỗi thời gian với dữ liệu không phải chuỗi. Giải pháp của tôi rất giống với giải pháp của bạn bằng cách kết hợp LSTM với mật độ dày đặc, tôi cố gắng áp dụng cách tiếp cận cho vấn đề của bạn. Những gì làm việc cho tôi là lớp dày đặc (s) trên đầu vào phụ trợ.

Hơn nữa trong trường hợp của bạn, một lớp được chia sẻ sẽ có ý nghĩa để cùng trọng số được sử dụng để "đọc" cả hai tài liệu. Đề xuất thử nghiệm dữ liệu của bạn:

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=[____]) 

Tôi không biết điều này có thể giúp ích gì khi tôi không có dữ liệu của bạn nên tôi không thể thử. Tuy nhiên đây là bức ảnh đẹp nhất của tôi.
Tôi không chạy mã trên vì những lý do rõ ràng.

Các vấn đề liên quan