ValueError:检查目标时出错:预期c_acti的形状为(10,),但数组的形状为(1,)

时间:2018-12-16 17:01:59

标签: python machine-learning keras neural-network deep-learning

我正在尝试构建双向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,200
Trainable params: 120,550
Non-trainable params: 8,760,650

这可能是什么解决方案?

1 个答案:

答案 0 :(得分:0)

您可能忘记了对标签进行一次热编码(即它们是稀疏标签,例如0、1、2、3,...,9)。因此,请将损失函数更改为'sparse_categorical_crossentropy'

model.compile(optimizer='sparse_categorical_crossentropy', ...)

或者,一键式编码它们:

from keras.utils import to_categorical

labels = to_categorical(np.array(labels), num_classes=10)