Skip to content

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

@Frightera

Description

@Frightera

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.

Metadata

Metadata

Labels

type:featureThe user is asking for a new feature.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions