keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.06k stars 19.48k forks source link

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

Closed EmilienDupont closed 8 years ago

EmilienDupont commented 8 years ago

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!

kgrm commented 8 years ago

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.

EmilienDupont commented 8 years ago

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

kvsramarao commented 7 years ago

I'm having exactly the same problem on Windows 10. I've tried every trick I could find on this forum and elsewhere but nothing has worked. The same issue arises in any Sequential setting - with LSTMs, with plain NNs, and so on. I've looked at the code in training.py where the input dimensions are checked but it's a bit abstruse how the expected shape is derived. Many thanks for your help if there's a mechanism you've found working on Windows 10.

abdulsalam-bande commented 7 years ago

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)

abdulsalam-bande commented 7 years ago

CAN SOME ONE HELP ME . THIS IS MY ERROR " Error when checking target: expected dense_15 to have shape (None, 8) but got array with shape (43314, 1)"

hafizurcse commented 7 years ago

@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.

ganav commented 7 years ago

Hello there i have same issue and flatten cannot solve it because flatten has its own error i mean flatten gives shape=(?,?) so i cannot use flatten but reshape() i even tried flatten after pooling and reshape() but gives (?,?) How to solve without using flatten? Similar error https://github.com/fchollet/keras/issues/5948

hafizurcse commented 7 years ago

@ganav your problem is not clear to me. can you please share a dummy example that is exactly similar to your problem?

ganav commented 7 years ago

@hafizurcse same code as https://github.com/fchollet/keras/issues/5948 But i am using my own CNN network. flatten returns none (?,?)

abmitra84 commented 7 years ago

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

ganav commented 7 years ago

@abmitra84 i have solved my issue using reshape but it gives me extra errors and solved that but keep giving errors finally worked without flatten. anyway i want to know why flatten gives none.

if u need my code let me share only important 3 lines for flattening: out.append(Reshape((1,size))(j)) # flattening out = keras.layers.concatenate([out[0],out_[1]], axis=-1) # concatenate out = Lambda(lambda x: K.squeeze(x, axis=0))(out) # decrease 1 dimension

if you do not use lambda and squeeze the next layer 'dense' gives error related to dimension.

SonawaneMayur commented 6 years ago

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

SonawaneMayur commented 6 years ago

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

akhiljalagam commented 6 years ago

from numpy import array from pickle import load from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences from keras.utils import to_categorical

from keras.utils import plot_model

from keras.models import Model from keras.layers import Input from keras.layers import Dense from keras.layers import LSTM from keras.layers import Embedding from keras.layers import Dropout from keras.layers.merge import add from keras.callbacks import ModelCheckpoint import itertools

load doc into memory

def load_doc(filename):

open the file as read only

file = open(filename, 'r')
# read all text
text = file.read()
# close the file
file.close()
return text

load a pre-defined list of photo identifiers

def load_set(filename): doc = load_doc(filename) dataset = list()

process line by line

for line in doc.split('\n'):
    # skip empty lines
    if len(line) < 1:
        continue
    # get the image identifier
    identifier = line.split('.')[0]
    dataset.append(identifier)
return set(dataset)

load clean descriptions into memory

def load_clean_descriptions(filename, dataset):

load document

doc = load_doc(filename)
descriptions = dict()
for line in doc.split('\n'):
    # split line by white space
    tokens = line.split()
    # split id from description
    image_id, image_desc = tokens[0], tokens[1:]
    # skip images not in the set
    if image_id in dataset:
        # create list
        if image_id not in descriptions:
            descriptions[image_id] = list()
        # wrap description in tokens
        desc = 'startseq ' + ' '.join(image_desc) + ' endseq'
        # store
        descriptions[image_id].append(desc)
return descriptions

load photo features

def load_photo_features(filename, dataset):

load all features

all_features = load(open(filename, 'rb'))
# filter features
features = {k: all_features[k] for k in dataset}
return features

covert a dictionary of clean descriptions to a list of descriptions

def to_lines(descriptions): all_desc = list() for key in descriptions.keys(): [all_desc.append(d) for d in descriptions[key]] return all_desc

fit a tokenizer given caption descriptions

def create_tokenizer(descriptions): lines = to_lines(descriptions) tokenizer = Tokenizer() tokenizer.fit_on_texts(lines) return tokenizer

calculate the length of the description with the most words

def max_length(descriptions): lines = to_lines(descriptions) return max(len(d.split()) for d in lines)

create sequences of images, input sequences and output words for an image

def create_sequences(tokenizer, max_length, descriptions, photos): X1, X2, y = list(), list(), list()

walk through each image identifier

for key, desc_list in descriptions.items():
    # walk through each description for the image
    for desc in desc_list:
        # encode the sequence
        seq = tokenizer.texts_to_sequences([desc])[0]
        # split one sequence into multiple X,y pairs
        for i in range(1, len(seq)):
            # split into input and output pair
            in_seq, out_seq = seq[:i], seq[i]
            # pad input sequence
            in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
            # encode output sequence
            out_seq = to_categorical([out_seq], num_classes=vocab_size)
            # store

            if key in photos.items():
                X1.append(photos[key][0])

            X2.append(in_seq)
            y.append(out_seq)
return array(X1), array(X2), array(y)

define the captioning model

def define_model(vocab_size, max_length):

feature extractor model

 inputs1 = Input(shape=(4096,))
 fe1 = Dropout(0.5)(inputs1)
 fe2 = Dense(256, activation='relu')(fe1)
# sequence model
 inputs2 = Input(shape=(max_length,))
 se1 = Embedding(vocab_size, 256, mask_zero=True)(inputs2)
 se2 = Dropout(0.5)(se1)
 se3 = LSTM(256)(se2)
# decoder model
 decoder1 = add([fe2, se3])
 decoder2 = Dense(256, activation='relu')(decoder1)
 outputs = Dense(vocab_size, activation='softmax')(decoder2)
# tie it together [image, seq] [word]
 model = Model(inputs=[inputs1, inputs2], outputs=outputs)
 model.compile(loss='categorical_crossentropy', optimizer='adam')
# summarize model
 print(model.summary())
# plot_model(model, to_file='model.png', show_shapes=True)
 return model

train dataset

load training dataset (6K)

filename = 'Flickr8k_text/Flickr_8k.trainImages.txt' train = load_set(filename) print('Dataset: %d' % len(train))

descriptions

train_descriptions = load_clean_descriptions('descriptions.txt', train) print('Descriptions: train=%d' % len(train_descriptions))

photo features

train_features = load_photo_features('features.pkl', train) print('Photos: train=%d' % len(train_features))

prepare tokenizer

tokenizer = create_tokenizer(train_descriptions) vocab_size = len(tokenizer.word_index) + 1 print('Vocabulary Size: %d' % vocab_size)

determine the maximum sequence length

max_length = max_length(train_descriptions) print('Description Length: %d' % max_length)

print(train_descriptions.type())

print(train_features.type())

interdesc = dict(itertools.islice(train_descriptions.items(),1000)) interfeatures = dict(itertools.islice(train_features.items(),1000))

print(train_descriptions)

print(interfeatures)

prepare sequences

X1train, X2train, ytrain = create_sequences(tokenizer, max_length, interdesc, interfeatures)

dev dataset

load test set

filename = 'Flickr8k_text/Flickr_8k.devImages.txt' test = load_set(filename) print('Dataset: %d' % len(test))

descriptions

test_descriptions = load_clean_descriptions('descriptions.txt', test) print('Descriptions: test=%d' % len(test_descriptions))

photo features

test_features = load_photo_features('features.pkl', test) print('Photos: test=%d' % len(test_features))

prepare sequences

X1test, X2test, ytest = create_sequences(tokenizer, max_length, test_descriptions, test_features)

fit model

define the model

model = define_model(vocab_size, max_length)

define checkpoint callback

filepath = 'model-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5' checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

fit model

model.fit([X1train, X2train], ytrain, epochs=20, verbose=2, callbacks=[checkpoint], validation_data=([X1test, X2test], ytest))

error msg; model.fit([X1train, X2train], ytrain, epochs=20, verbose=2, callbacks=[checkpoint], validation_data=([X1test, X2test], ytest)) File "C:\Users\pranyaram\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 1630, in fit batch_size=batch_size) File "C:\Users\pranyaram\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 1476, in _standardize_user_data exception_prefix='input') File "C:\Users\pranyaram\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 123, in _standardize_input_data str(data_shape)) ValueError: Error when checking input: expected input_1 to have shape (4096,) but got array with shape (1,)

got an error how to resolve

prateekbhadauria commented 6 years ago

My program also gives this error and i could not find my fault kindly help ,its a regressor type problem i want to implement CNN for an array of 50000 cross 20 . ValueError: Error when checking target: expected dense_9 to have shape (1,) but got array with shape (20,) import numpy as np import matplotlib as plt import tensorflow as tf from sklearn.preprocessing import StandardScaler from keras import models from keras import layers from sklearn.metrics import mean_squared_error seed = 7 np.random.seed(seed) from scipy.io import loadmat dataset = loadmat('matlab2.mat') Bx=basantix[:, 50001:99999]
Bx=np.transpose(Bx)
Fx=fx[:, 50001:99999] Fx=np.transpose(Fx) from sklearn.cross_validation import train_test_split Bx_train, Bx_test, Fx_train, Fx_test = train_test_split(Bx, Fx, test_size=0.2, random_state=0) scaler = StandardScaler()
scaler.fit(Bx_train)
Bx_train = scaler.transform(Bx_train) Bx_test = scaler.transform(Bx_test)

mean = Bx_train.mean(axis=0) Bx_train -= mean std = Bx_train.std(axis=0) Bx_train /= std Bx_test-= mean Bx_test /= std

def build_model(): model = models.Sequential() model.add(layers.Dense(64, activation='relu', input_shape=(Bx_train.shape[1],))) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(1)) model.compile(optimizer='rmsprop', loss='mean_squared_error') return model model=build_model() model.fit(Bx_train, Fx_train, epochs=80, batch_size=16, verbose=0) test_mse_score = model.evaluate(Fx, Bx)

debadridtt commented 6 years ago

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?

shamimnazrul14 commented 6 years ago

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Error when checking target: expected dense_8 to have shape (113,) but got array with shape (100,)

v-mostafapour commented 6 years ago

I am new in keras and tensorflow . I am trying to get an input tf tensor by keras.layers.InputLayer(tensor) and continue by keras sequential model but cant determine my input then throws this error:

Error massage

ValueError: ('Error when checking model input: expected no data, but got:', array([[[ 0, 1, 2, ...

here is my code:

num_steps = 70 # 50

input_word = tf.placeholder(dtype=tf.int32, shape=[None, num_steps], name='input_word') word_embedding = tf.get_variable(initializer=pretrained_embedding, name='word_embedding')

inputs_ = tf.nn.embedding_lookup(word_embedding, input_word)

defining my sequential model

test_model = Sequential() test_model.add(InputLayer(input_tensor=inputs_forward, input_shape=inputs_forward.shape )) test_model.add(Dense(7, activation='softmax'))

test_model.summary()

test_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

test_model.fit(x=train_word, y=train_y, epochs=1, batch_size=70)

rosefun commented 6 years ago
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 .

Wazaki-Ou commented 6 years ago

I am facing a similar issue with my Bidirectional LSTM. Can anyone please take a look and help me find the issue? I don't want to open a new issue. https://stackoverflow.com/questions/52800239/keras-bidirectional-lstm-error-when-checking-target Thanks a lot !!

Clinton237 commented 6 years ago

hello all i am facing the same issue. i am working on a model to presidct the presence or absence of plasmodium parasite and i got the error below.

ValueError: Error when checking target: expected activation_99 to have shape (2,) but got array with shape (1,)

here is the code below: from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining() model_trainer.setModelTypeAsResNet() model_trainer.setDataDirectory("idenpara") model_trainer.trainModel(num_objects=2, num_experiments=200, enhance_data=True, batch_size=32, show_network_summary=True)

sravankumar-challa commented 5 years ago

Just update keras version to 2.1.2 pip install keras==2.1.2

MariamBiltawi commented 5 years ago

Hello can anyone help me please I am learning to use Keras, and I have used the same code for question answering from the book entitled "Deep learning with python", and it was perfectly running for bAbi dataset but when I changed the dataset things went wrong, within the new dataset there are more unique tokens. The following is the error I got: ValueError: Error when checking target: expected dense_1 to have shape (1000,) but got array with shape (100,)

The code is:

text_input = Input(shape=(None,), dtype='int32', name='text') embedded_text = layers.Embedding(output_dim=64, input_dim=text_vocabulary_size, input_length=passage_max_length)(text_input) encoded_text = layers.LSTM(32)(embedded_text)

question_input = Input(shape=(None,), dtype='int32',name='question') embedded_question = layers.Embedding(output_dim=32, input_dim=question_vocabulary_size, input_length=question_max_length)(question_input) encoded_question = layers.LSTM(16)(embedded_question) concatenated = layers.concatenate([encoded_text, encoded_question], axis=-1) answer = layers.Dense(answer_vocabulary_size, activation='softmax')(concatenated) model = Model([text_input, question_input], answer) model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['acc']) print('Training') model.fit([train_texts_data, train_questions_data],train_answers_data, epochs=epochs, batch_size=batch_size) print('Evaluation') loss, acc = model.evaluate([test_texts_data, test_questions_data], test_answers_data, batch_size=batch_size) print('Test loss / test accuracy = {:.4f} / {:.4f}'.format(loss, acc))

Synoxy commented 5 years ago

Hello can anyone help me please I am learning to use Keras, and I have used the same code for question answering from the book entitled "Deep learning with python", and it was perfectly running for bAbi dataset but when I changed the dataset things went wrong, within the new dataset there are more unique tokens. The following is the error I got: ValueError: Error when checking target: expected dense_1 to have shape (1000,) but got array with shape (100,)

The code is:

text_input = Input(shape=(None,), dtype='int32', name='text') embedded_text = layers.Embedding(output_dim=64, input_dim=text_vocabulary_size, input_length=passage_max_length)(text_input) encoded_text = layers.LSTM(32)(embedded_text)

question_input = Input(shape=(None,), dtype='int32',name='question') embedded_question = layers.Embedding(output_dim=32, input_dim=question_vocabulary_size, input_length=question_max_length)(question_input) encoded_question = layers.LSTM(16)(embedded_question) concatenated = layers.concatenate([encoded_text, encoded_question], axis=-1) answer = layers.Dense(answer_vocabulary_size, activation='softmax')(concatenated) model = Model([text_input, question_input], answer) model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['acc']) print('Training') model.fit([train_texts_data, train_questions_data],train_answers_data, epochs=epochs, batch_size=batch_size) print('Evaluation') loss, acc = model.evaluate([test_texts_data, test_questions_data], test_answers_data, batch_size=batch_size) print('Test loss / test accuracy = {:.4f} / {:.4f}'.format(loss, acc))

Can you tell me what values have you assigned to:- question_vocabulary_size and text_vocabulary_size

MariamBiltawi commented 5 years ago

text_vocabulary_size = 60000 question_vocabulary_size = 1000 Am not sure if they are reasonable values, but the number of unique tokens in the dataset for the passages is more than 60000 and for the questions is more than 1000.

Synoxy commented 5 years ago

text_vocabulary_size = 60000 question_vocabulary_size = 1000 Am not sure if they are reasonable values, but the number of unique tokens in the dataset for the passages is more than 60000 and for the questions is more than 1000.

i had a similar error while i was trying to train a model for English alphabets the error was in mismatch of values when i added layers to the model.

henryhust commented 5 years ago

(Chinese words)报错的原因是因为输入模型的数据shape问题,model中的dim与你构造的数据维度不一致导致的。 (English)I think, It's because the shape of you input data is not match with the model dim. when you use the category_crossentropy , the dim of your y_train should be 10. So, change your loss function to "sparse_category_crossentropy" or change the y_train to [[0,1,0,0,0,0,0,0,0,0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]] .

mumuyanyan commented 5 years ago
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 .

虽然解决了,但是为啥呀

vahab-mspour commented 5 years ago

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).

chan

from keras.utils import to_categorical # import this guy

X_train = np.array([[1,2], [6,5], [8,2]]) y_train = np.array([2,3,7]) input_dim = X_train.shape[1] y_train = to_categorical(y_train,10)# this guy synchronizes the size of labels with output layer ==10 print(y_train) model = Sequential() print(input_dim) model.add(Dense(output_dim=64, input_dim=input_dim)) model.add(Activation("relu")) model.add(Dense(output_dim=10))# labels dimension should be "10" but yours was "1"
model.add(Activation("softmax")) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.summary() model.fit(X_train, y_train, nb_epoch=5, batch_size=32)

dimension of output and label set should be the same in "categorical_crossentropy"

dim of your y_train is 1 but the output is set to 10

encode [2,3,7] to [[0,0,1,0,...],[0,0,0,1,0,...],[0,0,...,0,1,0,0,0]] by y_train = to_categorical(y_train,10)

ghost commented 5 years ago

@debadridtt I'm still learning this, but I think it depends on how your training labels are set up. Isn't binary classification a special case where we can use a network with just one output? Single Output Node: 0 for ClassA and 1 for ClassB. You could use labels consisting of a single digit [0] or [1]. These are of course 1 dimensional vectors.

Wheras classifying between three classes, you really need to switch architechures and have 3 output nodes: Class A, Class B, Class C. You would need one hot encoded labels like [0 0 1], [0 1 0], [1, 0, 0] and these are 3-dimensional.

Just thoughts, I arrived here because I had the error above!

sumitjha4321 commented 4 years ago

In my case the error was that I was missing to Flatten before the fully connected layer begins.

model = Sequential()
model.add(Conv2D(6, kernel_size=(5, 5), activation='relu', use_bias=True, input_shape=(32, 32, 3)))
model.add(AveragePooling2D(pool_size=(2, 2)))
model.add(Conv2D(16, kernel_size=(5, 5), activation='relu', use_bias=True))
model.add(AveragePooling2D(pool_size=(2, 2)))
model.add(Conv2D(120, kernel_size=(5, 5), activation='relu', use_bias=True))
model.add(Flatten()) # <------ Flattening before Dense layer
model.add(Dense(84, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))
savin333 commented 4 years ago

In my case i just changed my dense layer

cnn.add(Dense(1))
cnn.add(Activation('sigmoid'))

to

cnn.add(Dense(3))
cnn.add(Activation('softmax'))

Because, I'm using 3 classes. Changing categorical_crossentropy to sparse_categorical_crossentropy didn't solved my error.

cnn.compile(loss = 'categorical_crossentropy', optimizer = 'adadelta', metrics = ['accuracy'])

I think your last dense layer should equal to the number of classes you have.

s4lm-xi commented 4 years ago

Having the same issue but can't be fixed by any of the solutions given, pls help, here's the code. I am trying to make an output of 2 classes but the model showing me that I have only 1 class. `


size = 50 #size of the image 50x50 
batch_size = 120 #batch size for each epoch
epochs = 42 

#directory for the train and test set
train_data_dir = 'Dataset/Train'
test_data_dir = 'Dataset/Test'

#data generating objects
datagen_train = ImageDataGenerator()
datagen_test = ImageDataGenerator()

#number of pictures in each directory
train_samples = 3200
test_samples = 800
#Neural Network

model = Sequential()
#the pictures are in grayscale color
input_shape = (size, size, 1)

model.add(Conv2D(32,(3,3), padding='same', input_shape = input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))

model.add(Conv2D(64, (5,5), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))

model.add(Conv2D(128, (5,5), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))

model.add(Conv2D(256, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(2,2))
model.add(Dropout(0.25))    

model.add(Flatten())    

model.add(Dense(32))
model.add(Activation('relu'))
model.add(Dropout(0.25))

model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dropout(0.25))

#output layer
model.add(Dense(2, activation='softmax'))#this where the problem is
#compiling the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

if i change model.add(Dense(1, activation='softmax') it works but of course, I have a binary problem which the output should be only 0's and 1's so the output must be 2, so it gave me the following error: 'ValueError: Error when checking target: expected dense_3 to have shape (2,) but got array with shape (1,)'

pls, help me it would very be appreciated. Thanks ;D

ghost commented 4 years ago

If you comment your code it would help us know what you are assuming and where you might be going wrong.

On 28 Mar 2020, at 12:19 am, S4lm notifications@github.com wrote:

Having the same issue but can't be fixed by any of the solutions given, pls help, here's the code. I am trying to make an output of 2 classes but the model showing me that I have only 1 class. ` size = 50 batch_size = 120 epochs = 42 train_data_dir = 'Dataset/Train' test_data_dir = 'Dataset/Test' datagen_train = ImageDataGenerator() datagen_test = ImageDataGenerator()

samples

train_samples = 3200 test_samples = 800

Neural Network

model = Sequential()

input_shape = (size, size, 1)

model.add(Conv2D(32,(3,3), padding='same', input_shape = (50,50,1))) model.add(Activation('relu')) model.add(MaxPooling2D(2,2)) model.add(Dropout(0.25))

model.add(Conv2D(64, (5,5), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(2,2)) model.add(Dropout(0.25))

model.add(Conv2D(128, (5,5), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(2,2)) model.add(Dropout(0.25))

model.add(Conv2D(256, (3,3), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(2,2)) model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(32)) model.add(Activation('relu')) model.add(Dropout(0.25))

model.add(Dense(16)) model.add(Activation('relu')) model.add(Dropout(0.25))

output layer

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

compiling the model

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) if i change model.add(Dense(1, activation='softmax`) it works but ofcourse i have a binary problem which the output should be only 0's and 1's pls help me it would very appreciated. Thanks ;D

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras/issues/3109#issuecomment-604995890, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABA4TQBWEWG75QR7M6QXQV3RJSRUXANCNFSM4CIGZIQA.

s4lm-xi commented 4 years ago

Done, hope it helps in understanding a bit, summarising my problem is that I am trying to predict either the picture is negative(0), or positive(1), which made me enter model.add(Dense(2, Activation='softmax) because I have a binary problem, but the error is giving me that I have only one class to predict, and i don't have two outputs which are strange, even though I imported everything correctly double-checked almost everything.

Wazaki-Ou commented 4 years ago

@S4lm4no Have you tried using sigmoid instead of softmax in your last layer while keeping the output 1 ?

sachinsavale2007 commented 4 years ago

@S4lm4no your picture is negative(0), or positive(1) hence you used 2 but at a time it will be either 0 or 1 but not both hence you should use 1 as output_dim/units, hope this is what you are looking for.

s4lm-xi commented 4 years ago

It worked, I changed the activation for the last layer and set the Dense to 1, but didn't work, after coming from a long break I updated all the libraries and my editor it seemed to work, I simply can't thank you @Wazaki-Ou & @sachinsavale2007 enough! ;D

Adinarayanreloaded commented 3 years ago

I am getting an error of expected activation_2 to have shape (None,7) but got array with shape (1400,8)