Debian Codegraph @

ml_collections/config_dict/examples/field_reference.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 FieldReference usage.
16
17 This shows how to use FieldReferences for lazy computation.
18 """
19
20 from absl import app
21 from ml_collections import config_dict
22
23
24 def lazy_computation():
25 """Simple example of lazy computation with `configdict.FieldReference`."""
26 ref = config_dict.FieldReference(1)
27 print(ref.get()) # Prints 1
28
29 add_ten = ref.get() + 10 # ref.get() is an integer and so is add_ten
30 add_ten_lazy = ref + 10 # add_ten_lazy is a FieldReference - NOT an integer
31
32 print(add_ten) # Prints 11
33 print(add_ten_lazy.get()) # Prints 11 because ref's value is 1
34
35 # Addition is lazily computed for FieldReferences so changing ref will change
36 # the value that is used to compute add_ten.
37 ref.set(5)
38 print(add_ten) # Prints 11
39 print(add_ten_lazy.get()) # Prints 15 because ref's value is 5
40
41
42 def change_lazy_computation():
43 """Overriding lazily computed values."""
44 config = config_dict.ConfigDict()
45 config.reference = 1
46 config.reference_0 = config.get_ref('reference') + 10
47 config.reference_1 = config.get_ref('reference') + 20
48 config.reference_1_0 = config.get_ref('reference_1') + 100
49
50 print(config.reference) # Prints 1.
51 print(config.reference_0) # Prints 11.
52 print(config.reference_1) # Prints 21.
53 print(config.reference_1_0) # Prints 121.
54
55 config.reference_1 = 30
56
57 print(config.reference) # Prints 1 (unchanged).
58 print(config.reference_0) # Prints 11 (unchanged).
59 print(config.reference_1) # Prints 30.
60 print(config.reference_1_0) # Prints 130.
61
62
63 def create_cycle():
64 """Creates a cycle within a ConfigDict."""
65 config = config_dict.ConfigDict()
66 config.integer_field = 1
67 config.bigger_integer_field = config.get_ref('integer_field') + 10
68
69 try:
70 # Raises a MutabilityError because setting config.integer_field would
71 # cause a cycle.
72 config.integer_field = config.get_ref('bigger_integer_field') + 2
73 except config_dict.MutabilityError as e:
74 print(e)
75
76
77 def lazy_configdict():
78 """Example usage of lazy computation with ConfigDict."""
79 config = config_dict.ConfigDict()
80 config.reference_field = config_dict.FieldReference(1)
81 config.integer_field = 2
82 config.float_field = 2.5
83
84 # No lazy evaluatuations because we didn't use get_ref()
85 config.no_lazy = config.integer_field * config.float_field
86
87 # This will lazily evaluate ONLY config.integer_field
88 config.lazy_integer = config.get_ref('integer_field') * config.float_field
89
90 # This will lazily evaluate ONLY config.float_field
91 config.lazy_float = config.integer_field * config.get_ref('float_field')
92
93 # This will lazily evaluate BOTH config.integer_field and config.float_Field
94 config.lazy_both = (config.get_ref('integer_field') *
95 config.get_ref('float_field'))
96
97 config.integer_field = 3
98 print(config.no_lazy) # Prints 5.0 - It uses integer_field's original value
99
100 print(config.lazy_integer) # Prints 7.5
101
102 config.float_field = 3.5
103 print(config.lazy_float) # Prints 7.0
104 print(config.lazy_both) # Prints 10.5
105
106
107 def lazy_configdict_advanced():
108 """Advanced lazy computation with ConfigDict."""
109 # FieldReferences can be used with ConfigDict as well
110 config = config_dict.ConfigDict()
111 config.float_field = 12.6
112 config.integer_field = 123
113 config.list_field = [0, 1, 2]
114
115 config.float_multiply_field = config.get_ref('float_field') * 3
116 print(config.float_multiply_field) # Prints 37.8
117
118 config.float_field = 10.0
119 print(config.float_multiply_field) # Prints 30.0
120
121 config.longer_list_field = config.get_ref('list_field') + [3, 4, 5]
122 print(config.longer_list_field) # Prints [0, 1, 2, 3, 4, 5]
123
124 config.list_field = [-1]
125 print(config.longer_list_field) # Prints [-1, 3, 4, 5]
126
127 # Both operands can be references
128 config.ref_subtraction = (
129 config.get_ref('float_field') - config.get_ref('integer_field'))
130 print(config.ref_subtraction) # Prints -113.0
131
132 config.integer_field = 10
133 print(config.ref_subtraction) # Prints 0.0
134
135
136 def main(argv=()):
137 del argv # Unused.
138 lazy_computation()
139 lazy_configdict()
140 change_lazy_computation()
141 create_cycle()
142 lazy_configdict_advanced()
143
144
145 if __name__ == '__main__':
146 app.run()