Set DNN weights

Hi,

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

Thanks,
Afshin

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

1 Like

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