keras: Error when checking model target: expected activation_2 to have shape (None, 10) but got array with shape (3, 1)

Hi,

I’ve synced to the latest master branch (Keras 1.0.5) and am getting the following exception.

Exception: Error when checking model target: expected activation_2 to have shape (None, 10) but got array with shape (3, 1)

I’m running the code in a conda environment with the Tensorflow backend (on Mac OS X).

I’m running the following

X_train = np.array([[1,2], [6,5], [8,2]])
y_train = np.array([2,3,7])
input_dim = X_train.shape[1]

model = Sequential()

model.add(Dense(output_dim=64, input_dim=input_dim))
model.add(Activation("relu"))
model.add(Dense(output_dim=10))
model.add(Activation("softmax"))
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

model.fit(X_train, y_train, nb_epoch=5, batch_size=32)

I’m synced with the master branch, but have also tried this with Keras 1.0.3, neither works. Do you have any idea what this issue could be stemming from?

Cheers

Note this is very similar to issue #3009 , but no real solution was found. Could this be due to the fact that I am running in the conda environment?

Please make sure that the boxes below are checked before you submit your issue. Thank you!

  • [x ] Check that you are up-to-date with the master branch of Keras. You can update with: pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
  • [x ] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with: pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
  • [x ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 29
  • Comments: 39

Most upvoted comments

Your model has an output of shape (10,), however, your outputs have dimension (1,). You probably want to convert your y_train to categorical one-hot vectors, ie, via keras.utils.np_utils.to_categorical.

Thanks for your answer @kgrm , I used sparse_categorical_crossentropy to solve my problem

@yazidabdulsalam @hafizurcse My issue got solve by using loss =“sparse_categorical_crossentropy” Thanks.

@yazidabdulsalam Hey mate,

Not sure if you still need any help.

Just wanna explain your message and then explain what to do. By your error message " Error when checking target: expected dense_15 to have shape (None, 8) but got array with shape (43314, 1)" means that you supposed to feed a 2D array of shape(x, 8) such as (1, 8), or (2, 8) or (3, 8) or … … or (n, 8). That clears that x is a variable and you can have any number of rows but you must provide 8 columns. That is what “None” stands for.

Having same issue as ganav on Win7 anaconda setup. Keras version 2.0.9.

My code is inspired by this https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html just that I am trying multi class classification with optimizer = ‘adam’ or ‘rmsprop’ loss = ‘categorical_crossentropy’ batch_size = 16 number of classes = 10

ValueError: Error when checking target: expected activation_5 to have shape (None, 1) but got array with shape (16, 10)

P.S. This worked perfectly for binary classificaiton with optimizer = ‘adam’ or ‘rmsprop’ loss = ‘binary_crossentropy’

EDIT: My issue is resolved, it was a mistake by not making Dense=number of classes in my final layer

Importing the libraries

import numpy as np import matplotlib.pyplot as plt import pandas as pd

Importing the dataset

train_dataset = pd.read_csv(‘train.csv’) test_dataset = pd.read_csv(‘test.csv’) X = train_dataset.iloc[:, 1:94].values y = train_dataset.iloc[:, 94].values

Encoding categorical data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder labelencoder_y_1 = LabelEncoder() y = labelencoder_y_1.fit_transform(y)

Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)

Feature Scaling

from sklearn.preprocessing import StandardScaler sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test)

#Part 2 -Lets make the ANN

#importing the keras libraries and keras import keras from keras.models import Sequential from keras.layers import Dense

#initializing the ANN classifier = Sequential()

#adding an input layer and the first hidden layer classifier.add(Dense(output_dim = 50,init =‘uniform’,activation = ‘relu’,input_dim=93))

#adding the second hidden layer classifier.add(Dense(output_dim =50,init =‘uniform’,activation = ‘relu’))

#adding the third hidden layer classifier.add(Dense(output_dim =50,init =‘uniform’,activation = ‘relu’))

#adding the forth hidden layer classifier.add(Dense(output_dim =50,init =‘uniform’,activation = ‘relu’))

#Adding the output layer #if you dependent variable is more than 1 categorie use softmax as the activation function, the ouput dim will also be your number of features classifier.add(Dense(output_dim = 8 ,init =‘uniform’,activation = ‘softmax’))

#Compiling the ANN #categorical_crossentropy classifier.compile(optimizer = ‘adam’ , loss = ‘categorical_crossentropy’, metrics = [‘accuracy’])

#Fitting the ANN to the traning set classifier.fit(X_train,y_train,batch_size = 10 ,nb_epoch=100)

def baseline_model(Xtrain,):
        # create model
        model = Sequential()
        model.add(Dense(50, input_dim=X_train.shape[1], init='normal'))
        model.add(PReLU())
        model.add(Dropout(0.2))
        model.add(Dense(output_dim=22, init='normal', activation='softmax'))
        # Compile model
        model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])  #logloss
        return model

You can try to use categorical_crossentropy instead of sparse_categorical_crossentropy .

Can anyone please tell me while solving a multiclass classification problem, why do we write n+1 variables in the Dense layer in Keras and 1 in case of binary classification not 2 or 3? Like if there are 3 classes to be predicted we write model.add(Dense(4,activation='softmax')) but incase of binary classification we write model.add(Dense(1,activation='sigmoid'))? Is there something like softmax uses more layers than sigmoid function or something? What is the reason?

@yazidabdulsalam what you did to solve your issue? I am also facing same issue in R but not getting what @hafizurcse trying to say.