Question about ".data"

I have some doubts about pieces of codes:

    def init_conv(self, inp, out, weight, bias, convert=True):
        conv = nn.Conv2d(inp, out, 3, 2, 1, bias=True)
        if convert:
            weight = self.convert_to_torch_weight(weight)
        conv.weight.data = torch.Tensor(weight).float()  
        conv.bias.data = torch.Tensor(bias).float()  
        return conv

I just can’t understand the “.data” in them, can anyone explain what it is used for?
I did’t find the parameter “data” in “nn.Conv2d” from “conv.py”
many thanks

it is an internal attribute, that is mostly used instead of torch.no_grad() context, as autograd misses tensor mutations via .data. and you have to initialize parameter values with autograd disabled, that’s what your code does.

1 Like

Conv2d.weight is inherited from the superclass _ConvNd of Conv2d, as you can see in the source code.

As you can see from the above link, ConvNd.weight is an object of type Parameter. And Parameter has an attribute data, as you can see here.

1 Like