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

问题描述

在使用keras搭建网络去运行时,使用了softmax:

model = Sequential()
……
model.add(Activation('softmax'))

结果在运行的时候就报错了:

Traceback (most recent call last):
  File "main.py", line 7, in 
    train.train()
  File "train.py", line 34, in quality_classify_model
    model.add(Activation('softmax'))
  File "/usr/local/app/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/engine/sequential.py", line 181, in add
    output_tensor = layer(self.outputs[0])
  File "/usr/local/app/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/engine/base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "/usr/local/app/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/layers/core.py", line 299, in call
    return self.activation(inputs)
  File "/usr/local/app/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/activations.py", line 31, in softmax
    return K.softmax(x)
  File "/usr/local/app/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 3231, in softmax
    return tf.nn.softmax(x, axis=axis)
TypeError: softmax() got an unexpected keyword argument 'axis'

解决方法

网上看到一种说法是由于tensorflow版本过低的问题,这里我的环境中tensorflow的版本是1.2.1,查看版本号的方法:终端命令查看TensorFlow版本号及路径。这种说法的解决方案当然就是升级tensorflow版本了。由于比较麻烦我没有选择这种方法。

找到了另一种方法,可以看到报错信息中最后落脚在:

  File "/usr/local/app/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 3231, in softmax
    return tf.nn.softmax(x, axis=axis)

所以我们进入这个路径(/usr/local/app/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/backend),打开tensorflow_backend.py,直接修改该文件的代码:


keras报错:TypeError: softmax() got an unexpected keyword argument 'axis'_第1张图片

去掉返回参数中的“axis”,改为:


keras报错:TypeError: softmax() got an unexpected keyword argument 'axis'_第2张图片

重新运行,就顺利且正常了。

暂时不明确这种做法是否会带来其他问题。


查看作者首页

你可能感兴趣的:(keras报错:TypeError: softmax() got an unexpected keyword argument 'axis')