我正在尝试构建双向 LSTM。这就是源代码的外观:
model1 = Sequential()model1.add(Embedding(vocabulary_size1, 50, weights=[embedding_matrix_passage], trainable=False))model1.add(Bidirectional(LSTM(64)))model1.add(Dropout(0.5))model1.add(Dense(10, activation='softmax'))model2 = Sequential()model2.add(Embedding(vocabulary_size2, 50, weights=[embedding_matrix_query], trainable=False))model2.add(Bidirectional(LSTM(64)))model2.add(Dropout(0.5))model2.add(Dense(10, activation='softmax'))concat = concatenate([model1.output,model2.output])x = Dense(10,name='c_dense')(concat)out = Activation('softmax',name='c_acti')(x)model = Model([model1.input, model2.input], out)model.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])model.fit([passage_padded_data,query_padded_data], np.array(labels), epochs=2, verbose=1)
当我尝试执行 fit 语句时,我得到了以下错误:
ValueError: Error when checking target: expected c_acti to have shape (10,) but got array with shape (1,)
当我运行 model.summary()
时,模型的摘要看起来像这样:
Layer (type) Output Shape Param # Connected to embedding_29_input (InputLayer) (None, None) 0 embedding_30_input (InputLayer) (None, None) 0 embedding_29 (Embedding) (None, None, 50) 7626350 embedding_29_input[0][0] embedding_30 (Embedding) (None, None, 50) 1134300 embedding_30_input[0][0] bidirectional_27 (Bidirectional) (None, 128) 58880 embedding_29[0][0] bidirectional_28 (Bidirectional) (None, 128) 58880 embedding_30[0][0] dropout_27 (Dropout) (None, 128) 0 bidirectional_27[0][0] dropout_28 (Dropout) (None, 128) 0 bidirectional_28[0][0] dense_27 (Dense) (None, 10) 1290 dropout_27[0][0] dense_28 (Dense) (None, 10) 1290 dropout_28[0][0] concatenate_13 (Concatenate) (None, 20) 0 dense_27[0][0] dense_28[0][0] c_dense (Dense) (None, 10) 210 concatenate_13[0][0] c_acti (Activation) (None, 10) 0 c_dense[0][0] -----------------------Total params: 8,881,200Trainable params: 120,550Non-trainable params: 8,760,650
可能的解决方案是什么?
回答:
可能你忘记了对标签进行 one-hot 编码(即它们是稀疏标签,如 0, 1, 2, 3, …, 9)。因此,可以将损失函数改为 'sparse_categorical_crossentropy'
:
model.compile(optimizer='sparse_categorical_crossentropy', ...)
或者,你也可以对它们进行 one-hot 编码:
from keras.utils import to_categoricallabels = to_categorical(np.array(labels), num_classes=10)