Python TypeError: softmax() got an unexpected keyword argument ‘axis’

There are several solutions to this problem. You can lower the version of keras, for example:

pip install keras==2.1

However, there is a more convenient way. From the error, we can see that softmax does not contain the axis parameter, so we can replace the axis parameter with dim. The source code is as follows:

def softmax(x, axis=-1):
    """Softmax of a tensor.

    # Arguments
        x: A tensor or variable.
        axis: The dimension softmax would be performed on.
            The default is -1 which indicates the last dimension.

    # Returns
        A tensor.
    """
    return tf.nn.softmax(x, axis=axis)

Change to this:

def softmax(x, axis=-1):
    """Softmax of a tensor.

    # Arguments
        x: A tensor or variable.
        axis: The dimension softmax would be performed on.
            The default is -1 which indicates the last dimension.

    # Returns
        A tensor.
    """
    return tf.nn.softmax(x, dim=axis)

That’s to change the last line

Similar Posts: