Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Weixing-Zhang opened this issue Mar 12, 2018 · 52 comments

Comments

@Weixing-Zhang
Copy link

@Weixing-Zhang Weixing-Zhang commented Mar 12, 2018

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'`

@Weixing-Zhang

This comment has been minimized.

Copy link
Author

@Weixing-Zhang Weixing-Zhang commented Mar 12, 2018

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'`

@srjoglekar246

This comment has been minimized.

Copy link
Contributor

@srjoglekar246 srjoglekar246 commented Mar 13, 2018

@Weixing-Zhang Which Tensorflow version are you using?

@Weixing-Zhang

This comment has been minimized.

Copy link
Author

@Weixing-Zhang Weixing-Zhang commented Mar 13, 2018

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

@srjoglekar246

This comment has been minimized.

Copy link
Contributor

@srjoglekar246 srjoglekar246 commented Mar 13, 2018

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

@Weixing-Zhang

This comment has been minimized.

Copy link
Author

@Weixing-Zhang Weixing-Zhang commented Mar 13, 2018

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

@Weixing-Zhang

This comment has been minimized.

Copy link
Author

@Weixing-Zhang Weixing-Zhang commented Mar 13, 2018

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

@fitrialif

This comment has been minimized.

Copy link

@fitrialif fitrialif commented Mar 14, 2018

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.

@srjoglekar246

This comment has been minimized.

Copy link
Contributor

@srjoglekar246 srjoglekar246 commented Mar 14, 2018

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

@ATSEGYPT

This comment has been minimized.

Copy link

@ATSEGYPT ATSEGYPT commented Apr 2, 2018

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

@ambodi

This comment has been minimized.

Copy link

@ambodi ambodi commented Apr 10, 2018

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?

@Weixing-Zhang

This comment has been minimized.

Copy link
Author

@Weixing-Zhang Weixing-Zhang commented Apr 10, 2018

@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

@shouvik10

This comment has been minimized.

Copy link

@shouvik10 shouvik10 commented Apr 19, 2018

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

@srjoglekar246

This comment has been minimized.

Copy link
Contributor

@srjoglekar246 srjoglekar246 commented Apr 19, 2018

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

@shouvik10

This comment has been minimized.

Copy link

@shouvik10 shouvik10 commented Apr 23, 2018

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

@JaviFuentes94

This comment has been minimized.

Copy link

@JaviFuentes94 JaviFuentes94 commented Apr 25, 2018

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

@JudSchneider

This comment has been minimized.

Copy link

@JudSchneider JudSchneider commented Apr 25, 2018

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

@liliarizona

This comment has been minimized.

Copy link

@liliarizona liliarizona commented May 1, 2018

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.

@abhishek-v

This comment has been minimized.

Copy link

@abhishek-v abhishek-v commented May 9, 2018

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.

@prabinrath

This comment has been minimized.

Copy link

@prabinrath prabinrath commented May 13, 2018

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

@Zellar209

This comment has been minimized.

Copy link

@Zellar209 Zellar209 commented May 24, 2018

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.

@starfleet1702

This comment has been minimized.

Copy link

@starfleet1702 starfleet1702 commented Jun 5, 2018

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

@ebuildy

This comment has been minimized.

Copy link

@ebuildy ebuildy commented Jun 5, 2018

@starfleet1702 you are right.

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

@ZainUlMustafa

This comment has been minimized.

Copy link

@ZainUlMustafa ZainUlMustafa commented Jun 7, 2018

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.

@ZainUlMustafa

This comment has been minimized.

Copy link

@ZainUlMustafa ZainUlMustafa commented Jun 7, 2018

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.

@iamtodor

This comment has been minimized.

Copy link

@iamtodor iamtodor commented Jun 30, 2018

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.
@iamtodor

This comment has been minimized.

Copy link

@iamtodor iamtodor commented Jun 30, 2018

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

@Bleyddyn

This comment has been minimized.

Copy link

@Bleyddyn Bleyddyn commented Jul 21, 2018

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

@davmaz

This comment has been minimized.

Copy link

@davmaz davmaz commented Jul 22, 2018

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.

@npeirson

This comment has been minimized.

Copy link

@npeirson npeirson commented Jul 22, 2018

@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!

@hakex

This comment has been minimized.

Copy link

@hakex hakex commented Aug 1, 2018

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

@unix-root

This comment has been minimized.

Copy link

@unix-root unix-root commented Aug 9, 2018

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

@zchrissirhcz

This comment has been minimized.

Copy link

@zchrissirhcz zchrissirhcz commented Sep 3, 2018

@unix-root It works for me. Thanks!

@majthehero

This comment has been minimized.

Copy link

@majthehero majthehero commented Sep 4, 2018

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

@hakex

This comment has been minimized.

Copy link

@hakex hakex commented Sep 4, 2018

@zchrissirhcz

This comment has been minimized.

Copy link

@zchrissirhcz zchrissirhcz commented Sep 5, 2018

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

@LiuWenzhang

This comment has been minimized.

Copy link

@LiuWenzhang LiuWenzhang commented Oct 18, 2018

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

@Zahlii

This comment has been minimized.

Copy link

@Zahlii Zahlii commented Nov 19, 2018

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.

@loretoparisi

This comment has been minimized.

Copy link

@loretoparisi loretoparisi commented Nov 26, 2018

@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

@hakex

This comment has been minimized.

Copy link

@hakex hakex commented Nov 27, 2018

@loretoparisi

This comment has been minimized.

Copy link

@loretoparisi loretoparisi commented Nov 27, 2018

@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
@hefv57

This comment has been minimized.

Copy link

@hefv57 hefv57 commented Dec 29, 2018

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

@GuillemGSubies

This comment has been minimized.

Copy link

@GuillemGSubies GuillemGSubies commented Jan 9, 2019

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

@jpinzonc

This comment has been minimized.

Copy link

@jpinzonc jpinzonc commented Feb 12, 2019

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

@Nempickaxe

This comment has been minimized.

Copy link

@Nempickaxe Nempickaxe commented Feb 13, 2019

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!!

@neelkanthk

This comment has been minimized.

Copy link

@neelkanthk neelkanthk commented Mar 12, 2019

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.

@psn1997

This comment has been minimized.

Copy link

@psn1997 psn1997 commented Mar 23, 2019

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

@poglesbyg

This comment has been minimized.

Copy link

@poglesbyg poglesbyg commented Mar 26, 2019

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

@ybdesire

This comment has been minimized.

Copy link

@ybdesire ybdesire commented Jun 12, 2019

upgrade tensorflow and keras to below version solved my issue

pip install keras==2.1.6
pip install tensorflow==1.7.0
@FlashSnail

This comment has been minimized.

Copy link

@FlashSnail FlashSnail commented Jun 19, 2019

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

@ycdaskin

This comment has been minimized.

Copy link

@ycdaskin ycdaskin commented Sep 27, 2019

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

because upgrading affects other dependents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.