【问题标题】:Why .add layers can`t be done when creating Keras model (NoneType error)为什么创建 Keras 模型时无法添加图层(NoneType 错误)
【发布时间】:2023-04-04 14:00:01
【问题描述】:

当我创建模型并直接添加一些层来创建生成器时,这会生成NoneType 对象。

代码:

network = Sequential().add(Flatten())
network.add(Dropout(0.5))

这会导致错误:

AttributeError                          
Traceback (most recent call last)
<ipython-input-41-4636db57f4d3> in <module>()
      1
      2 network = Sequential().add(Flatten())
----> 3 network.add(Dropout(0.5))

AttributeError: 'NoneType' object has no attribute 'add'

我的问题是:为什么我们不能使用.add 创建 keras 模型?

【问题讨论】:

    标签: python keras


    【解决方案1】:

    Sequential 类中的函数 add 不返回模型。它是一个所谓的 void 函数(非值返回函数),在 Python 中返回 None。您需要单独执行此步骤:

    network = Sequential()
    network.add(Flatten())
    network.add(Dropout(0.5))
    

    【讨论】:

    • 首先,.add 是一个方法,而不是函数。这是真的,method 什么都不返回,但是在执行network = Sequential(); network.add(Flatten()).add(Dropout(0.5)) 时,这也没有返回网络对象,即使网络的值没有被重写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多