Search code examples
machine-learningtensorflowneural-networkkeraskeras-layer

Model.fit() ValueError: Error when checking model target: expected dense_21 to have shape (None, 1) but got array with shape (1708, 66)


This the code I'm working on:

from __future__ import print_function
from keras.models import Sequential
from keras.layers import Dense
from sklearn.cross_validation import train_test_split
import numpy
numpy.random.seed(7)

data_pixels=np.genfromtxt("pixels_dataset.csv", delimiter=',')
classes_dataset=np.genfromtxt("labels.csv",dtype=np.str , delimiter='\t')
x_train, x_test, y_train, y_test = train_test_split(data_pixels, classes_dataset, test_size=0.3

x_train has a shape of (1708, 3072)

array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  1.,  1.,  1.],
       [ 1.,  1.,  1., ...,  1.,  1.,  1.],
       ..., 
       [ 0.,  0.,  0., ...,  1.,  1.,  1.],
       [ 1.,  1.,  1., ...,  1.,  1.,  1.],
       [ 0.,  0.,  0., ...,  1.,  1.,  1.]])

y_train has a shape of (1708,)

array(['7', 'f', '3', ..., '6', 'o', 'O'], 
      dtype='|S5')

the characters of y_train are

: , : ; ! è à ä Aa..Zz 0-9

model = Sequential()
model.add(Dense(12, input_dim=3072, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

l got error after executing the following :

model.fit(x_train,y_train, epochs=150, batch_size=10)

the error is

ValueError: could not convert string to float: A

l tried the following alternatives : 1)

x_train=n.array(x_train)
y_train=n.array(y_train)

2)

 model.fit(x_train,str(y_train), epochs=150, batch_size=10)

But l got the same error Then l tried another alternative

from sklearn.preprocessing import LabelBinarizer
encoder = LabelBinarizer()
y_train = encoder.fit_transform(y_train)

then l get a new error which is

ValueError: Error when checking model target: expected dense_21 to have shape (None, 1) but got array with shape (1708, 66)

Solution

  • Change the following lines of code:

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

    and:

    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    

    The problem lied in this that you wanted to predict a char which was coded as one-hot vector of length 66. In this case - you are setting your output to have desired length and you are using categorical_crossentropy loss and softmax activation.