// 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);