How to make the weights tranined in pytorch to be accessble for C++


I have built and trained a model in pytorch. Then i need to use the weights from this model to another DL model in C++. While, the weights extracted from pytorch with is in torch.tensor type. It can be print out with no problem. But how can i store it to txt file or any other file type readable for C++ program? I tried write but seems only support string type and i also tried whilt it looks like can only store at .pt or .pth. The problem is the both of these 2 formats can be read by pytorch with no problem but how i can load it from C++ program? no idea about how to convert the torch.tensor data type in .pt to some data type readable for C++.


One way is to convert the matrices to numpy format and save an npz file. Then load the npz file within C++. There’s a C++ library that can read npz files.

Thanks for your reply so much! Gonna try it soon.