keras: TypeError: softmax() got an unexpected keyword argument 'axis'
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) <ipython-input-8-15af8cf9c234> in <module>() 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’`
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 9
- Comments: 53 (4 by maintainers)
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.
pip3 install --upgrade keras==2.1.3
Made it for me. Tensorflow version 1.4After 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
@fitrialif - Just curious - why do you think downgrade is a better alternative than upgrade?
I just edited tensorflow_backend.pyc
return tf.nn.softmax(x, axis=axis)
toreturn tf.nn.softmax(x, axis)
srjoglekar246 was right. I upgraded tf-gpu to 1.6, cuda to 9.0, and cudnn to 7.0 and everything worked as normal.
Getting this same error on
gcloud
usingKeras 2.1.6
. Switched tokeras==2.1.3
and it seems to work just fine.Easiest way to get rid of the error is to directly use softmax function from tensorflow
It works with keras=2.0.8, tensorflow=1.3.0 without upgrading/degrading
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))
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.
sudo pip install --force-reinstall keras==2.1.3
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
It works after upgrading TF: 1.7.0 and Keras: ‘2.1.5’
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 😃
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.
Aah. Seems like softmax in TF 1.4 did not support
axis
as an argument 😕Both seems good way without changing versions. Thanks!!
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?