How can I get the in and out edges weights for each neuron in a neural network?

Penguin

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

Shai

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can a genetic algorithm optimize a neural network's weights without knowing the search volume?

How can I apply multithreading to the backpropagation neural network training?

Accessing neural network weights and neuron activations

to show the weights of edges in a projected network

How can I train neural network to play the 2048 game?

How to get an output dimension for each layer of the Neural Network in Pytorch?

How can I update the parameters of a neural network in PyTorch?

How does a Neural Network calculate the sum of the weights?

How can I use generalized regression neural network in python?

How can I save the weights and biases in my neural network after training it using JavaScript?

How can I add bias using pytorch to a neural network?

How can I load CSV data for a PyTorch neural network?

How can I save all the details of a Neural Network model?

Weights in Neural Network

How do neural network models learn different weights for each of the neuron in a single layer?

How to define neural network neuron-by-neuron?

Simple neural network - how to store weights?

update of weights in a neural network

How to apply weights in neural network?

Single Neuron Neural Network - Types of Questions?

How can I use dlib for a neural network regression?

Python/Pybrain: How can I fix weights of a neural network during training?

How can I build a recurrent neural network to deal with time series to get a single continuous value?

Updating weights in a neural network

How can I start building Neural Deep network using MATLAB

Can't init the weights of my neural network PyTorch

Should I transpose features or weights in Neural network?

How can I build a neural network with 2 independent sets of weights and 2 losses?

how do I optmize the weights of the input layer using backward for this simple neural network in pytorch when .grad is None

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive