pytorch / examples

A set of examples around pytorch in Vision, Text, Reinforcement Learning, etc.
https://pytorch.org/examples
BSD 3-Clause "New" or "Revised" License
22k stars 9.49k forks source link

How to save model in mnist.cpp? #517

Open engineer1109 opened 5 years ago

engineer1109 commented 5 years ago

How to save the model in cpp api mnist.cpp? model.save and torch::save(model,"mnisttrain.pkl") All error

gaohaihui commented 5 years ago

parser.add_argument('--save-model', action='store_true', default=False, help='For Saving the current Model') default rewrite True in parameter

engineer1109 commented 5 years ago

@gaohaihui I mean the minst.cpp ,c++ api ,not python

xiaoLiuxiaoLiuxiaoLiu commented 4 years ago

You can save all layers weights and bias at a vector, and torch::save and torch::load will be useful in this situation. Any easier implement ? Plz comment.

soumith commented 4 years ago

cc: @yf225

xiaoLiuxiaoLiuxiaoLiu commented 4 years ago

Here is a very stupid but useful implement of model_load and model_save. In model_load, Load a std::vector and save every weights and bias in a "inter_layer.pt" file. And then, with the code of torch::load(model.parameters()[i], "inter_layer.pt"), layers parameters can be loaded. In contrary, with the code of { std::vector a; a.push_back(model.parameters()[i])}, layers parameters can be saved as Vector File. Unfortunately, with my test, a complex model with many structures cannot be save and load directly. Plz comment if any easier implement exists.