Issue
keras documentation provides an example of extending the keras model without the init
method. From my understanding, this is nice because you don’t have to implement the call
function. Now to instantiate the model you can do something like this
model = CustomModel(inputs, outputs)
Not sure where inputs
, outputs
are going – it would be nice to know – but my question is how to do I pass additional arguments when instantiating the model i.e.:
model = CustomModel(inputs, outputs, other_args)
Edit
other_args
can be anything passed to CustomModel
(not keras.model
) i.e.: alpha=1.0
The research effort is that I looked through keras documentation and it shows two ways to extend the model. The advertised method is to implement __init__
Solution
The CustomModel example allows one to override specific methods of the Model class (train_step
) in the example.
When you call
model = CustomModel(inputs, outputs)
inputs is a tensor or list of input tensors to the custom model and outputs the output tensor(s).
You can see that in the example:
inputs = keras.Input(shape=(32,))
outputs = keras.layers.Dense(1)(inputs)
This defines a graph with an input layer with shape (32,) and a single output neuron (and no hidden layers).
Please clarify the usage of other_args
. If these are tensors used by the model train / fit they should be part of the input / output arguments (which can be a list of tensors). If other_args
are parameters that influence the Model custom train_step
function then you need to define a new init method that expects it and call super().__init__(inputs, outputs)
.
The keras.Model
class is concerned with the machinery of training and predicting models. The ML model itself is defined as a graph of keras.Layer(s).
Answered By – Pedro Marques
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0