Error when checking target: expected dense_3 to have shape (3,) but got array with shape (1,)

后端 未结 9 1780
不思量自难忘°
不思量自难忘° 2020-12-04 17:26

I am working on training a VGG16-like model in Keras, on a 3 classes subset from Places205, and encountered the following error:

ValueError: Error when chec         


        
相关标签:
9条回答
  • 2020-12-04 18:20

    The problem is with your label-data shape. In a multiclass problem you are predicting the probabibility of every possible class, so must provide label data in (N, m) shape, where N is the number of training examples, and m is the number of possible classes (3 in your case).

    Keras expects y-data in (N, 3) shape, not (N,) as you've problably provided, that's why it raises an error.

    Use e.g. OneHotEncoder to convert your label data to one-hot encoded form.

    0 讨论(0)
  • 2020-12-04 18:23

    if take these errors u just need to specify the last with numbers of the classes > as an example u have 6 classes u have to make that :

    model.add(Dense(6, activation='softmax'))
    

    u can use this

    num_classes=...
    

    and the last layers will be

    model.add(Dense(num_classes, activation='softmax'))
    
    0 讨论(0)
  • 2020-12-04 18:29

    Problem : expected dense_3 to have shape (3,) but got array with shape (1,)

    If you are using it for classification then the number of variables should be correct in the parameter for adding a dense layer.

    variables_for_classification=5 #change it as per your number of categories
    model.add(Dense(variables_for_classification, activation='softmax'))
    
    model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size,validation_split=0.1,callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])
    

    To make it more clear. As I was using the LSTM to predict the category of the news and the categories were 5- business, tech, politics, sports, entertainment

    In that dense function when I put 5 it worked correctly.

    0 讨论(0)
提交回复
热议问题