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

Created on 12 Mar 2018  Β·  53Comments  Β·  Source: keras-team/keras

I ran my code ran without any error back to November 2017. This error happened after updated to 2.1.5. Please help. Thank you.

`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
89 model.add(Dropout(0.5))
90 model.add(Dense(num_classes))
---> 91 model.add(Activation('softmax'))
92
93 # -----------------------------------------------------------

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in add(self, layer)
490 output_shapes=[self.outputs[0]._keras_shape])
491 else:
--> 492 output_tensor = layer(self.outputs[0])
493 if isinstance(output_tensor, list):
494 raise TypeError('All layers in a Sequential model '

/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in __call__(self, inputs, *kwargs)
617
618 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 619 output = self.call(inputs, *
kwargs)
620 output_mask = self.compute_mask(inputs, previous_mask)
621

/usr/local/lib/python2.7/dist-packages/keras/layers/core.pyc in call(self, inputs)
301
302 def call(self, inputs):
--> 303 return self.activation(inputs)
304
305 def get_config(self):

/usr/local/lib/python2.7/dist-packages/keras/activations.pyc in softmax(x, axis)
27 ndim = K.ndim(x)
28 if ndim == 2:
---> 29 return K.softmax(x)
30 elif ndim > 2:
31 e = K.exp(x - K.max(x, axis=axis, keepdims=True))

/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in softmax(x, axis)
2957 A tensor.
2958 """
-> 2959 return tf.nn.softmax(x, axis=axis)
2960
2961

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

Most helpful comment

Hi,
I get the same problem but I got a better solution.
Just downgrade the tensorflow and keras.
My previous tensorflow version is 1.4.1 and keras version 2.1.5.
I downgrade to tensorflow version 1.4.0 and keras version 2.0.8.
The error doesn't appear anymore.

All 53 comments

I got exactly the same error by running an example code on your site (https://github.com/keras-team/keras/blob/master/examples/cifar10_resnet.py)

`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
322 model = resnet_v2(input_shape=input_shape, depth=depth)
323 else:
--> 324 model = resnet_v1(input_shape=input_shape, depth=depth)
325
326 model.compile(loss='categorical_crossentropy',

in resnet_v1(input_shape, depth, num_classes)
219 outputs = Dense(num_classes,
220 activation='softmax',
--> 221 kernel_initializer='he_normal')(y)
222
223 # Instantiate model.

/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in __call__(self, inputs, *kwargs)
617
618 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 619 output = self.call(inputs, *
kwargs)
620 output_mask = self.compute_mask(inputs, previous_mask)
621

/usr/local/lib/python2.7/dist-packages/keras/layers/core.pyc in call(self, inputs)
857 output = K.bias_add(output, self.bias)
858 if self.activation is not None:
--> 859 output = self.activation(output)
860 return output
861

/usr/local/lib/python2.7/dist-packages/keras/activations.pyc in softmax(x, axis)
27 ndim = K.ndim(x)
28 if ndim == 2:
---> 29 return K.softmax(x)
30 elif ndim > 2:
31 e = K.exp(x - K.max(x, axis=axis, keepdims=True))

/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in softmax(x, axis)
2957 A tensor.
2958 """
-> 2959 return tf.nn.softmax(x, axis=axis)
2960
2961

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

@Weixing-Zhang Which Tensorflow version are you using?

@srjoglekar246 thanks for your response. I'm using tensorflow-gpu 1.4.0. My keras couldn't load a model when I had the latest version installed. That's why I downgraded it from 1.5.0 to 1.4.0.

Aah. Seems like softmax in TF 1.4 did not support axis as an argument :-/

Seems like I better off upgrading everything. I'll keep my result posted.

srjoglekar246 was right. I upgraded tf-gpu to 1.6, cuda to 9.0, and cudnn to 7.0 and everything worked as normal.

Hi,
I get the same problem but I got a better solution.
Just downgrade the tensorflow and keras.
My previous tensorflow version is 1.4.1 and keras version 2.1.5.
I downgrade to tensorflow version 1.4.0 and keras version 2.0.8.
The error doesn't appear anymore.

@fitrialif - Just curious - why do you think downgrade is a better alternative than upgrade?

It works after upgrading TF: 1.7.0 and Keras: '2.1.5'

I ran into the same issue! But why isn't this handled in the pip package of Keras? Can't it point to specific TF versions as dependencies?

@ambodi
You have to make sure your Keras is up-to-date.

This is what I did: I upgraded tensorflow-gpu to 1.6, cuda to 9.0 (Not 9.0.5 or others), and cudnn to 7.0.

Finally, for python 2.x $ sudo pip install --upgrade keras
for python 3.x $ sudo pip3 install --upgrade keras

hello all!
I am currently working with raspberry pi 3. I have already installed tensorflow version 1.1.0 and keras 2.1.5!But the error TypeError: softmax() got an unexpected keyword argument 'axis'`occurs. I am not able to move further. What should I do? If i have to upgrade my tensorflow version, how do i do it? I need help please

@shouvik10 , can you try updating the TF version using apt-get or something similar?

@srjoglekar246 hey I've tried that too. Its not working :(

pip3 install --upgrade keras==2.1.3
Made it for me. Tensorflow version 1.4

Getting this same error on gcloud using Keras 2.1.6. Switched to keras==2.1.3 and it seems to work just fine.

For those of you working on Mac OS, if trying different versions of keras or tensorflow doesn't work, find the line that gives you the error and delete ', axis=axis' inside softmax().
It worked for me.

Hi, I have Mac OS. What I did was uninstall keras version 2.1.6 and installed version 2.1.3 as @JaviFuentes94 suggested and it started working.

just upgrade tensorflow to the latest version.
$ sudo pip3 install --upgrade tensorflow
works without any issues with tensorflow 1.8.0 and keras 2.1.6

My previous tensorflow version is 1.4.2 and keras version is the newest version (with cuda8.0, cudnn 6.0, and python2.7).
I downgrade to tensorflow version 1.4.0 and keras version 2.0.8. It works.

Easiest way to get rid of the error is to directly use softmax function from tensorflow

from keras import backend as K
model.add(Lambda(lambda x: K.tf.nn.softmax(x)))

It works with keras=2.0.8, tensorflow=1.3.0 without upgrading/degrading

@starfleet1702 you are right.

This must be a matrix compat. issue, keras should check the version of tensorflow to use the correct syntax.

Same problem with RaspberryPi 3. I trained the model on my PC using Tensorflow 1.6.0 and Keras 2.1.5 and saved the model using model_from_json.
When I load the model on RaspberryPi3, it shows softmax got unexpected keyword axis.
RaspberryPi3 currently run Tensorflow 1.1.0 and Keras 2.1.6.

Any help is appreciated.

Update: seems like if you downgrade Keras to a version which was used to create the model like in my case I downgraded Keras from 2.1.6 to 2.1.5, it worked! Model was loaded successfully on Raspberry Pi3 created on my PC.
Currently I have:
PC -->TF 1.6.0 and Keras 2.1.5
Pi3 -->TF 1.1.0 and Keras 2.1.5

If this help you please add a comment. I am looking forward for a better solution if anybody has it.

Hello, gents,
I have the similar problem:
Cuda: 8
tf: 1.4 (cause of Cuda, I cant upgrade it)
keras: 2.2
Keras-Applications==1.0.2
Keras-Preprocessing==1.0.1
If I downgrade keras I get errors about keras-application and keras-preprocessing needs more freshly keras:

keras-applications 1.0.2 has requirement keras>=2.1.6, but you'll have keras 2.1.5 which is incompatible.
keras-preprocessing 1.0.1 has requirement keras>=2.1.6, but you'll have keras 2.1.5 which is incompatible.

Is there any table version compatibility between keras+tensorflow?
The example of what I looking for might be such information: https://www.tensorflow.org/install/install_sources#tested_source_configurations

sudo pip install --force-reinstall keras==2.1.3

you can add softmax layer in this fashion and will work:
first:

import tensorflow as tf

then, within you model definition:

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

Confirming that @saeeron's solution works on a Raspberry Pi without having to downgrade anything. Bit annoying, though, that the TF version for it is so far behind (at least as far as pip is concerned).

Edited to add:
Looks like maybe there is a TF 1.9 if you have an up-to-date Raspbian with pip pointing at: https://www.piwheels.org

Python (and its libraries) are a pain in the butt. I've tried 'upgrading' everything and it broke all kinds of other things that were working. It would be nice if the pythonistas added functionality WITHOUT breaking earlier versions (why I love 'go'). I spend way too much time trying to get the 'right' version for all these fantastic Python libraries to play nice together: very frustrating.

@davmaz I feel your pain, for sure. Sometimes I have to set a project aside for a few days until it feels less burdensome. The main reason we run into this so much is that we're working with rapidly developing libraries, right. we could do menial data manipulation all day with the seldom-changing Numpy, but this stuff is way more exciting... and yeah... the update-frustration is the price. you're not alone! but at least we get to be frustrated while working with such neat stuff!

When I downgrade my tensorflow version to 1.8.0, the problem was solved.

I just edited tensorflow_backend.pyc return tf.nn.softmax(x, axis=axis) to return tf.nn.softmax(x, axis)

@unix-root It works for me. Thanks!

The problem is still there. I'm using Keras 2.2.2 and TF 1.2.1. I'll revert. Please fix this :)

Please downgrade your keras to 2.2.0. And upgrade tensorflow version to
1.8.0.

2018λ…„ 9μ›” 4일 (ν™”) μ˜€ν›„ 11:12, Maj Smerkol notifications@github.comλ‹˜μ΄ μž‘μ„±:

The problem is still there. I'm using Keras 2.2.2 and TF 1.2.1. I'll
revert. Please fix this :)

β€”
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/keras-team/keras/issues/9621#issuecomment-418381671,
or mute the thread
https://github.com/notifications/unsubscribe-auth/APXRVItQ075zHLyOnsOaOTeAQvkfh3hWks5uXopGgaJpZM4Sl-Su
.

@hakex Why should people downgrade their Keras version? This would not be an elegant approach.

I ran my code ran without any error back to November 2017. This error happened after updated to 2.1.5. Please help. Thank you.

`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
89 model.add(Dropout(0.5))
90 model.add(Dense(num_classes))
---> 91 model.add(Activation('softmax'))
92
93 # -----------------------------------------------------------

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in add(self, layer)
490 output_shapes=[self.outputs[0]._keras_shape])
491 else:
--> 492 output_tensor = layer(self.outputs[0])
493 if isinstance(output_tensor, list):
494 raise TypeError('All layers in a Sequential model '

/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in call(self, inputs, *kwargs)
617
618 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 619 output = self.call(inputs, *
kwargs)
620 output_mask = self.compute_mask(inputs, previous_mask)
621

/usr/local/lib/python2.7/dist-packages/keras/layers/core.pyc in call(self, inputs)
301
302 def call(self, inputs):
--> 303 return self.activation(inputs)
304
305 def get_config(self):

/usr/local/lib/python2.7/dist-packages/keras/activations.pyc in softmax(x, axis)
27 ndim = K.ndim(x)
28 if ndim == 2:
---> 29 return K.softmax(x)
30 elif ndim > 2:
31 e = K.exp(x - K.max(x, axis=axis, keepdims=True))

/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in softmax(x, axis)
2957 A tensor.
2958 """
-> 2959 return tf.nn.softmax(x, axis=axis)
2960
2961

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

It is because the current tensorflow version you used is not matching to your code. Just modify the tf.nn.softmax(x, axis=axis) to tf.nn.softmax(x, dim=axis).

The error is again occuring with tensorflow 1.12.0 and keras 2.2.4, most likely because axis got renamed to dim in TF.

@Zahlii confirmer! I also have the same error with keras 2.2.4 and tensorflow 1.2.1

$ python -c "import keras; print( keras.__version__ );"
Using TensorFlow backend.
2.2.4
$ python -c "import tensorflow as tf; print( tf.__version__ );"
1.2.1

cc @ghostplant

please upgrade tensorflow to 1.8.0 and check it out again.

If this doesn't work. please downgrade keras to 2.2.0.

2018λ…„ 11μ›” 27일 (ν™”) μ˜€μ „ 12:25, Loreto Parisi notifications@github.comλ‹˜μ΄ μž‘μ„±:

@Zahlii https://github.com/Zahlii confirmer! I also have the same error
with keras 2.2.4 and tensorflow 1.2.1

$ python -c "import keras; print( keras.__version__ );"
Using TensorFlow backend.
2.2.4
$ python -c "import tensorflow as tf; print( tf.__version__ );"
1.2.1

cc @ghostplant https://github.com/ghostplant

β€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/keras-team/keras/issues/9621#issuecomment-441677072,
or mute the thread
https://github.com/notifications/unsubscribe-auth/APXRVKU9rjUR-Rsw6ASHKPXvLMQl2gkJks5uzAfRgaJpZM4Sl-Su
.

--

μœ€ν•™λ¦Ό λ“œλ¦Ό

μ„Έμ’…λŒ€ν•™κ΅ 컴퓨터곡학과
05006 μ„œμšΈνŠΉλ³„μ‹œ 광진ꡬ λŠ₯λ™λ‘œ 209 μ„Έμ’…λŒ€ν•™κ΅
μ „ ν™” 02)465-5266
Mobile 010-2396-3278
E-mail [email protected]

@hakex it does not work. I had to downgrade.

This is my configuration that it works for sure:

tensorflow==1.8.0
keras==2.2.0
PyHyphen==0.10
google==2.0.1
protobuf==3.4.0

liliarizona's suggestion worked for me (delete: axis=axis)

The error is again occuring with tensorflow 1.12.0 and keras 2.2.4, most likely because axis got renamed to dim in TF.

It happens to me too

After trying some of the above suggestions, I ended up trying this change:

From:
model.add(Dense(133, activation='softmax'))

To:
model.add(Dense(133, activation = (tf.nn.softmax)))

and that did the trick. No change in versions or downgrade.
I am on tensorflow==1.12.0 and Keras==2.2.4

After trying some of the above suggestions, I ended up trying this change:

From:
model.add(Dense(133, activation='softmax'))

To:
model.add(Dense(133, activation = (tf.nn.softmax)))

and that did the trick. No change in versions or downgrade.
I am on tensorflow==1.12.0 and Keras==2.2.4

Lambda

from keras import backend as K
model.add(Lambda(lambda x: K.tf.nn.softmax(x)))

Both seems good way without changing versions. Thanks!!

Hi,
I get the same problem but I got a better solution.
Just downgrade the tensorflow and keras.
My previous tensorflow version is 1.4.1 and keras version 2.1.5.
I downgrade to tensorflow version 1.4.0 and keras version 2.0.8.
The error doesn't appear anymore.

This really works.

pip3 install --upgrade keras==2.1.3
Made it for me. Tensorflow version 1.4
Works for me

The solution that worked for me was to use activation = (tf.nn.softmax) as opposed to just activation = 'softmax'

upgrade tensorflow and keras to below version solved my issue

pip install keras==2.1.6
pip install tensorflow==1.7.0

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).

@fitrialif - Just curious - why do you think downgrade is a better alternative than upgrade?

because upgrading affects other dependents

Happened to me while loading a saved model, downgraded tensorflow to version 1.14.0 and keras to 2.2.4 and it works now (I may have installed another library requiring tf 2.0 so that caused the problem to me)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Imorton-zd picture Imorton-zd  Β·  3Comments

farizrahman4u picture farizrahman4u  Β·  3Comments

fredtcaroli picture fredtcaroli  Β·  3Comments

rantsandruse picture rantsandruse  Β·  3Comments

zygmuntz picture zygmuntz  Β·  3Comments