python – PyYaml – overwrite the yaml file and save it

what I'm trying to do is load a yaml file and update its values ​​by argparse and save the resulting yaml in another route.

The output of yaml.save_load is:

import os
import argparse
import yaml
import functions

parser = argparse.ArgumentParser ()
parser.add_argument (
"--config",
nargs = "?",
type = str
default = "p.yml",
help = "Configuration file to use",
)
parser.add_argument (& # 39; - learning_rate & # 39 ;, type = float, default = 0.001, help = & # 39; amount of the learning rate & # 39;)

args = parser.parse_args ()


def dumps (func):
@ functools.wraps (func)
def wrapper (self, * args, ** kwargs):
ret = func (self, * args, ** kwargs)
with open (self.filename, "w") as f:
yaml.safe_dump (self, f)
back ret
return wrap

Config class (dict):
def __init __ (self, filename):
self.filename = filename

If os.path.isfile (file name):
print (file name)
with open (file name) as f:
# use super here to avoid unnecessary writing
super (Config, self) .update (yaml.safe_load (f) or {})

__setitem__ = dumps (dict .__ setitem__)
__delitem__ = dumps (dict .__ delitem__)
update = dumps (dict.update)



cfg = Config (args.config)
print (cfg)
cfg.update ({& # 39; training & # 39 ;: {& # 39; optimizer & # 39 ;: {& # 39; lr & # 39 ;: args.learning_rate}}})
print (cfg)

The original yaml file has the following settings:

> data:
dataset: pascal
train_split: train
val_split: val
img_rows: & # 39; same & # 39;
img_cols: & # 39; same & # 39;
route: SkyScapes /
sbd_path: / private / home / meetshah / datasets / VOC / benchmark_RELEASE /
training:
Trainers: 300000
batch_size: 1
val_interval: 1000
n_workers: 16
print_interval: 50
optimizer:
name: & # 39; sgd & # 39;
lr: 1.0e-10
Weight_decay: 0.0005
Momentum: 0.99
lost:
name: & # 39; cross_entropy & # 39;
size_average: False
lr_schedule:
Summary: fcn8s_pascal_best_model.pkl

and I'm going to update lr parameter. However, I face the following error:

> {& # 39 ;: data & # 39 ;: {& # 39; dataset & # 39 ;: & # 39; pascal & # 39 ;, & # 39; img_cols & # 39 ;: & # 39; same & # 39 ;, & # 39; img_rows & # 39 ;: & # 39; same & # 39 ;, & # 39; sbd_path & # 39 ;: & # 39; / private / home / meetshah / datasets / VOC / benchmark_RELEASE / & # 39; , & # 39; train_split & # 39 ;: & # 39; train & # 39 ;, & # 39; val_split & # 39 ;: & & 39 ;, & # 39 ;, & # 39; path & # 39 ;: & # 39; SkyScapes / & # 39;}, & # 39; training & # 39 ;: {& # 39; resume & # 39 ;: & # 39; fcn8s_pascal_best_model.pkl & # 39 ;, & # 39; train_iters & # 39 ;: 300000, & # 39; lr_schedule & # 39 ;: None, & # 39; optimizer & # 39 ;: {& # 39; name & # 39;: & # 39; sgd & # 39;, & # 39; weight_decay & # 39 ;: 0.0005, & # 39; lr & # 39 ;: 1e-10, & # 39 ;: 1e-10, & # 39; momentum & # 39 ;: 0.99}, & # 39; print_interval & # 39 ;: 50, & # 39; n_workers & # 39 ;: 16, & # 39; batch_size & # 39 ;: 1, & # 39; loss & # 39 ;: {& # 39; name & # 39 ;: & # 39; cross_entropy & # 39 ;, & # 39 ; size_average & # 39 ;: False}, & # 39; val_interval & # 39 ;: 1000}}
Tracking (recent calls latest):
File "test.py", line 70, in 
    cfg.update ({& # 39; training & # 39 ;: {& # 39; optimizer & # 39 ;: {& # 39; lr & # 39 ;: args.learning_rate}}})
File "test.py", line 48, in container
yaml.safe_dump (self, f)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 306, in safe_dump
back dump_all[data], stream, Dumper = SafeDumper, ** kwds)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 278, in dump_all
dumper.represent (data)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 27, in representation
node = self.represent_data (data)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 58, in represent_data
node = self.yaml_representers[None](auto, data)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 231, in represent_undefined
raise RepresenterError ("can not represent an object", data)
yaml.representer.RepresenterError: (& # 39; can not represent an object & # 39 ;, {& # 39; data & # 39 ;: {& # 39; dataset & # 39 ;: & # 39; pascal & # 39 ;, & # 39; img_cols & # 39 ;: & # 39 ;, same & # 39 ;, & # 39; img_rows & # 39 ;: & # 39 ;, same & # 39 ;, & # 39; sbd_path & # 39 ;: & # 39; / private / home / meetshah / datasets / VOC / benchmark_RELEASE / & # 39;, & # 39; train_split & # 39;: & # 39; train & # 39;, & # 39; val_split & # 39;: & # 39; val & # 39;, & # 39; path & # 39;: & # 39; SkyScapes / & # 39;}, & # 39; training & # 39 ;: {& # 39; optimizer & # 39 ;: { & # 39; lr & # 39 ;: 2.0}}})