from collections import namedtuple
a = {'a':12,'b':123}
h = namedtuple('HParams',a.keys())(**a)
print(h)
print(h.a)
>>HParams(a=12, b=123)
12
使用FLAGS.__flags.items()
将所有的超参数取出来,更换名字放到一个命名元组里面方便使用。
# Make a namedtuple hps, containing the values of the hyperparameters that the model needs
hparam_list = ['mode', 'lr', 'adagrad_init_acc', 'rand_unif_init_mag', 'trunc_norm_init_std', 'max_grad_norm', \
'hidden_dim', 'emb_dim', 'batch_size', 'max_dec_sen_num','max_dec_steps', 'max_enc_steps']
hps_dict = {}
for key,val in FLAGS.__flags.items(): # for each flag
if key in hparam_list: # if it's in the list
hps_dict[key] = val # add it to the dict
hps_generator = namedtuple("HParams", hps_dict.keys())(**hps_dict)