代码之家  ›  专栏  ›  技术社区  ›  Mnemosyne

使用KERS功能模型时出现类型错误

  •  0
  • Mnemosyne  · 技术社区  · 6 年前

    我使用keras函数API(keras版本2.2)定义了一个模型,但是当我尝试将数据与模型相匹配时,我会得到一些错误信息。我目前使用的是python 2.7,代码在Ubuntu18.04上运行。

    以下是模型的代码:

    class Model:
    
        def __init__(self, config):
            self.hidden_layers = config["hidden_layers"]
            self.loss = config["loss"]
            self.optimizer = config["optimizer"]
            self.batch_normalization = config["batch_normalization"]
            self.model = self._build_model()
    
        def _build_model(self):
            input = Input(shape=(32,))
    
            hidden_layers = []
    
            if self.batch_normalization:
                hidden_layers.append(Dense(self.hidden_layers[0], bias_initializer= Orthogonal)(input))
                hidden_layers.append(BatchNormalization()(hidden_layers[-1]))
                hidden_layers.append(Activation("relu")(hidden_layers[-1]))
            else:
                hidden_layers.append(Dense(self.hidden_layers[0], bias_initializer= Orthogonal, activation='relu')(input))
    
            for i in self.hidden_layers[1:]:
                if self.batch_normalization:
                    hidden_layers.append(Dense(i, bias_initializer= Orthogonal)(hidden_layers[-1]))
                    hidden_layers.append(BatchNormalization()(hidden_layers[-1]))
                    hidden_layers.append(Activation("relu")(hidden_layers[-1]))
                else:
                    hidden_layers.append(Dense(i, bias_initializer= Orthogonal, activation='relu')(hidden_layers[-1]))
    
            output_layer = Dense(2, activation="softmax")(hidden_layers[-1])
            model = Model(input= input, output= output_layer)
            model.compile(optimizer=self.optimizer, loss=self.loss, metrics=["accuracy"])
            return model
    

    以下是我使用的命令和运行fit方法后得到的错误消息:

    model.fit(x=X_train,y=Y_train, epochs=20)
    
      File "/home/project/main.py", line 69, in <module>
        main(config)
      File "/home/project/main.py", line 62, in main
        model = Model(config, logging).model
      File "/home/project/model.py", line 18, in __init__
        self.model = self._build_model()
      File "/home/project/model.py", line 34, in _build_model
        hidden_layers.append(Dense(self.hidden_layers[0], bias_initializer= Orthogonal, activation='relu')(input))
      File "/home/project/venv/local/lib/python2.7/site-packages/keras/engine/base_layer.py", line 431, in __call__
        self.build(unpack_singleton(input_shapes))
      File "/home/project/venv/local/lib/python2.7/site-packages/keras/layers/core.py", line 872, in build
        constraint=self.bias_constraint)
      File "/home/project/venv/local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
        return func(*args, **kwargs)
      File "/home/project/venv/local/lib/python2.7/site-packages/keras/engine/base_layer.py", line 252, in add_weight
        constraint=constraint)
      File "/home/project/venv/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 402, in variable
        v = tf.Variable(value, dtype=tf.as_dtype(dtype), name=name)
      File "/home/project/venv/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 183, in __call__
        return cls._variable_v1_call(*args, **kwargs)
      ...
      ...
      File "/home/project/venv/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 1329, in __init__
        constraint=constraint)
      File "/home/project/venv/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 1437, in _init_from_args
        initial_value(), name="initial_value", dtype=dtype)
    TypeError: __call__() takes at least 2 arguments (1 given)
    

    我真的不明白这是什么类型的错误。我不确定如何更改模型定义以避免此错误。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Daniel Möller    6 年前

    似乎偏差初始值设定项出错。你通过了一个班 Orthogonal 当您应该传递该类的一个实例时,例如 bias_initializer=Orthogonal() .

    现在,我强烈建议您不要在课堂上使用与Keras相同的名称。不要创建 class Model ,创建任何其他内容,如 class AnyNameOtherThanModel .