Step_in

不知道该写些什么,第一篇博客,想学点语言,却不知道学哪种,也许就C吧。加油~坚持=胜利。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor class LossCallBack(LossMonitor): """ Monitor the loss in training. If the loss in NAN or INF terminating training. """ def __init__(self, has_trained_epoch=0, per_print_times=per_print_steps): super(LossCallBack, self).__init__() self.has_trained_epoch = has_trained_epoch self._per_print_times = per_print_times def step_end(self, run_context): cb_params = run_context.original_args() loss = cb_params.net_outputs if isinstance(loss, (tuple, list)): if isinstance(loss[0], ms.Tensor) and isinstance(loss[0].asnumpy(), np.ndarray): loss = loss[0] if isinstance(loss, ms.Tensor) and isinstance(loss.asnumpy(), np.ndarray): loss = np.mean(loss.asnumpy()) cur_step_in_epoch = (cb_params.cur_step_num - 1) % cb_params.batch_num + 1 if isinstance(loss, float) and (np.isnan(loss) or np.isinf(loss)): raise ValueError("epoch: {} step: {}. Invalid loss, terminating training.".format( cb_params.cur_epoch_num, cur_step_in_epoch)) if self._per_print_times != 0 and cb_params.cur_step_num % self._per_print_times == 0: # pylint: disable=line-too-long print("epoch: %s step: %s, loss is %s" % (cb_params.cur_epoch_num + int(self.has_trained_epoch), cur_step_in_epoch, loss), flush=True) time_cb = TimeMonitor(data_size=step_size) loss_cb = LossCallBack(has_trained_epoch=0) cb = [time_cb, loss_cb] ckpt_save_dir = cfg['output_dir'] device_target = context.get_context('device_target') if cfg['save_checkpoint']: config_ck = CheckpointConfig(save_checkpoint_steps=save_ckpt_num*step_size, keep_checkpoint_max=10) # config_ck = CheckpointConfig(save_checkpoint_steps=5*step_size, keep_checkpoint_max=10) ckpt_cb = ModelCheckpoint(prefix="resnet", directory=ckpt_save_dir, config=config_ck) cb += [ckpt_cb]
最新发布
07-14

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值