python - Keras - softmax 函数的默认轴设置为 Axis

标签 python tensorflow keras softmax activation-function

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

*model  =  Sequential()*

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

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

返回的行:

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

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

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

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

最佳答案

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

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

另外introduced as an argument on November 22, 2017在 TensorFlow 的 softmax() 中,因此如果 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

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

关于python - Keras - softmax 函数的默认轴设置为 Axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321437/

相关文章:

python - Keras LSTM 神经网络 : TypeError: LSTM() missing 1 required positional argument: 'Y'

Python:未安装 _imagingft C 模块

python - Angular js上传文件功能无法按预期工作

python - 错误5 : Acces is denied when moving files in python

memory-management - 为什么这个 tensorflow 循环需要这么多内存?

python - 获得模型预测的准确性

python - 检查列表中的两个项目是否相同?

python-3.x - 属性错误: module 'tensorflow_core.keras.layers' has no attribute 'Conv1d'

tensorflow - Google Colab - 训练时 session 崩溃错误

python - 神经网络模型