keras icon indicating copy to clipboard operation
keras copied to clipboard

[Previous Feature Request] Throw an error if `softmax` is used incorrectly

Open Frightera opened this issue 1 year ago • 4 comments

I had a PR (#18105 - closed due to merging process) which was adding a utility function to the codebase if softmax is used with a 1 neuron as this will only produce a vector of 1s (ones).

This is a mistake mostly beginners do, and error should be raised IMHO. There are too many Stack Overflow questions because of this logical error.

Consider:

import keras
import numpy as np

keras.__version__ # 3.0.1

model = keras.Sequential([
    # ... some layers ... #
    keras.layers.Dense(1, activation = "softmax")
])

x = np.random.randn(100, 1)
if np.allclose(model.predict(x), 1.0):
    print("1 neuron with softmax only outputs 1s")

It's actually about the applied axis of the softmax, so this is valid for any layer.

If the PR above sounds reasonable, I can adapt the changes for Keras 3.0

Thanks.

Frightera avatar Dec 07 '23 23:12 Frightera

@Frightera based on the context of the old PR, do you want a open a new PR?

divyashreepathihalli avatar Dec 20 '23 21:12 divyashreepathihalli

Hi @divyashreepathihalli,

Yes, I can create a new PR if you can confirm that this feature seems reasonable and that the Keras team would be willing to merge it.

Thanks.

Frightera avatar Dec 22 '23 00:12 Frightera

yes, please go ahead.

divyashreepathihalli avatar Dec 22 '23 00:12 divyashreepathihalli

The work is currently in progress 👍🏼

Frightera avatar Jan 14 '24 20:01 Frightera