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
init(params=None)
staticmethod
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
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 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 | |
launch(func=None, *, _caller_globals=None, _caller_locals=None)
Launch CLI for @auto_param functions.
- launch(f): expose a single @auto_param function f as CLI.
- launch(): expose all @auto_param functions in the caller module as subcommands.
Source code in hyperparameter/api.py
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 | |
run_cli(func=None)
Alias for launch() with a less collision-prone name.