Keras: Any example on using softmax(x, axis=-1) from keras.activations?

Created on 23 Apr 2017  路  11Comments  路  Source: keras-team/keras

As mentioned in the official documentation, one can specify the axis to perform softmax activation.
But I am confused about how to use it in functional model API.

stale

Most helpful comment

When using a standard function, you can always wrap it in a Lambda layer to make it a layer.

All 11 comments

Hi,

I am having the same issue. The axis argument doesn't seem to be implemented.

Some investigations to help @fchollet and others:

from keras.layers import Activation
x = Activation('softmax', axis=1)
## does not work: TypeError: ('Keyword argument not understood:', 'axis')

And when looking at backend, I see K.softmax at https://github.com/fchollet/keras/blob/master/keras/backend/tensorflow_backend.py#L2572
which doesn't have an axis argument.

This has been fixed there : https://github.com/fchollet/keras/commit/31d821d8785979f071c578ffaa20dda79914f49c

from keras.activations import softmax
x = a tensor
out = softmax(x,axis=-1)

@Dref360: Hi, tried your suggestion as I was running into this problem. It complains that:

TypeError: softmax() got an unexpected keyword argument 'axis'

Thanks, I did. I think the issue may be that it's not a Layer object, so it can't be directly used in defining a model. It needs to be used in Activation. I don't know how that would work.

When using a standard function, you can always wrap it in a Lambda layer to make it a layer.

I had the same issue while trying to use softmax with "channels_first", where axis=1 is mandatory.
As a workaround, I used the Permute layer to move the channels axis at last position, perform the softmax and then move back channels to first position:

model.add(Permute((2,3,1))) # CHW -> HWC for softmax
model.add(Activation('softmax'))
model.add(Permute((3,1,2))) # HWC -> CHW

This could help until Activation will accept axis parameter.

@oinegue Thanks, that's helpful. It also has an issue with using categorical_crossentropy for similar reasons. Have you used that and have a workaround for that too perhaps?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

I am using tensorflow 1.8.0 and it is still showing the same error
softmax() got an unexpected keyword argument 'axis'

Yup same error

Was this page helpful?
0 / 5 - 0 ratings