Reputation: 45
I am trying to get Keras working with a classification problem, which has five categorical target labels (1, 2, 3, 4, 5). For some reason I am unable to get it working, while using StratifiedKFold. X and y are NumPy arrays with shapes (500, 20) and (500, ), respectively.
The error message is "ValueError: Error when checking target: expected dense_35 to have shape (1,) but got array with shape (5,)", which leads me to think that the error definitely lies in the format of the target variable. It is also notable, that the number in "dense_35" seems to be varying for each attempt of trying to run the code.
random_state = 123
n_splits = 10
cv = StratifiedKFold(n_splits=n_splits,
random_state=random_state, shuffle=False)
def baseline_model():
nn_model = Sequential()
nn_model.add(Dense(units=50, input_dim=X.shape[1], init='normal',
activation= 'relu' ))
nn_model.add(Dense(30, init='normal', activation='relu'))
nn_model.add(Dense(10, init='normal', activation='relu'))
nn_model.add(Dense(1, init='normal', activation='softmax'))
nn_model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics = ['accuracy'])
return nn_model
for train, test in cv.split(X, y):
X_train, X_test = X[train], X[test]
y_train, y_test = y[train], y[test]
np_utils.to_categorical(y_train)
np_utils.to_categorical(y_test)
estimator = KerasClassifier(build_fn=baseline_model,
epochs=200, batch_size=5,
verbose=0)
estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)
The numpy array (y), that I am trying to split:
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5]
Upvotes: 1
Views: 231
Reputation: 1261
Since it's a classification problem, which has five categorical target labels, the last dense layer (output layer) must have 5 units:
def baseline_model():
nn_model = Sequential()
nn_model.add(Dense(units=50, input_dim=X.shape[1], init='normal',
activation= 'relu' ))
nn_model.add(Dense(30, init='normal', activation='relu'))
nn_model.add(Dense(10, init='normal', activation='relu'))
#Output layer
nn_model.add(Dense(5, init='normal', activation='softmax'))
nn_model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics = ['accuracy'])
return nn_model
Upvotes: 0
Reputation: 1633
random_state = 123
n_splits = 10
cv = StratifiedKFold(n_splits=n_splits, random_state=random_state, shuffle=False)
def baseline_model():
nn_model = Sequential(name='model_name')
nn_model.add(Dense(units=50, input_dim=X.shape[1], init='normal',
activation= 'relu', name='dense1'))
nn_model.add(Dense(30, init='normal', activation='relu', name='dense2'))
nn_model.add(Dense(10, init='normal', activation='relu', name='dense3'))
# code changed here
nn_model.add(Dense(5, init='normal', activation='softmax', name='dense4'))
nn_model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics = ['accuracy'])
return nn_model
for train, test in cv.split(X, y):
X_train, X_test = X[train], X[test]
y_train, y_test = y[train], y[test]
# the error is due to this step
# you have specified only one output in the last dense layer (dense4)
# but you are giving input of length 5
np_utils.to_categorical(y_train)
np_utils.to_categorical(y_test)
estimator = KerasClassifier(build_fn=baseline_model,
epochs=200, batch_size=5,
verbose=0)
estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)
Upvotes: 1