Reference
param_scope(*args, **kwargs)
Bases: _HyperParameter
A thread-safe hyperparameter context scope
Examples:
create new param_scope
>>> ps = param_scope(a="a", b="b") # create from call arguments
>>> ps = param_scope(**{"a": "a", "b": "b"}) # create from a dict
read parameters from param_scope
>>> ps.a() # read parameter
'a'
>>> ps.c("c") # read parameter with default value if missing
'c'
>>> ps.c | "c" # another way for reading missing parameters
'c'
param_scope
as a context scope
read parameter from param_scope in a function
>>> def foo():
... with param_scope() as ps:
... return ps.a()
>>> with param_scope(**{"a": "a", "b": "b"}) as ps:
... foo() # foo should get param_scope using a with statement
'a'
modify parameters in nested scopes
>>> with param_scope.empty(**{'a': 1, 'b': 2}) as ps:
... _repr_dict(ps.storage().storage())
... with param_scope(**{'b': 3}) as ps:
... _repr_dict(ps.storage().storage())
... with param_scope() as ps:
... _repr_dict(ps.storage().storage())
[('a', 1), ('b', 2)]
[('a', 1), ('b', 3)]
[('a', 1), ('b', 2)]
use object-style parameter key in param_scope
access parameter with param_scope
>>> with param_scope(x=1):
... param_scope.x(2) # read parameter
... param_scope.y(2) # read a missing parameter with default value
... param_scope.y | 2
... param_scope.z = 3
... param_scope.z | 0
1
2
2
3
convert param_scope to dict:
Source code in hyperparameter/api.py
__enter__()
enter a param_scope
context
Examples:
>>> with param_scope():
... param_scope.p = "origin"
... with param_scope(**{"p": "origin"}) as ps:
... ps.storage().storage() # outer scope
... with param_scope() as ps: # unmodified scope
... ps.storage().storage() # inner scope
... with param_scope(**{"p": "modified"}) as ps: # modified scope
... ps.storage().storage() # inner scope with modified params
... _ = param_scope(**{"p": "modified"}) # not used in with ctx
... with param_scope() as ps: # unmodified scope
... ps.storage().storage() # inner scope
{'p': 'origin'}
{'p': 'origin'}
{'p': 'modified'}
{'p': 'origin'}
Source code in hyperparameter/api.py
current()
staticmethod
get current param_scope
Examples:
>>> with param_scope(a=1) as ps:
... param_scope.current().a("empty") # read `a` from current `param_scope`
'1'
>>> with param_scope() as ps1:
... with param_scope(a=1) as ps2:
... param_scope.current().a = 2 # set parameter `a` = 2
... param_scope.a("empty") # read `a` in `ps2`
... param_scope.a("empty") # read `a` in `ps1`, where `a` is not set
'2'
'empty'
Source code in hyperparameter/api.py
empty(*args, **kwargs)
staticmethod
create an empty param_scope
.
Examples:
>>> with param_scope(a="not empty") as ps: # start a new param_scope `a` = 'not empty'
... param_scope.a("empty") # read parameter `a`
... with param_scope.empty() as ps2: # parameter `a` is cleared in ps2
... param_scope.a("empty") # read parameter `a` = 'empty'
'not empty'
'empty'
Source code in hyperparameter/api.py
auto_param(name_or_func)
Convert keyword arguments into hyperparameters
Examples:
classes are also supported:
>>> @auto_param('myns.foo.params')
... def foo(a, b=2, c='c', d=None):
... print(a, b, c, d)
>>> foo(1)
1 2 c None
>>> with param_scope('myns.foo.params.b=3'):
... param_scope.myns.foo.params.b = 4
... foo(2)
2 4 c None
Source code in hyperparameter/api.py
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
|