Open nhuet opened 1 year ago
A simpler solution to your problem would be:
dense = Dense.from_config(...)
. (It doesn't have weights at that time)dense.kernel = old_layer.kernel
, dense.bias = old_layer.bias
, dense.built = True
Nice!
But are we sure that the build()
method does only create the weights? Perhaps i will miss something else by skipping build()
?
I would like a solution that works with any layer. By setting self.built = True
, I skip the build()
and thus do not overwrite the weights, but is there anything else that could be important not to bypass so that the call()
works ?
At least, it seems build()
sets also input_spec
attribute, but perhaps this will not be too much of a loss (and i can also copy it from previous layer)
A simpler solution to your problem would be:
- Instantiate the new Dense layer, e.g.
dense = Dense.from_config(...)
. (It doesn't have weights at that time)- Set
dense.kernel = old_layer.kernel
,dense.bias = old_layer.bias
,dense.built = True
- Just use the layer -- no new weights will be created since the layer is already built
It does not work anymore from keras 3.0.3 since Dense.kernel
is now a property not settable...
We'll add a setter for the kernel.
Thx!
The setter thing turned out to be problematic. What I would recommend is just direct setting but use ._kernel
instead of .kernel
.
It seems that sharing weights is not possible anymore afterwards in keras 3. We should instead share layers as explained here.
But I have a usecase where I need to share a weight
In my usecase, I transform a model by splitting activations out of each layer, that means a Dense(3, activation="relu") is transformed in a Dense(3) + Activation layer. But I need
For now I have a solution but that use private attribute since by design this is currently not possible in keras 3.
Here is an example that works for sharing kernel (I actually will use something more generic to share any weight, but this is simpler to look at):
Notes:
layer2.kernel = layer1.kernel
after build will raise an error because of the lock.