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

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注