I'm using the subclassing API in Tensorflow to build a simple MLP model.
from tensorflow.keras import Model
from tensorflow.keras.optimizers import AdamW
from tensorflow.keras.layers import Dense
class MyModel(Model):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.layer_hidden1 = Dense(units=10, activation='relu')
self.layer_hidden2 = Dense(units=10, activation='relu')
self.layer_output = Dense(units=1, activation='linear')
def call(self, inputs):
hidden1_out = self.layer_hidden1(inputs)
hidden2_out = self.layer_hidden2(hidden1_out)
output = self.layer_output(hidden2_out )
return output
if __name__ == '__main__':
optimizer = AdamW(learning_rate=0.001, beta_1=0.9, beta_2=0.999, weight_decay=0.004)
model = MyModel()
model.compile(loss='mse', optimizer=optimizer)
model.build((7,)) # Throws warning
model.fit(train, label, epochs=100, verbose=True, batch_size=32) # Works fine
Calling model.build(input_shape) throws the following error:
C:\Users\ab1234\DevopsSetupPrograms\Anaconda\envs\py39\lib\site-packages\keras\src\layers\layer.py:372: UserWarning: `build()` was called on layer 'MyModel_1', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
Calling the model.fit() method works fine and results in the weights being initialised as expected and the model training correctly.
I would like to call the build method prior to calling fit and can't work out why this is occurring. Calling the summary method returns no weights because the model has not been built yet. I've looked at a number of other posts regarding weights initialisation, but these posts seem to imply that subclassing the Model class does not need an explicit build method in the subclass as it should already be part of the superclass, i.e. calling model.build() should work right off the bat.
When and How the Call function work in Model Subclassing of Keras?
This model has not yet been built error on model.summary()
Initial weights in Network with Keras / Tensorflow
I've also tried adding a Normalization layer to the start and then calling the adapt method in the script. This works to build the model and I get my weights as expected, but I would still like to call build without having this layer.
The warning message seems to think my custom model is a layer? Is this just a typo? Is it also pertinent that it is looking for the build method in \keras\src\layers\layer.py?
ValueError: Input 0 of layer "dense" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (7,). Fixing the input shape gives no warnings or errors.pip install tensorflow. Can you elaborate on how you fixed the input shape?model.build(input_shape), which doesn't have build method implemented on your custom model. when using subclassing Api to create a custom model, it is recommended to create layer weights in thebuild(self, inputs_shape)method inside custom model and no need to call themodel.build(input_shape)explicitly. Please, refer to this document for more details.