Set DNN weights

(Afshin Oroojlooy) #1


I was looking for an API to set weight values in libtorch. In the python version, one can easily use and But, it seems that such an API does not exist in C++ yet.
I would appreciate any help or comment to achieve such functionality.


(Martin Huber) #2

So I am not 100% sure what you are doing but you can simply provide a function in your network that does the following

void YourNet::Init() {

    torch::NoGradGuard no_grad;
    for (auto& p : this->parameters()) {
        p.uniform_(-1, 1); // or whatever initialization you are looking for, see link below

And then call this function in your constructor or elsewhere. Other possible initializations can be found in the documentation of Tensor

(Afshin Oroojlooy) #3

I came up with a different solution, and this looks shorter.