本文整理汇总了Python中json5.dump方法的典型用法代码示例。如果您正苦于以下问题:Python json5.dump方法的具体用法?Python json5.dump怎么用?Python json5.dump使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类json5
的用法示例。
在下文中一共展示了json5.dump方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_model
# 需要导入模块: import json5 [as 别名]
# 或者: from json5 import dump [as 别名]
def build_model(self):
states = {}
interface = Interface(self.args, self.log)
self.log(f'#classes: {self.args.num_classes}; #vocab: {self.args.num_vocab}')
if self.args.seed:
random.seed(self.args.seed)
torch.manual_seed(self.args.seed)
if self.args.cuda:
torch.cuda.manual_seed(self.args.seed)
if self.args.deterministic:
torch.backends.cudnn.deterministic = True
model = Model(self.args)
if self.args.pretrained_embeddings:
embeddings = interface.load_embeddings()
model.set_embeddings(embeddings)
# set initial states
states['start_epoch'] = 1
states['best_eval'] = 0.
states['best_epoch'] = 0
states['best_step'] = 0
self.log(f'trainable params: {model.num_parameters():,d}')
self.log(f'trainable params (exclude embeddings): {model.num_parameters(exclude_embed=True):,d}')
validate_params(self.args)
with open(os.path.join(self.args.summary_dir, 'args.json5'), 'w') as f:
json5.dump(self.args.__dict__, f, indent=2)
self.log(pformat(vars(self.args), indent=2, width=120))
return model, interface, states
示例2: build_model
# 需要导入模块: import json5 [as 别名]
# 或者: from json5 import dump [as 别名]
def build_model(self, sess):
states = {}
interface = Interface(self.args, self.log)
self.log(f'#classes: {self.args.num_classes}; #vocab: {self.args.num_vocab}')
if self.args.seed:
random.seed(self.args.seed)
np.random.seed(self.args.seed)
tf.set_random_seed(self.args.seed)
model = Model(self.args, sess)
sess.run(tf.global_variables_initializer())
embeddings = interface.load_embeddings()
model.set_embeddings(sess, embeddings)
# set initial states
states['start_epoch'] = 1
states['best_eval'] = 0.
states['best_epoch'] = 0
states['best_step'] = 0
self.log(f'trainable params: {model.num_parameters():,d}')
self.log(f'trainable params (exclude embeddings): {model.num_parameters(exclude_embed=True):,d}')
validate_params(self.args)
with open(os.path.join(self.args.summary_dir, 'args.json5'), 'w') as f:
args = {k: v for k, v in vars(self.args).items() if not k.startswith('_')}
json5.dump(args, f, indent=2)
self.log(pformat(vars(self.args), indent=2, width=120))
return model, interface, states
示例3: test_basic
# 需要导入模块: import json5 [as 别名]
# 或者: from json5 import dump [as 别名]
def test_basic(self):
sio = io.StringIO()
json5.dump(True, sio)
self.assertEqual('true', sio.getvalue())
示例4: _post_process
# 需要导入模块: import json5 [as 别名]
# 或者: from json5 import dump [as 别名]
def _post_process(args: Object):
if not args.output_dir.startswith('models'):
args.output_dir = os.path.join('models', args.output_dir)
os.makedirs(args.output_dir, exist_ok=True)
if not args.name:
args.name = str(datetime.now())
args.summary_dir = os.path.join(args.output_dir, args.name)
if os.path.exists(args.summary_dir):
shutil.rmtree(args.summary_dir)
os.makedirs(args.summary_dir)
data_config_file = os.path.join(args.output_dir, 'data_config.json5')
if os.path.exists(data_config_file):
with open(data_config_file) as f:
config = json5.load(f)
for k, v in config.items():
if not hasattr(args, k) or getattr(args, k) != v:
print('ERROR: Data configurations are different. Please use another output_dir or '
'remove the older one manually.')
exit()
else:
with open(data_config_file, 'w') as f:
keys = ['data_dir', 'min_df', 'max_vocab', 'max_len', 'min_len', 'lower_case',
'pretrained_embeddings', 'embedding_mode']
json5.dump({k: getattr(args, k) for k in keys}, f)
args.metric = args.metric.lower()
args.watch_metrics = [m.lower() for m in args.watch_metrics]
if args.metric not in args.watch_metrics:
args.watch_metrics.append(args.metric)
args.cuda = args.cuda and torch.cuda.is_available()
args.fix_embeddings = args.pretrained_embeddings and args.fix_embeddings
def samples2steps(n):
return int(math.ceil(n / args.batch_size))
if not hasattr(args, 'log_per_updates'):
args.log_per_updates = samples2steps(args.log_per_samples)
if not hasattr(args, 'eval_per_updates'):
args.eval_per_updates = samples2steps(args.eval_per_samples)
if not hasattr(args, 'eval_per_updates_warmup'):
args.eval_per_updates_warmup = samples2steps(args.eval_per_samples_warmup)
if not hasattr(args, 'eval_warmup_steps'):
args.eval_warmup_steps = samples2steps(args.eval_warmup_samples)
if not hasattr(args, 'min_steps'):
args.min_steps = samples2steps(args.min_samples)
if not hasattr(args, 'early_stopping'):
args.early_stopping = samples2steps(args.tolerance_samples)
if not hasattr(args, 'lr_warmup_steps'):
args.lr_warmup_steps = samples2steps(args.lr_warmup_samples)
if not hasattr(args, 'lr_decay_steps'):
args.lr_decay_steps = samples2steps(args.lr_decay_samples)
if not hasattr(args, 'summary_per_updates'):
args.summary_per_updates = args.summary_per_logs * args.log_per_updates
assert args.lr >= args.min_lr, 'initial learning rate must be larger than min_lr'
示例5: __init__
# 需要导入模块: import json5 [as 别名]
# 或者: from json5 import dump [as 别名]
def __init__(self, config, resume, model, optimizer, loss_function):
self.n_gpu = torch.cuda.device_count()
self.device = self._prepare_device(self.n_gpu, cudnn_deterministic=config["cudnn_deterministic"])
self.model = model.to(self.device)
if self.n_gpu > 1:
self.model = torch.nn.DataParallel(self.model, device_ids=list(range(self.n_gpu)))
self.optimizer = optimizer
self.loss_function = loss_function
# Trainer
self.epochs = config["trainer"]["epochs"]
self.save_checkpoint_interval = config["trainer"]["save_checkpoint_interval"]
self.validation_config = config["trainer"]["validation"]
self.validation_interval = self.validation_config["interval"]
self.find_max = self.validation_config["find_max"]
self.validation_custom_config = self.validation_config["custom"]
self.start_epoch = 1
self.best_score = -np.inf if self.find_max else np.inf
self.root_dir = Path(config["root_dir"]) / config["experiment_name"]
self.checkpoints_dir = self.root_dir / "checkpoints"
self.logs_dir = self.root_dir / "logs"
prepare_empty_dir([self.checkpoints_dir, self.logs_dir], resume=resume)
self.writer = visualization.writer(self.logs_dir.as_posix())
self.writer.add_text(
tag="Configuration",
text_string=f"<pre> \n{json5.dumps(config, indent=4, sort_keys=False)} \n</pre>",
global_step=1
)
if resume: self._resume_checkpoint()
print("Configurations are as follows: ")
print(json5.dumps(config, indent=2, sort_keys=False))
with open((self.root_dir / f"{time.strftime('%Y-%m-%d-%H-%M-%S')}.json").as_posix(), "w") as handle:
json5.dump(config, handle, indent=2, sort_keys=False)
self._print_networks([self.model])
示例6: _post_process
# 需要导入模块: import json5 [as 别名]
# 或者: from json5 import dump [as 别名]
def _post_process(args: Object):
if not args.output_dir.startswith('models'):
args.output_dir = os.path.join('models', args.output_dir)
os.makedirs(args.output_dir, exist_ok=True)
if not args.name:
args.name = str(datetime.now())
args.summary_dir = os.path.join(args.output_dir, args.name)
if os.path.exists(args.summary_dir):
shutil.rmtree(args.summary_dir)
os.makedirs(args.summary_dir)
data_config_file = os.path.join(args.output_dir, 'data_config.json5')
if os.path.exists(data_config_file):
with open(data_config_file) as f:
config = json5.load(f)
for k, v in config.items():
if not hasattr(args, k) or getattr(args, k) != v:
print('ERROR: Data configurations are different. Please use another output_dir or '
'remove the older one manually.')
exit()
else:
with open(data_config_file, 'w') as f:
keys = ['data_dir', 'min_df', 'max_vocab', 'max_len', 'min_len', 'lower_case',
'pretrained_embeddings', 'embedding_mode']
json5.dump({k: getattr(args, k) for k in keys}, f)
args.metric = args.metric.lower()
args.watch_metrics = [m.lower() for m in args.watch_metrics]
if args.metric not in args.watch_metrics:
args.watch_metrics.append(args.metric)
assert args.pretrained_embeddings, 'pretrained embeddings must be provided.'
def samples2steps(n):
return int(math.ceil(n / args.batch_size))
if not hasattr(args, 'log_per_updates'):
args.log_per_updates = samples2steps(args.log_per_samples)
if not hasattr(args, 'eval_per_updates'):
args.eval_per_updates = samples2steps(args.eval_per_samples)
if not hasattr(args, 'eval_per_updates_warmup'):
args.eval_per_updates_warmup = samples2steps(args.eval_per_samples_warmup)
if not hasattr(args, 'eval_warmup_steps'):
args.eval_warmup_steps = samples2steps(args.eval_warmup_samples)
if not hasattr(args, 'min_steps'):
args.min_steps = samples2steps(args.min_samples)
if not hasattr(args, 'early_stopping'):
args.early_stopping = samples2steps(args.tolerance_samples)
if not hasattr(args, 'lr_warmup_steps'):
args.lr_warmup_steps = samples2steps(args.lr_warmup_samples)
if not hasattr(args, 'lr_decay_steps'):
args.lr_decay_steps = samples2steps(args.lr_decay_samples)
示例7: __init__
# 需要导入模块: import json5 [as 别名]
# 或者: from json5 import dump [as 别名]
def __init__(self,
config,
resume: bool,
model,
loss_function,
optimizer):
self.n_gpu = torch.cuda.device_count()
self.device = self._prepare_device(self.n_gpu, cudnn_deterministic=config["cudnn_deterministic"])
self.optimizer = optimizer
self.loss_function = loss_function
self.model = model.to(self.device)
if self.n_gpu > 1:
self.model = torch.nn.DataParallel(self.model, device_ids=list(range(self.n_gpu)))
# Trainer
self.epochs = config["trainer"]["epochs"]
self.save_checkpoint_interval = config["trainer"]["save_checkpoint_interval"]
self.validation_config = config["trainer"]["validation"]
self.validation_interval = self.validation_config["interval"]
self.find_max = self.validation_config["find_max"]
self.validation_custom_config = self.validation_config["custom"]
# The following args is not in the config file. We will update it if the resume is True in later.
self.start_epoch = 1
self.best_score = -np.inf if self.find_max else np.inf
self.root_dir = Path(config["root_dir"]).expanduser().absolute() / config["experiment_name"]
self.checkpoints_dir = self.root_dir / "checkpoints"
self.logs_dir = self.root_dir / "logs"
prepare_empty_dir([self.checkpoints_dir, self.logs_dir], resume=resume)
self.writer = visualization.writer(self.logs_dir.as_posix())
self.writer.add_text(
tag="Configuration",
text_string=f"<pre> \n{json5.dumps(config, indent=4, sort_keys=False)} \n</pre>",
global_step=1
)
if resume: self._resume_checkpoint()
print("Configurations are as follows: ")
print(json5.dumps(config, indent=2, sort_keys=False))
with open((self.root_dir / f"{time.strftime('%Y-%m-%d-%H-%M-%S')}.json").as_posix(), "w") as handle:
json5.dump(config, handle, indent=2, sort_keys=False)
self._print_networks([self.model])