Access model weights #5945
-
I wanted to know if the following way is the right method to access the model weights and possibly modify them ? from torch_geometric.nn import GCNConv
import torch
from torch.nn import Linear
import torch.nn.functional as F
from tqdm import tqdm
from torch import linalg as LA
class GCN(torch.nn.Module):
def __init__(self, hidden_channels):
super().__init__()
self.conv1 = GCNConv(dataset.num_features, hidden_channels,bias=False)
self.conv2 = GCNConv(hidden_channels, dataset.num_classes,bias=False)
def forward(self, x, edge_index):
x = self.conv1(x, edge_index)
x = x.relu()
x = F.dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
return x
model = GCN(hidden_channels=2)
with torch.no_grad():
for param in model.parameters():
print(param)
print("===========================")
with torch.no_grad():
for param in model.parameters():
param = param/(LA.norm(param,2))
print("AFter")
print(param) Here I am basically trying to modify the initial weights by dividing them with their norm. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Answered by
EdisonLeeeee
Nov 10, 2022
Replies: 1 comment 5 replies
-
You need to use "inplaced" operation instead: param.div_(LA.norm(param,2)) |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
AdarshMJ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to use "inplaced" operation instead: