设置¶
示例用法:
# Simple getting and setting.
print(pyro.settings.get()) # print all settings
print(pyro.settings.get("cholesky_relative_jitter")) # print one
pyro.settings.set(cholesky_relative_jitter=0.5) # set one
pyro.settings.set(**my_settings) # set many
# Use as a contextmanager.
with pyro.settings.context(cholesky_relative_jitter=0.5):
my_function()
# Use as a decorator.
fn = pyro.settings.context(cholesky_relative_jitter=0.5)(my_function)
fn()
# Register a new setting.
pyro.settings.register(
"binomial_approx_sample_thresh", # alias
"pyro.distributions.torch", # module
"Binomial.approx_sample_thresh", # deep name
)
# Register a new setting on a user-provided validator.
@pyro.settings.register(
"binomial_approx_sample_thresh", # alias
"pyro.distributions.torch", # module
"Binomial.approx_sample_thresh", # deep name
)
def validate_thresh(thresh): # called each time setting is set
assert isinstance(thresh, float)
assert thresh > 0
默认设置¶
二项式近似对数概率容差 = 0.0
二项式近似样本阈值 = 无穷大
cholesky_relative_jitter = 4.0
module_local_params = False
validate_distributions_pyro = True
validate_distributions_torch = True
validate_infer = True
validate_poutine = True
设置界面¶
- get(alias: Optional[str] = None) Any [source]¶
获取一个或所有全局设置。
- Parameters
alias (str) – 已注册设置的名称。
- Returns
当前设置的值。
- context(**kwargs) Iterator[None] [source]¶
上下文管理器用于临时覆盖一个或多个设置。这也作为装饰器使用。
- Parameters
**kwargs – 别名=值 对。
- register(alias: str, modulename: str, deepname: str, validator: Optional[Callable] = None) Callable [source]¶
注册一个全局设置。
这应该在定义设置的模块中声明。
这可以作为声明使用:
settings.register("my_setting", __name__, "MY_SETTING")
或者作为用户定义的验证器函数的装饰器:
@settings.register("my_setting", __name__, "MY_SETTING") def _validate_my_setting(value): assert isinstance(value, float) assert 0 < value