Search code examples
torchlibtorch

Set neural network initial weight values in C++ torch


I was looking for an API to set initial weight values in libtorch. In the python version, (i.e. pytorch) 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


Solution

  • I got this solution better than the previous one in which model is an object of type torch::nn::Sequential:

    torch::NoGradGuard no_grad;
    
    for (auto &p : model->named_parameters()) {
        std::string y = p.key();
        auto z = p.value(); // note that z is a Tensor, same as &p : layers->parameters
    
        if (y.compare(2, 6, "weight") == 0)
            z.uniform_(l, u);
        else if (y.compare(2, 4, "bias") == 0)
            z.uniform_(l, u);
    }
    

    Instead of uniform_ you can use normal_, ... which are available on torch. This solution is not limited to torch::nn::Linear layer and can be used foy any layer type.