What is the default initial weights for pytorch-geometric SAGEconv layer and other convolution layers? and how to initialize them using Xavier?

Hello,
what is the default initial weights for pytorch-geometric SAGEconv layer and other convolution layers? and how to initialize them using Xavier?

I need guidance in how to apply Xavier initialization in graph neural network, I am using pytorch geometry

I tried this, but it didn’t work,

def initialize_weights(self):
for m in self.modules():
if isinstance(m,SAGEConv):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)

it neither goes to the first if statement, and gives error “AttributeError(f”‘{type(self).name}’ object has no attribute ‘{name}’“) AttributeError: ‘MeanAggregation’ object has no attribute ‘bias’”

The default PyTorch conv class uses Kaiming uniform for weights and a standard uniform distribution for biases.

I’m not sure for SAGEConv, but for regular PyTorch layers, you can just define a custom conv class, and reference the parent accordingly, then redefine the reset_parameters() function. Or just modify your code to something like this:

def initialize_weights(self):
    for m in self.modules():
        if isinstance(m,SAGEConv):
            nn.init.xavier_normal_(m.weight.data)

Note the .data.

when I tried using m.weight.data, I still get this error,
‘SAGEConv’ object has no attribute ‘weight’
do you have any idea ?

Had a look at the SAGEConv module docs and it appears they just create a separate linear class, also defined within that library:


So calling on the SAGEConv class won’t target the weights. Instead, you may need to change SAGEConv to Linear. But given it’s not a torch.nn.Linear, that might not work, too.

They also have a specific reset_parameters method:

Digging further, we can find the specific reset_weight definition:

And it appears that takes an initialization argument.

However, I do not see Xavier listed as an initialization option.

Perhaps you could check on the Slack linked on their docs homepage:
https://pytorch-geometric.readthedocs.io/en/latest/index.html

Seems to me the issue is not related to PyTorch.

I tried to print out the weights and I found that it is initialised by default with uniform Kaiming, and the weights are in conv1.lin_l.weight and conv1.lin_r.weight. for each SAGEconv layer. Reaching both of these weights, I could initialise them using Xavier.
thanks.

1 Like