Keras level build and call: weights are constructed only once
The Keras subclass has usually placed the configuration unrelated to input at the initialization stage, creating weights that depend on input shapes at build and placing values forward at call.
Learning objectives and first steps2 objectives 2 first fixes
Try to do it after reading
- Distinguishing enduring weights from each forward result
- Create weights according to input feature dimensions
Suggested first
- Python Basic Syntax:
- Counting and training closed
Layers are not a function to retrain every call
The Keras subclass has usually placed the configuration unrelated to input at the initialization stage, creating weights that depend on input shapes at build and placing values forward at call. The same layer is repeated and the same group of weights is repeated. If new weights are created in call, the state of training changes and tracking and preservation may fail.
Sets a custom linear layer to receive [batch,] 2-D output. The weight shall be [3,2], biased [2], with a total of eight trainingable metrics. Enter [4,3] and [7,3] to reuse the eight parameters and to output [4,2] and [7,2], respectively. Batch is not a weight dimension, and the first axis cannot be placed into the parameter shape in build.
Use add_weight to get weights into the Keras tracking and preservation system. Placing a single external load in a Python attribute is not the same as creating a trainable weight; Keras3 should also not rely on the hidden tracking behaviour of the old version of certain variable properties. Confirms the layers of the lists of trainable_weights and non_trainable_weights, which are reliable compared to the properties of the object only.
Shape check should occur as soon as possible
If the layer is constructed for the first time with a 3D feature, the second receives a 4D feature, the old weight cannot complete the same matrix map. The model cannot determine the meaning of the additional feature by automatically extending the weight. Incompatible input needs to be rejected, or a new model version clearly created and old weight migration processed.
For example, weight matrix columns [1,1] and [0,1,1] enter [2,3,4] output [6,7]. This handwriting can serve as a model for forward units, checking column direction and bias in broadcasting. If the weight is written [2,3] and the transfer is forgotten, it may not be wrongly stated in some equivalent dimensions and semantics are different.
call can also receive parameters such as training or mask to pass mode and valid position to the internal layer. Customized layers that leave mask silently appear to run externally, but filled to the pool. Layer interface should contain these syntaxes, not just input output shape.
This section is directed at Keras3 with the example of the TensorFlow backend; No private Keras2 interface. The number of parameters and the number [6,7] were calculated and not implemented in the framework. Rebuilding output is consistent before saving and the list of weights is not empty as a complete proof of correctness.
Do you need to add parameters for linear layers ranging from four to seven, three to two?
No need. The parameters are still 3 x 2+2 = 8, and the batch decides only how many records are to be calculated simultaneously.
Why are you here? call I'm not going anywhere. add_weight Problem?
Every advance may attempt to create a new state of affairs, repeat the violation rights and trace the restrictions. Status creation should be consistent with the life cycle of the layer.
Sources and reconciliations
Definitions, methods and interfaces are available.
Figures and calculations are reorganized according to this chapter. Mathematical measurements, framework operations and real business effects are described separately in the body of the text.