使用yaml配置文件

29 篇文章 6 订阅
23 篇文章 5 订阅

yaml:

yet another markup language,另一种标记语言,专门用来写配置文件,类似于json格式。

语法

  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进时不允许使用Tab键,只允许使用空格
  • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
  • # 表示注释,从这个字符一直到行尾,都会被解析器忽略

数据结构:

  • 对象:键值对的集合,又称为映射/哈希/字典
  • 数组:一组按次序排列的值,又称为序列/列表
  • 纯量:单个的、不可再分的值。字符串、布尔值、整数、浮点数、null、时间、日期

序列:

1.yaml里面写一个数组,前面加一个‘-’符号,如下

- admin1: 123456
- admin2: 111111
- admin3: 222222

对应python里面的list

[{'admin1': 123456},
{'admin2': 111111}, 
{'admin3': 222222}]

注意:数字读出来的是int或float类型

纯量:

在yaml文件写入以下内容:

n1: 12.30
n2: true
n3: false
n4: ~
time1: 2018-04-18t21:59:43.10+08:00
date1: 2018-04-18
n6: !!str 123
n7: !!str true

python读取结果:

{'n1': 12.3, 
'n2': True, 
'n3': False, 
'n4': None, 
'time1': datetime.datetime(2018, 4, 18, 13, 59, 43, 100000), 
'date1': datetime.date(2018, 4, 18), 
'n6': '123', 
'n7': 'true'}

混合使用:

1.list嵌套dict,在yaml里面写入如下内容:

- user: admin1
  psw: '123456' 
- user: admin2
  psw: '111111' 
- user: admin3
  psw: '222222'
用python读取出来的结果:

[{'user': 'admin1', 'psw': '123456'},
{'user': 'admin2', 'psw': '111111'},
{'user': 'admin3', 'psw': '222222'}]

2.dict嵌套list,在yaml里面写入如下内容:
nub1:
    - admin1
    - '123456' 
nb2:
    - admin2
    - '111111' 
nb3:
    - admin3
    - '222222'
用python读取出来的结果:

{'nub1': ['admin1', '123456'],
'nb2': ['admin2', '111111'], 
'nb3': ['admin3', '222222']

读取yaml格式文件:

with open('./config.yml') as file_config:
    config=yaml.load(file_config)

feature_names=config['model_params']['feature_names']


#以下是config配置内容例子

data_params:
  path_result: ../data/lstm_crf/result.txt
  path_test: ../data/lstm_crf/test.txt
  path_train: ../data/lstm_crf/train.txt
  sep: table
  voc_params:
    char:
      min_count: 1
      path: null
    f1:
      min_count: 1
      path: ../data/lstm_crf/train_test_word2ids.pkl
    f2:
      min_count: 1
      path: null
    label:
      min_count: 0
      path: ../data/lstm_crf/train_test_tag2ids.pkl
model: lstm_crf
model_params:
  batch_size: 128
  bilstm_params:
    num_layers: 1
    num_units: 256
    rnn_dropout: 0.2
  clip: 10
  conv_dropout: 0.2
  conv_filter_len_list:
  - 1
  - 2
  - 3
  - 4
  - 5
  conv_filter_size_list:
  - 8
  - 8
  - 8
  - 8
  - 8
  dev_size: 0.1
  dropout_rate: 0.3
  embed_params:
    char:
      dropout_rate: null
      path: null
      path_pre_train: null
      shape:
      - null
      - null
    f1:
      dropout_rate: 0.3
      path: ../data/lstm_crf/f1_embed.mat.pkl
      path_pre_train: /E/home/mayajun/PycharmProjects/daguan_lstm_crf/data/word2vec.txt
      shape:
      - 4685
      - 256
    f2:
      dropout_rate: 0.3
      path: null
      path_pre_train: null
      shape:
      - null
      - null
  feature_names:
  - f1
  l2_rate: 0.001
  learning_rate: 5.0e-05
  max_patience: 5
  nb_classes: 4
  nb_epoch: 1000
  path_model: ../model/lstm_crf/model
  rnn_unit: lstm
  sequence_len_pt: 95
  sequence_length: 128
  use_char_feature: false
  use_crf: true
  word_len_pt: 95
  word_length: 11
unknow_word: unk

 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值