yenw opened a new issue #17659: Did some C ++ APIs become abnormal in version 
1.6?
URL: https://github.com/apache/incubator-mxnet/issues/17659
 
 
   As shown in the following code, 
   `case 2`:  if a model does not exist then initialize an empty model, 
   `case 1`: otherwise load the existing model.
   
   In version 1.4.1, `case 1` and `case 2` worked fine.
   In version 1.6, `case 2` can be compiled, but the output of neural network 
is `Nan`.
   In version 1.6, `case 1` can load the model saved in version 1.4, and the 
output is normal.
   
   I want to know if the C++ API has changed in version 1.6?
   How should I initialize the model correctly in version 1.6?
   
   ```cpp
   void predict(vector<float>& data_batch, vector<float>& output)
   {
       if (predict_exec == nullptr)
       {
           auto model = GetSymbol(body_blocks, body_filters);
           std::map<string, NDArray> args_map, aux_map;
           args_map["data"] = NDArray(Shape(predict_batch_size, img_channel, 
img_height, img_width), ctx);
   
           if (train_iter != 0)
           {
               // case 1: Load an existing model
               std::map<std::string, NDArray> arg_grad_store;
               std::map<std::string, OpReqType> grad_req_type;
   
               weight_load(args_map, aux_map);
               predict_exec = model.SimpleBind(ctx, args_map, arg_grad_store, 
grad_req_type, aux_map);
           }
           else
           {
               // case 2: Initialize an empty model
               predict_exec = model.SimpleBind(ctx, args_map);
               args_map = predict_exec->arg_dict();
               weight_init(args_map);
   
               //model.InferArgsMap(ctx, &args_map, args_map);
               //weight_init(args_map);
               //predict_exec = model.SimpleBind(ctx, args_map);
           }
       }
   
       auto data = NDArray(data_batch, Shape(predict_batch_size, img_channel, 
img_height, img_width), ctx);
       data.CopyTo(&predict_exec->arg_dict()["data"]);
       NDArray::WaitAll();
       predict_exec->Forward(false);
       NDArray::WaitAll();
       auto out_0 = predict_exec->outputs[0];// normal in 1.4.1, output Nan in 
1.6
       out_0.SyncCopyToCPU(&output);
   }
   
   void weight_init(std::map<std::string, NDArray>& args_map)
   {
       auto xavier = Xavier(Xavier::gaussian, Xavier::avg, 3.0f);
       for (auto &arg: args_map)
           xavier(arg.first, &arg.second);
   }
   
   void weight_load(std::map<std::string, NDArray>& args_map, 
std::map<std::string, NDArray>& aux_map)
   {
       string load_path = "xxx";
   
       std::map<std::string, NDArray> params = NDArray::LoadToMap(load_path);
       for (auto iter : params)
       {
           string type = iter.first.substr(0, 4);
           string name = iter.first.substr(4);
           NDArray target;
           if (type == "arg:")
               args_map.insert({name, iter.second});
           else if (type == "aux:")
               aux_map.insert({name, iter.second});
       }
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to