Mnemosyne
Mnemosyne

Reputation: 1192

TypeError when using keras functional model

I used the Keras functional API (keras version 2.2) to define a model but when I try to fit the data to the model I get some for of error. I am currently using python 2.7 and the code is running on Ubuntu 18.04.

The following is the code for the model:

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

The following is the command that I use and the error message I get once I run the fit method:

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)

I really don't understand what this TypeError is. I am not sure how to change my model definition to avoid this error.

Upvotes: 0

Views: 44

Answers (1)

Daniel M&#246;ller
Daniel M&#246;ller

Reputation: 86600

It seems the error happens with the bias initializer. You're passing a class Orthogonal when you should be passing an instance of that class, like bias_initializer=Orthogonal().

Now, I strongly recommend you never use the same names as Keras for your classes. Don't create a class Model, create anything else, like class AnyNameOtherThanModel.

Upvotes: 1

Related Questions