0
votes

I am facing the problem with input shape of model for categorical classifier

   x         y
 [1,2,3]    [0]
 [2,3,5]    [1]
 [2,1,6]    [2]
 [1,2,3]    [0]
 [2,3,5]    [0]
 [2,1,6]    [2]

then i changed the y label into categorical as

   y
  [1,0,0]
  [0,1,0]
  [0,0,1]
  [1,0,0]
  [1,0,0]
  [0,0,1]

and my x_train shape is (6000,3) y_train shape is (6000,3) x_test shape is (2000,3) y_test shape is (2000,3)

i tried this model and getting value error

model=sequential()
model.add(Dense(1, input_shape(3,), activation="softmax"))
model.compile(Adam(lr=0.5), 'categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train,y_train,epochs=50, verbose=1)

Value error: Error when checking target: expected dense_1 to have shape(None,1) but got array with shape (6000,3)

i dont understand this error. help me to sort out this

1

1 Answers

1
votes

You need an output layer for your network that matches the number of output classes. You can do it like this

X_train = np.zeros((10,3))
y_train = np.zeros((10,))

X_test = np.zeros((10,3))
y_test = np.zeros((10,))

num_classes = 3
y_train_binary = keras.utils.to_categorical(y_train, num_classes)
y_test_binary = keras.utils.to_categorical(y_test, num_classes)

input_shape = (3,)

model = Sequential()                 
model.add(Dense(16, activation='relu',input_shape=input_shape))         
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss='categorical_crossentropy',
                       optimizer='rmsprop',
                       metrics=['mae'])

model.summary()

history=model.fit(X_train,
                  y_train_binary,
                  epochs=5,
                  batch_size=8,
                  validation_data=(X_test, y_test_binary))