John Wills
John Wills

Reputation: 5

Keras model only working with Sigmoid activation

I'm building a Keras model to categorise data into one of 9 categories. The issue is it will only work with a Sigmoid activation which is designed for binary outputs, other activations result in 0 accuracy. What would I need to change for it to classify into each of the labels?

#Reshape data to add new dimension
X_train = X_train.reshape((100, 150, 1)) 
Y_train = X_train.reshape((100, 1, 1)) 
model = Sequential() 
model.add(Conv1d(1, kernel_size=3, activation='relu', input_shape=(None, 1))) 
model.add(Dense(1)) 
model.add(Activation('sigmoid')) 

model.compile(loss='categorical_hinge', optimizer='adam', metrics=['accuracy']) 
model.fit(x=X_train,y=Y_train, epochs=200, batch_size=20)

Upvotes: 0

Views: 246

Answers (1)

desertnaut
desertnaut

Reputation: 60321

A single-unit dense layer is not what we use in the case of multi-class classification; you should first ensure that your Y data are one-hot encoded - if not, you can make them so using Keras utility functions:

num_classes=9
Y_train = keras.utils.to_categorical(Y_train, num_classes)

and then change your last layer to:

model.add(Dense(num_classes)) 
model.add(Activation('softmax')) 

Also, if you don't have any specific reasons to use the categorical Hinge loss, I would suggest starting with loss='categorical_crossentropy' in your model compilation.

That said, your model seems too simple, and you may want to try adding some more layers...

Upvotes: 1

Related Questions