使用keras功能模型时出现TypeError

时间:2018-12-29 13:36:17

标签: python keras

我使用Keras功能API(keras版本2.2)定义了一个模型,但是当我尝试将数据拟合到模型中时,会出现一些错误。我目前正在使用python 2.7,并且代码在Ubuntu 18.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)

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

1 个答案:

答案 0 :(得分:1)

似乎该错误发生在偏压初始化器上。您应该在传递类Orthogonal时传递该类的实例,例如bias_initializer=Orthogonal()

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