MXNet C++ 保存和加载模型参数

    // infer args automatically
    std::map<std::string, NDArray> args_map;

    args_map["data"] = NDArray(Shape(batch_size, 1, image_high, image_wdith), ctx_dev);
    net.InferArgsMap(ctx_dev, &args_map, args_map);

args_map可以自动根据输入数据维度推断出来,这里面保存了网络的各种权重,需要保存。

通过auto *exec = net.SimpleBind(ctx_dev, args_map);创建Executor后,Executor里面的aux_map通常也需要保存, 

其作用为:  * \param aux_map NDArray that stores the internal state in op
例如使用了BatchNorm层,BatchNorm的均值和方差就放在  std::vector<NDArray> aux_arrays 里面
 

保存参数

// save model weights
string Weight_args_Name = string("weight_file_args_epoc") + std::to_string(epoch) + ".txt";

auto save_args = args_map;
// we do not want to save the data and label
save_args.erase(save_args.find("data"));
save_args.erase(save_args.find("label"));
NDArray::Save(Weight_args_Name, save_args);

string Weight_auxi_Name = string("weight_file_auxi_epoc") + std::to_string(epoch) + ".txt";
NDArray::Save(Weight_auxi_Name, exec->aux_dict());

加载参数

// initialze parameters, initialize by random or pre-trained model
//	ArgsInitialize(args_map);

NDArray::Load("weight_file_args_epoc2.txt", nullptr, &args_map);

std::map<std::string, NDArray> aux_map = NDArray::LoadToMap("weight_file_auxi_epoc2.txt");

// Create executor by binding parameters to the model
auto *exec = net.SimpleBind(ctx_dev, args_map, std::map<std::string, NDArray>(), std::map<std::string, OpReqType>(), aux_map);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luchang-Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值