| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
r"""Example of basic DEFINE_config_dataclass usage. |
| 16 |
|
| 17 |
To run this example: |
| 18 |
python define_config_dataclass_basic.py -- --my_config.field1=8 \ |
| 19 |
--my_config.nested.field=2.1 --my_config.tuple='(1, 2, (1, 2))' |
| 20 |
""" |
| 21 |
|
| 22 |
import dataclasses |
| 23 |
from typing import Any, Mapping, Sequence |
| 24 |
|
| 25 |
from absl import app |
| 26 |
from ml_collections import config_flags |
| 27 |
|
| 28 |
|
| 29 |
@dataclasses.dataclass |
| 30 |
class MyConfig: |
| 31 |
field1: int |
| 32 |
field2: str |
| 33 |
nested: Mapping[str, Any] |
| 34 |
tuple: Sequence[int] |
| 35 |
|
| 36 |
|
| 37 |
config = MyConfig( |
| 38 |
field1=1, |
| 39 |
field2='tom', |
| 40 |
nested={'field': 2.23}, |
| 41 |
tuple=(1, 2, 3), |
| 42 |
) |
| 43 |
|
| 44 |
_CONFIG = config_flags.DEFINE_config_dataclass('my_config', config) |
| 45 |
|
| 46 |
|
| 47 |
def main(_): |
| 48 |
print(_CONFIG.value) |
| 49 |
|
| 50 |
|
| 51 |
if __name__ == '__main__': |
| 52 |
app.run(main) |