return a large negative number based on dtype if backend standardizedtypedtype float16 return 3e4 return 1e9 kerasexportkeras layers softmax class softmaxlayer def initself axis1 kwargs super initkwargs self supportsmasking true self axis axis self built true def callself inputs masknone if mask is not none adder 1 0 backend castmask inputs dtype largenegativenumberinputs dtype inputs adder if isinstanceself axis tuple list if lenself axis 1 return backend numpy exp inputs backend math logsumexp inputs axisself axis keepdimstrue else return activations softmaxinputs axisself axis0 return activations softmaxinputs axisself axis def getconfigself config super getconfig config updateaxis self axis return config def computeoutputshapeself inputshape return inputshape return a large negative number based on dtype softmax activation layer formula python exp_x exp x max x f x exp_x sum exp_x example softmax_layer keras layers activations softmax input np array 1 0 2 0 1 0 result softmax_layer input 0 21194157 0 5761169 0 21194157 args axis integer or list of integers axis along which the softmax normalization is applied kwargs base layer keyword arguments such as name and dtype call arguments inputs the inputs logits to the softmax layer mask a boolean mask of the same shape as inputs the mask specifies 1 to keep and 0 to mask defaults to none returns softmaxed output with the same shape as inputs
from keras import activations
from keras import backend
from keras.api_export import keras_export
from keras.layers.layer import Layer


def _large_negative_number(dtype):
    
    if backend.standardize_dtype(dtype) == "float16":
        return -3e4
    return -1e9


@keras_export("keras.layers.Softmax")
class Softmax(Layer):
    

    def __init__(self, axis=-1, **kwargs):
        super().__init__(**kwargs)
        self.supports_masking = True
        self.axis = axis
        self.built = True

    def call(self, inputs, mask=None):
        if mask is not None:
            adder = (
                1.0 - backend.cast(mask, inputs.dtype)
            ) * _large_negative_number(inputs.dtype)
            inputs += adder
        if isinstance(self.axis, (tuple, list)):
            if len(self.axis) > 1:
                return backend.numpy.exp(
                    inputs
                    - backend.math.logsumexp(
                        inputs, axis=self.axis, keepdims=True
                    )
                )
            else:
                return activations.softmax(inputs, axis=self.axis[0])
        return activations.softmax(inputs, axis=self.axis)

    def get_config(self):
        config = super().get_config()
        config.update({"axis": self.axis})
        return config

    def compute_output_shape(self, input_shape):
        return input_shape