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

15,947

Solution 1

Try this:

import tensorflow as tf 

Then add a softmax layer in this way:

model.add(Activation(tf.nn.softmax))

Solution 2

Upgrade your tensoflow and Keras libraries to latest versions. Lower versions don't support softmax axis. Make sure you are upgrading them in the environment in which you are running the program (very important).

Solution 3

The reason why assert this error is version of tensorflow and keras is mismatch. I have slove this problem:

pip install tensorflow==1.5.0

If you donot want to downdegree keras, tf 1.5.0 is the first version that support softmax(axis=axis).

Share:
15,947
Aakash aggarwal
Author by

Aakash aggarwal

I am a software developer always ready to learn new things.

Updated on June 05, 2022

Comments

  • Aakash aggarwal
    Aakash aggarwal almost 2 years

    When I use this it does not give any error

    out_layer = tf.add(tf.matmul(layer_4 , weights['out']) , biases['out'])
    out_layer = tf.nn.softmax(out_layer)
    

    But when I use this

    model=Sequential()
    
    model.add(Dense(100, input_dim= n_dim, 
    activation='tanh',kernel_initializer='uniform'))
    keras.layers.core.Dropout(0.3, noise_shape=None, seed=None)
    
    model.add(Dense(50,input_dim=1000,activation='sigmoid'))
    keras.layers.core.Dropout(0.4, noise_shape=None, seed=None)
    
    model.add(Dense(15,input_dim=500,activation='sigmoid'))
    keras.layers.core.Dropout(0.2, noise_shape=None, seed=None)
    
    model.add(Dense(units=n_class))
    model.add(Activation('softmax'))
    

    I get error as

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

    What should I do? I am using python2 Thanks

  • Elik
    Elik almost 6 years
    @SAEERON Hey it works for me, but when I try to save the model and load it, I got the same error: TypeError: softmax() got an unexpected keyword argument 'axis'. My env is: OSX, keras=2.1.6, tf=1.1.0
  • puifais
    puifais over 5 years
    What is the equivalent of model.add(Dense(4, activation='softmax'))? i.e. how do you use your syntax (model.add(Activation(tf.nn.softmax))) for multi-class output?
  • Saeed Zhiany
    Saeed Zhiany over 4 years
    Please complete your answer and provide the necessary commands for updating the package.