如何在Keras中使用categorical_hinge? [英] How do I use categorical_hinge in Keras?
问题描述
也许是一个非常愚蠢的问题,但是我找不到在Keras中如何使用categorical_hinge的示例.我进行分类,我的目标是shape(,1)
,其值为[-1,0,1],所以我有3个类别.使用功能性API,我像这样设置了输出层:
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')(输出1)
output = Dense(1, name='output', activation='tanh', kernel_initializer='lecun_normal')(output1)
然后我申请:
model.compile(optimizer = adam,loss = {'output':'categorical_hinge'}, metrics = ['accuracy'])
model.compile(optimizer=adam, loss={'output': 'categorical_hinge'}, metrics=['accuracy'])
结果是模型正在收敛,但准确性接近0.我该怎么办?
The result is that the model is converging but accuracy goes towards to 0. What do I do wrong?
推荐答案
虽然[-1, 0, 1]
是tanh激活函数的有效目标范围,但经验表明Keras模型不适用于二进制输出中的分类.考虑使用带有softmax分类器的三个单热点向量.如果我正确解释此错误报告,则分类铰链可以与热向量.
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)
这篇关于如何在Keras中使用categorical_hinge?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!