Skip to main content

所有参数类

项目描述

参数

简单可配置的参数/配置类。无需手动模式!
使用 python3.6+ 类型提示来声明参数类型,或者让 eparams 从分配中推断它,并防止您分配错误的类型!

它的核心是作为dataclass.

安装

$pip install eparams

特征

  • 使用typeguard自动类型检查参数。
  • 自动键入 - 不会强迫您在任何地方添加类型提示。
  • 定义一个自定义/使用我们的默认预处理函数将您的输入转换为正确的类型。
  • 在类定义中使用可变类型(list dict等),不用担心,也没有尴尬的强制性工厂对象。
  • 防止通常会导致分配未声明参数的拼写错误。
  • 定义自定义约束。
  • 冻结/解冻类。
  • 历史追踪。
    • 跟踪对类的分配以查看分配了哪些属性以及从哪些值分配。
  • 添加了辅助方法:
    • _to_json() _to_yaml() _to_dict() _from_json() _from_yaml() _from_dict()
    • copy() _freeze() _unfreeze() _history()
    • __contains__() __eq__() __getitem__() __setitem__()
  • 所有上述功能都可以根据参数和全局完全配置。
  • 内置嵌套,具有conf.sub_conf.some_val结构,所有方法都以递归方式工作。与dataclass.
  • 辅助功能:
    • 递归比较:比较来自不同版本代码/跨实验的配置
    • 在字典中注册部分配置函数,以便使用 cli 和外部配置轻松调用。

示例用法:

有关“完整”项目中的示例用法,请参见sample_project ,其中包括:

  • cli 与argparse.
  • 增量配置注册。
  • 约束。
  • 冻结类。
  • yaml/dict 导出。

基本用法:

from eparams import params

# Basic usage
@params
class OptimizerParams:
    learning_rate = 1e-2
    weight_decay = 0.001
    batch_size = 1024

@params
class Params:
  name = 'default name'
  optim = OptimizerParams()
  verbose = False

config = Params(name='new name')
config.optim.batch_size = 2048  # ok
config.optim.learning_rate = '0.001' # ok, cast to float
config.optim.batch_size = '32' # ok, cast to int
config.verbose = 'True' # ok, cast to bool
config['optim.weight_decay'] = 0  # we can set nested attributes like this as-well
print(config)
# prints: 
# name='new name'
# optim.learning_rate=0.001
# optim.weight_decay=0
# optim.batch_size=32
# verbose=True
config._to_yaml('/path/to/config.yaml')  # save as yaml

# The following lines will raise an exception
config.optim.batch_size = 'string'  # raises ValueError: invalid literal for int() with base 10: 'string'
config.optim.batch_size = 1.3  # raises TypeError: type of batch_size must be int; got float instead
config.verrrbose = False  # raises ValueError: Cannot assign <verrrbose> to class <<class '__main__.Params'>>, missing from class definition (allow_dynamic_attribute=False)

调试和比较旧/不同运行的配置:

from eparams import params, params_compare

# type_verify=False does not check for typing errors.
# default_preprocessor=None removes the preprocessing step (a custom function is also valid here)
# allow_dynamic_attribute=True allows the class to dynamically set new attributes.
@params(type_verify=False, default_preprocessor=None, allow_dynamic_attribute=True)
class Params:
  name = 'default name'
  my_int = 0

old_config = Params()._from_yaml('/path/to/old/config.yaml', strict=False)  # load some old config
print(old_config._history())  # show parameters that were loaded and their pre-loaded value
params_not_in_old_yaml = [k for k, hist in old_config._history(full=True).items() if not hist]
modified, added, removed = params_compare(old_config, Params())  # compare two versions of configs

预处理示例:

import enum
from pathlib import Path
from eparams import params, Var


class MyEnum(enum.Enum):
    a = 'a'
    b = 'b'

@params
class Example:
    num: int = 3
    type = MyEnum.b
    path: Path = '/user/home'  # cast to Path
    some_list = [1, 2, 3]  # we copy the list before assignment
    some_string = Var('yo_yo', preprocess_fn=str.lower)  # lower string

ex = Example()
ex.num = '4'  # cast to int
ex.type = 'a'  # cast to MyEnum
ex.some_string = 'HELLO'  # .lower()

assert ex.some_list is not Example.some_list
print(ex)
# prints:
# num=4
# type=<MyEnum.a: 'a'>
# path=PosixPath('/user/home')
# some_list=[1, 2, 3]
# some_string='hello'

其他流行的方法/库来定义参数:

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

eparams-0.1.tar.gz (13.0 kB 查看哈希)

已上传 source

内置分布

eparams-0.1-py3-none-any.whl (11.4 kB 查看哈希

已上传 py3