| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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()) |
| 28 |
|
| 29 |
add_ten = ref.get() + 10 |
| 30 |
add_ten_lazy = ref + 10 |
| 31 |
|
| 32 |
print(add_ten) |
| 33 |
print(add_ten_lazy.get()) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
ref.set(5) |
| 38 |
print(add_ten) |
| 39 |
print(add_ten_lazy.get()) |
| 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) |
| 51 |
print(config.reference_0) |
| 52 |
print(config.reference_1) |
| 53 |
print(config.reference_1_0) |
| 54 |
|
| 55 |
config.reference_1 = 30 |
| 56 |
|
| 57 |
print(config.reference) |
| 58 |
print(config.reference_0) |
| 59 |
print(config.reference_1) |
| 60 |
print(config.reference_1_0) |
| 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 |
|
| 71 |
|
| 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 |
|
| 85 |
config.no_lazy = config.integer_field * config.float_field |
| 86 |
|
| 87 |
|
| 88 |
config.lazy_integer = config.get_ref('integer_field') * config.float_field |
| 89 |
|
| 90 |
|
| 91 |
config.lazy_float = config.integer_field * config.get_ref('float_field') |
| 92 |
|
| 93 |
|
| 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) |
| 99 |
|
| 100 |
print(config.lazy_integer) |
| 101 |
|
| 102 |
config.float_field = 3.5 |
| 103 |
print(config.lazy_float) |
| 104 |
print(config.lazy_both) |
| 105 |
|
| 106 |
|
| 107 |
def lazy_configdict_advanced(): |
| 108 |
"""Advanced lazy computation with ConfigDict.""" |
| 109 |
|
| 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) |
| 117 |
|
| 118 |
config.float_field = 10.0 |
| 119 |
print(config.float_multiply_field) |
| 120 |
|
| 121 |
config.longer_list_field = config.get_ref('list_field') + [3, 4, 5] |
| 122 |
print(config.longer_list_field) |
| 123 |
|
| 124 |
config.list_field = [-1] |
| 125 |
print(config.longer_list_field) |
| 126 |
|
| 127 |
|
| 128 |
config.ref_subtraction = ( |
| 129 |
config.get_ref('float_field') - config.get_ref('integer_field')) |
| 130 |
print(config.ref_subtraction) |
| 131 |
|
| 132 |
config.integer_field = 10 |
| 133 |
print(config.ref_subtraction) |
| 134 |
|
| 135 |
|
| 136 |
def main(argv=()): |
| 137 |
del argv |
| 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() |