keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.11k stars 19.36k forks source link

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

Closed Weixing-Zhang closed 6 years ago

Weixing-Zhang commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@Weixing-Zhang Which Tensorflow version are you using?

Weixing-Zhang commented 6 years ago

@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 commented 6 years ago

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

Weixing-Zhang commented 6 years ago

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

Weixing-Zhang commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

ATSEGYPT commented 6 years ago

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

amir-rahnama commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

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

shouvik10 commented 6 years ago

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

JaviFuentes94 commented 6 years ago

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

JudSchneider commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

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 commented 5 years ago

@starfleet1702 you are right.

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

ZainUlMustafa commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

aakashkag commented 5 years ago

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

saeeron commented 5 years ago

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

Bleyddyn commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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

tapasrm commented 5 years ago

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

zchrissirhcz commented 5 years ago

@unix-root It works for me. Thanks!

majthehero commented 5 years ago

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

hakex commented 5 years ago

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 .

zchrissirhcz commented 5 years ago

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

wenzhangliu commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 yinhelin0608@gmail.com

loretoparisi commented 5 years ago

@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 commented 5 years ago

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

GuillemGSubies commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

poglesbyg commented 5 years ago

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

ybdesire commented 4 years ago

upgrade tensorflow and keras to below version solved my issue

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