keras softmax classification always get 1

1,962 views
Skip to first unread message

Angela ZHOU

unread,
Apr 5, 2016, 4:24:40 AM4/5/16
to Keras-users

model = Sequential()
model.add(LSTM(32,input_dim=4, return_sequences=True)) # returns a sequence of vectors of dimension 32
model.add(Dropout(0.5))
model.add(LSTM(32, return_sequences=False)) # returns a sequence of vectors of dimension 32
model.add(Dropout(0.5))
"""model.add(LSTM(32)) # return a single vector of dimension 32"""
model.add(Dense(1,input_dim=16))
model.add(Activation("softmax"))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')


model.fit(x_t, y_t, batch_size=64, nb_epoch=200,
validation_split=0.2, show_accuracy=True)

predicted = model.predict(x_tt)


x_t is 4 dimension numeric normalized data
y_t is 1 and 0

However, I always get 1 in predicted results.

Could anyone help?

Matias Valdenegro

unread,
Apr 5, 2016, 6:16:54 AM4/5/16
to keras...@googlegroups.com
Having just one output with a softmax activation doesn't make much sense,
since the softmax will normalize the output, and since there is only one
element, it will always be set to one.

If you want binary output, then use two outputs with the softmax.

angel...@koiinvestments.com

unread,
Apr 6, 2016, 10:25:36 PM4/6/16
to Keras-users
Sorry. I think my understanding must be wrong somewhere. In my example, the target is y which has value 0 or 1.  If I put output node as 2, it requires two dimension output y1, y2. Right? Could you give me a example how to change the output to 2 if you only predict 1 element?

Matias Valdenegro

unread,
Apr 7, 2016, 6:15:17 AM4/7/16
to keras...@googlegroups.com
The softmax function outputs a probability distribution over your labels,
that's why it is a two dimensional output, the probability for label 0 and for
label 1.
To get the one dimensional output, you take the label with the biggest
probability (argmax).

Also Keras expects your labels one-hot vectors for the categorical
crossentropy, you can convert numerical labels to this format with
keras.np_utils.to_categorical
Reply all
Reply to author
Forward
0 new messages