get_weight_paths
- FullyConnected.get_weight_paths()
Retrieve all the variables and their paths for the model.
The variable path (string) is a stable key to identify a tf.Variable instance owned by the model. It can be used to specify variable-specific configurations (e.g. DTensor, quantization) from a global view.
This method returns a dict with weight object paths as keys and the corresponding tf.Variable instances as values.
Note that if the model is a subclassed model and the weights haven’t been initialized, an empty dict will be returned.
- Returns:
- A dict where keys are variable paths and values are tf.Variable
instances.
Example:
```python class SubclassModel(tf.keras.Model):
- def __init__(self, name=None):
super().__init__(name=name) self.d1 = tf.keras.layers.Dense(10) self.d2 = tf.keras.layers.Dense(20)
- def call(self, inputs):
x = self.d1(inputs) return self.d2(x)
model = SubclassModel() model(tf.zeros((10, 10))) weight_paths = model.get_weight_paths() # weight_paths: # { # ‘d1.kernel’: model.d1.kernel, # ‘d1.bias’: model.d1.bias, # ‘d2.kernel’: model.d2.kernel, # ‘d2.bias’: model.d2.bias, # }
# Functional model inputs = tf.keras.Input((10,), batch_size=10) x = tf.keras.layers.Dense(20, name=’d1’)(inputs) output = tf.keras.layers.Dense(30, name=’d2’)(x) model = tf.keras.Model(inputs, output) d1 = model.layers[1] d2 = model.layers[2] weight_paths = model.get_weight_paths() # weight_paths: # { # ‘d1.kernel’: d1.kernel, # ‘d1.bias’: d1.bias, # ‘d2.kernel’: d2.kernel, # ‘d2.bias’: d2.bias, # } ```