test_20220105.yaml 内容如下:
decoder: transformer
grad_clip: 5
accum_grad: 4
max_epoch: 240
log_interval: 100
optim: adam
shuffle: true
optim_conf:
lr: 0.002
dataset_conf:
filter_conf:
max_length: 40960
min_length: 0
token_max_length: 200
token_min_length: 1
代码示例:
import yaml
config = "test_20220105.yaml"
with open(config, 'r', encoding='utf-8') as fin:
configs = yaml.load(fin, Loader=yaml.FullLoader)
"""
取key对应的value值的两种方式:
1、configs['key']
2、configs.get('key')
根据下面的例子,显然第一种方式更简单。
"""
max_epoch = configs['max_epoch']
max_epoch = configs.get('max_epoch')
max_length = configs['dataset_conf']['filter_conf']['max_length']
max_length = configs.get('dataset_conf').get('filter_conf').get('max_length')
"""
设置默认值的方法:
configs.get('key', value) value为默认值
"""
num_epochs = configs.get('max_epoch', 100)
type = configs.get('dynamic', 'static')