【问题标题】:ValueError: Error when checking target: expected c_acti to have shape (10,) but got array with shape (1,)ValueError:检查目标时出错:预期 c_acti 的形状为 (10,) 但得到的数组的形状为 (1,)
【发布时间】:2018-12-17 01:25:05
【问题描述】:

我正在尝试构建双向 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

可能的解决方案是什么?

【问题讨论】:

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


    【解决方案1】:

    您可能忘记对标签进行一次热编码(即它们是稀疏标签,例如 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)
    

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 2021-06-30
      • 2023-03-25
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 2018-10-24
      • 2018-09-30
      • 2019-03-05
      相关资源
      最近更新 更多