Keras - 功能和顺序模型给出不同的结果

时间:2018-04-24 09:47:39

标签: python neural-network keras

我实现了两个模型,一次使用顺序方式,一次使用功能API。现在这两个模型给出了不同的结果,这对我来说没有意义。 我无法弄清楚,问题是什么。任何想法或解决方案?

这两个模型:

顺序模型:

model = Sequential()
embedding_layer = Embedding(VOCAB_SIZE +1, EMBEDDING_SIZE, mask_zero= True)  
model.add(embedding_layer)

model.add(Bidirectional(LSTM(HIDDEN_SIZE, return_sequences= True))),
model.add(TimeDistributed(Dense(NUM_LABELS, activation='softmax')))

model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

model.fit(train_sents_padded, train_labels_padded, batch_size=4, epochs=10, 
validation_data=(dev_sents_padded, dev_labels_padded))

score, acc = model.evaluate(dev_sents_padded, dev_labels_padded)
print("\nAccuracy: ", acc)

功能模型:

inputs = Input(shape=(MAX_LENGTH,))  
embedding = Embedding(VOCAB_SIZE +1, EMBEDDING_SIZE, mask_zero= True)(inputs)

left = LSTM(HIDDEN_SIZE, return_sequences=True)(embedding)
right = LSTM(HIDDEN_SIZE, go_backwards=True, return_sequences=True) 
(embedding)

left_right = concatenate([left, right])

left_right = TimeDistributed(Dense(NUM_LABELS, activation='softmax')) 
(left_right)

combined_model = Model(inputs=inputs, outputs=left_right)

combined_model.compile(loss='categorical_crossentropy', optimizer='adam', 
metrics=['accuracy'])

combined_model.fit(
train_sents_padded,
train_labels_padded,
batch_size=4,
epochs=10,
validation_data=(dev_sents_padded, dev_labels_padded)
)

score, acc = combined_model.evaluate(dev_sents_padded, dev_labels_padded)
print("\nBidirectional LSTM Accuracy: ", acc)

+++ 摘要:

顺序模型:

Layer (type)                 Output Shape              Param #   
=================================================================
embedding_1 (Embedding)      (None, None, 50)          26150     
_________________________________________________________________
bidirectional_1 (Bidirection (None, None, 100)         40400     
_________________________________________________________________
time_distributed_1 (TimeDist (None, None, 61)          6161      
=================================================================
Total params: 72,711
Trainable params: 72,711
Non-trainable params: 0 `

功能模型:

Layer (type)                    Output Shape         Param #     Connected to   
========================================================================
input_1 (InputLayer)      (None, 34)           0                                            
____________________________________________________________________
embedding_2 (Embedding)   (None, 34, 50)       26150       input_1[0][0]                    
______________________________________________________________________
lstm_2 (LSTM)             (None, 34, 50)       20200       embedding_2[0][0]                
____________________________________________________________________
lstm_3 (LSTM)             (None, 34, 50)       20200       embedding_2[0][0]                
_________________________________________________________________
concatenate_1 (Concatenate)(None, 34, 100)      0           lstm_2[0][0]                     
                                                             lstm_3[0][0]                     
 ___________________________________________________________
time_distributed_2 (TimeDistrib (None, 34, 61)  6161    concatenate_1[0][0]              
=====================================================================
Total params: 72,711
Trainable params: 72,711
Non-trainable params: 0`

+++ 如果我在seqential模型中将VOCAB_SIZE + 1更改为VOCAB_SIZE,则acc为59,但仅限每三次运行?

0 个答案:

没有答案
相关问题