I have the following simple fully-connected neural network:
class Neural_net(nn.Module):
def __init__(self):
super(Neural_net, self).__init__()
self.fc1 = nn.Linear(2, 2)
self.fc2 = nn.Linear(2, 1)
self.fc_out = nn.Linear(1, 1)
def forward(self, x,train = True):
x = torch.tanh(self.fc1(x))
x = torch.tanh(self.fc2(x))
x = self.fc_out(x)
return x
net = Neural_net()
How can I loop through all the parameters of the network and check if for example they are greater than a certain value? I am using pytorch and if I do:
for n,p in net.named_parameters():
if p > value:
...
I get an error since p is not a single number, but rather a tensor of either weights or biases for each layer.
My goal is to check if a criterion is satisfied for each of the parameters and flag them e.g. with 1 if it is or 0 if it is not, storing it in a dictionary with the same structure as net.parameters(). Yet, I am having trouble figuring out how to loop through them.
I thought about creating a parameter vector:
param_vec = torch.cat([p.view(-1) for p in net.parameters()])
and then accessing the parameter values and checking them would be easy,but then I can't think of a way to go back to the dictionary form to flag them.
Thank you for any help!