【问题标题】:Keras - Default Axis for softmax function is set to AxisKeras - softmax 函数的默认轴设置为 Axis
【发布时间】:2018-05-13 23:19:00
【问题描述】:

我正在学习如何创建顺序模型。我有一个模型:

*model  =  Sequential()*

然后我继续添加池化层和卷积层(这很好)。但是在创建密集层时:

*model.add(Dense(num_classes, activation = 'softmax'))*

返回的行:

*tf.nn.softmax(x, axis=axis)* 

由于未定义轴而导致错误。 Keras 和 TensorFlow 文档都显示 softmax 的默认轴是 None 或 -1。

这是 keras 的错误吗?是否有一个简单的解决方法(如果我要设置轴,我不确定输入张量是什么)?

-如果需要,我可以添加其余代码,但它只是由其他层组成,我认为它不会有太大帮助。

【问题讨论】:

  • 通常这可以正常工作,axis=-1 确实如此。但我不确定你是如何到达tf.nn.softmax 的。你是怎么知道的?
  • 我在尝试查找错误根源时在回溯中看到了它:第 2963 行,在 softmax return tf.nn.softmax(x, axis=axis) TypeError: softmax() got an unexpected关键字参数“轴”
  • 所以,实际上,softmax“不”期望轴......这可能是旧版本的 tensorflow,不是吗?
  • 是的!使用的是 tensorflow 1.4.0

标签: python tensorflow keras softmax activation-function


【解决方案1】:

我认为您的 Keras 和/或 TensorFlow 不是最新的,您应该更新它/它们。

这是 2017 年夏季 Keras 中的一个已知问题,已在 this commit 中修复。在错误报告中查看this comment 的更多信息。

另外,axis 在 TensorFlow 的 softmax() 中是 introduced as an argument on November 22, 2017,所以如果 TensorFlow 版本为 1.4.0 或更低,也会导致此错误。

如果您在linked commit 查看 Keras 的来源,则究竟是哪一个导致错误取决于处理的张量的等级。

此代码适用于当前版本(在 https://colab.research.google.com 上测试):

import keras
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD

print( keras.__version__ )

model = Sequential()
model.add( Dense(6, input_shape=(6,), activation = 'softmax' ) )

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

输出

2.1.6

但更重要的是,编译模型没有错误。

【讨论】:

  • 我认为,通过对问题的评论,这可能是 tensorflow 版本中的问题。说“softmax”得到了一个意想不到的参数“axis”。
  • 不是,实际上是指softmax()Activations包中的Keras实现。如果你关注link to the commit,你会看到Keras基于K.exp()K.sum()等实现了自己的softmax。
  • @DanielMöller 也许你是对的,根据错误消息,重新阅读它。很高兴看到整个事情......
  • @DanielMöller axis 在 TensorFlow 的 softmax() 中是 introduced as an argument on November 22, 2017,所以是的,如果 TensorFlow 版本为 1.4.0 或更低,则会导致此错误。
猜你喜欢
  • 1970-01-01
  • 2021-03-06
  • 1970-01-01
  • 2010-10-28
  • 2011-11-06
  • 2020-06-19
  • 1970-01-01
相关资源
最近更新 更多