【问题标题】:TypeError: compile() missing 1 required positional argument: 'self'类型错误:compile() 缺少 1 个必需的位置参数:'self'
【发布时间】:2017-08-21 15:08:09
【问题描述】:

model.compile 中的“self”是什么?我尝试在 keras 模型中使用 python 运行代码,但出现此错误:

model.compile(loss="binary_crossentropy", optimizer='adam', metrics=['accuracy'])
TypeError: compile() missing 1 required positional argument: 'self'

【问题讨论】:

    标签: python-3.x keras conv-neural-network keras-layer


    【解决方案1】:

    编译模型的参数如下:

    如果你定义了Sequential模型,那么你可以直接调用compile

    model.compile(optimizer=..., loss=..., metrics=...)
    

    否则,需要使用Keras Functional API构建模型,并使用上述代码编译模型。

    # input is the input layer and output is the output layer.
    model = Model(inputs=input, outputs=output) 
    

    【讨论】:

      【解决方案2】:

      其实你需要显示更多代码才能得到合适的答案,但我试一试:

      如果要编译模型,至少需要执行以下步骤:

      1. from keras.models import Model
      2. model = Model(inputs=in, outputs=out) - 其中in 是输入层,out 是输出层。
      3. model.compile(loss=someLoss, optimizer=someOpt)

      【讨论】:

        【解决方案3】:

        实例化类,然后使用方法... 应该是这样的

        model().compile()
        
        or 
        m = model()
        m.compile()
        

        【讨论】:

          猜你喜欢
          • 2022-01-03
          • 2021-07-14
          • 2015-07-13
          • 2019-10-02
          • 2017-06-24
          • 2016-05-27
          • 2021-09-21
          • 2020-11-10
          • 2018-04-05
          相关资源
          最近更新 更多