Reputation: 2441
Say I have the following network
import torch
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.fc1 = nn.Linear(1, 2)
self.fc2 = nn.Linear(2, 3)
self.fc3 = nn.Linear(3, 1)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Model()
I know that I can access the weights (i.e edges) at each layer:
net.fc1.weight
However, I'm trying to create a function that randomly selects a neuron from the entire network and outputs it's in-connections (i.e the edges/weights that are attached to it from the previous layer) and its out-connections (i.e the edges/weights that are going out of it to the next layer).
pseudocode:
def get_neuron_in_out_edges(list_of_neurons):
shuffled_list_of_neurons = shuffle(list_of_neurons)
in_connections_list = []
out_connections_list = []
for neuron in shuffled_list_of_neurons:
in_connections = get_in_connections(neuron) # a list of connections
out_connections = get_out_connections(neuron) # a list of connections
in_connections_list.append([neuron,in_connections])
out_connections_list.append([neuron,out_connections])
return in_connections_list, out_connections_list
The idea is that I can then access these values and say if they're smaller than 10
, change them to 10
in the network. This is for a networks class where we're working on plotting different networks so this doesn't have to make much sense from a machine learning perspective
Upvotes: 1
Views: 53
Reputation: 114826
Let's ignore biases for this discussion.
A linear layer computes the output y
given weights w
and inputs x
as:
y_i = sum_j w_ij x_j
So, for neuron i
all the incoming edges are the weights w_ij
- that is the i
-th row of the weight matrix W
.
Similarly, for input neuron j
it affects all y_i
according to the j
-th column of the weight matrix W
.
Upvotes: 2