问题
Maybe a very dumb question but I can't find an example how to use categorical_hinge in Keras. I do classification and my target is shape(,1)
with values [-1,0,1] so I have 3 categories. Using the functional API I have set up my output layer like this:
output = Dense(1, name='output', activation='tanh', kernel_initializer='lecun_normal')(output1)
Then I apply:
model.compile(optimizer=adam, loss={'output': 'categorical_hinge'}, metrics=['accuracy'])
The result is that the model is converging but accuracy goes towards to 0. What do I do wrong?
回答1:
While [-1, 0, 1]
is a valid target range for your tanh activation function, experience tells that Keras models don't work well with classification in a binary output. Consider using three one-hot vectors with a softmax classifier instead. If I interpret this bug report correctly, categorical hinge is built to work with one-hot vectors anyway.
So: Convert your labels to one-hots and change your output to something along the lines of:
output = Dense(3, name='output', activation='softmax', kernel_initializer='lecun_normal')(output1)
回答2:
Use:
model.compile(optimizer=adam, loss="categorical_hinge", metrics=['accuracy'])
来源:https://stackoverflow.com/questions/48492617/how-do-i-use-categorical-hinge-in-keras