Reference
scope(*args, **kwargs)
Bases: _HyperParameter
A thread-safe hyperparameter context scope
Examples:
create new scope
>>> ps = scope(a="a", b="b") # create from call arguments
>>> ps = scope(**{"a": "a", "b": "b"}) # create from a dict
read parameters from 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'
scope as a context scope
read parameter from scope in a function
>>> def foo():
... with scope() as ps:
... return ps.a()
>>> with scope(**{"a": "a", "b": "b"}) as ps:
... foo() # foo should get scope using a with statement
'a'
modify parameters in nested scopes
>>> with scope.empty(**{'a': 1, 'b': 2}) as ps:
... _repr_dict(ps.storage().storage())
... with scope(**{'b': 3}) as ps:
... _repr_dict(ps.storage().storage())
... with 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 scope
access parameter with scope
>>> with scope(x=1):
... scope.x(2) # read parameter
... scope.y(2) # read a missing parameter with default value
... scope.y | 2
... scope.z = 3
... scope.z | 0
1
2
2
3
convert scope to dict:
Source code in hyperparameter/api.py
__enter__()
enter a scope context
Examples:
>>> with scope():
... scope.p = "origin"
... with scope(**{"p": "origin"}) as ps:
... ps.storage().storage() # outer scope
... with scope() as ps: # unmodified scope
... ps.storage().storage() # inner scope
... with scope(**{"p": "modified"}) as ps: # modified scope
... ps.storage().storage() # inner scope with modified params
... _ = scope(**{"p": "modified"}) # not used in with ctx
... with 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 scope
Examples:
>>> with scope() as ps1:
... with scope(a=1) as ps2:
... scope.current().a = 2 # set parameter `a` = 2
... scope.a("empty") # read `a` in `ps2`
... 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 scope.
Examples:
>>> with scope(a="not empty") as ps: # start a new scope `a` = 'not empty'
... scope.a("empty") # read parameter `a`
... with scope.empty() as ps2: # parameter `a` is cleared in ps2
... scope.a("empty") # read parameter `a` = 'empty'
'not empty'
'empty'
Source code in hyperparameter/api.py
param(name_or_func)
Convert keyword arguments into hyperparameters
Examples:
classes are also supported:
>>> @param('myns.foo.params')
... def foo(a, b=2, c='c', d=None):
... print(a, b, c, d)
>>> foo(1)
1 2 c None
Source code in hyperparameter/api.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 | |