简体   繁体   English

Keras密集层输出-形状错误

[英]Keras Dense Layer Output - Shape Error

I was working on an NLP problem with LSTM. 我正在使用LSTM解决NLP问题。 The problem is a multiclass classification with 3 classes (1,2 & 3). 问题是具有3个类别(1,2和3)的多类别分类。 So I converted the target classes with the following code: y_train=to_catgorical(y_train) and similarly for y_test . 因此,我使用以下代码转换了目标类: y_train=to_catgorical(y_train)并类似地为y_test But in the output dense layer when I write model.add((Dense(3,activation='softmax')) , I get the following error: 但是在我写model.add((Dense(3,activation='softmax'))的输出密集层中,出现以下错误:

Error when checking target: expected dense_1 to have shape (None, 3) but got array with shape (658118, 4) 检查目标时出错:预期density_1具有形状(None,3),但数组的形状(658118,4)

However when I fix it to model.add((Dense(4,activation='softmax')) , ie n+1 (n=no. of classes to be predicted) it works. But in Keras examples they have worked with cifar10 dataset and have used number of classes as 10, not 11 and it works. Also in case of binary classification (2 classes to be predicted) we use just 1 ouptut, ie model.add(Dense(1,"sigmoid")) . I've gone through this post but still not been able to find a convincing logic for this thing, so I want to get this concept cleared with the Dense layer output shape in Keras. 但是,当我将其修复为model.add((Dense(4,activation='softmax')) ,即n + 1(n =要预测的类数),它可以工作, 但是在Keras示例中,它们已经与cifar10一起工作了。数据集,并且使用的类数为10,而不是11,并且可以工作,在二进制分类的情况下(要预测2个类),我们仅使用1个输出,即model.add(Dense(1,"sigmoid")) 。我已经看完了这篇文章,但仍然无法找到令人信服的逻辑,因此我想通过Keras中的Dense层输出形状来清除此概念。

PS What I've understood is, Keras considers classes from 0:num_classes , so we have to give one more transformation. PS我了解的是,Keras考虑从0:num_classes类,因此我们必须再进行一次转换。 But to ask one thing that '0' vector layer then has to remain unused right? 但是要问一件事,那么“ 0”向量层必须保留未使用的权利吗? And in case of cifar10 dataset, is it that the classes are 0:9 , that's why num_classes=10 work? 在cifar10数据集的情况下,是否类是0:9 ,这就是为什么num_classes = 10起作用的原因? If that is the case then if we have to make a prediction of 3 classes say (0,1 & 2) then we can use num_classes=3 right? 如果真是这样,那么如果我们必须对3个类(0,1&2)进行预测,那么我们可以使用num_classes = 3对吗?

Perhaps it is important to note the use of Keras framework to provide an one hot encoding function: 也许重要的是要注意使用Keras框架来提供一种热编码功能:

from keras.datasets import cifar10
from keras.utils import np_utils    
y_label_train_OneHot = np_utils.to_categorical(y_label_train)
y_label_test_OneHot = np_utils.to_categorical(y_label_test)

You can see this code code In short, I personally think that the use of different functions lead to a different set of categories. 您可以看到此代码代码简而言之,我个人认为使用不同的功能会导致一组不同的类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM