我正在学习如何创建顺序模型。我有一个模型:
*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/