How do I use categorical_hinge in Keras?

吃可爱长大的小学妹 提交于 2019-12-10 14:54:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!