使用configparser
模块读取模型参数,设置config.ini文件内容如下,[train]
和[savepath]
分别为两个session
[train]
lr = 1e-4
epochs = 200
dropout = 0.5
batch_size = 4
[savepath]
path = ../data/
基本操作如下
import configparser
config = configparser.RawConfigParser()
config.read("config.ini")
secs = config.sections()
print(secs)
>>> ['train', 'savepath']
options = config.options("train")
print(options)
>>> ['lr', 'epochs', 'dropout', 'batch_size']
learning_rate = config.getfloat("train", "lr")
print(learning_rate , type(learning_rate))
>>> 0.0001 <class 'float'>
path = config.get('filepath', 'path')
print(path, type(path))
>>> ../data/ <class 'str'>