Debian Codegraph @

ml_collections/config_dict/examples/config_dict_placeholder.py

raw symbols SCIP
1 # Copyright 2024 The ML Collections Authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Example of placeholder fields in a ConfigDict.
16
17 This example shows how ConfigDict placeholder fields work. For a more complete
18 example of ConfigDict features, see example_advanced.
19 """
20
21 from absl import app
22 from ml_collections import config_dict
23
24
25 def main(_):
26 placeholder = config_dict.FieldReference(0)
27 cfg = config_dict.ConfigDict()
28 cfg.placeholder = placeholder
29 cfg.optional = config_dict.FieldReference(0, field_type=int)
30 cfg.nested = config_dict.ConfigDict()
31 cfg.nested.placeholder = placeholder
32
33 try:
34 cfg.optional = 'tom' # Raises Type error as this field is an integer.
35 except TypeError as e:
36 print(e)
37
38 cfg.optional = 1555 # Works fine.
39 cfg.placeholder = 1 # Changes the value of both placeholder and
40 # nested.placeholder fields.
41
42 # Note that the indirection provided by FieldReferences will be lost if
43 # accessed through a ConfigDict:
44 placeholder = config_dict.FieldReference(0)
45 cfg.field1 = placeholder
46 cfg.field2 = placeholder # This field will be tied to cfg.field1.
47 cfg.field3 = cfg.field1 # This will just be an int field initialized to 0.
48
49 print(cfg)
50
51
52 if __name__ == '__main__':
53 app.run()