Skip to content
Nishant Munjal
  • About
  • NMRIL Labs
  • Courses
  • ResearchExpand
    • Research Publications
    • Books
    • Patents
    • Ph.D. Supervised
  • Workshop/Conferences
  • ToolsExpand
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
One Page CV
Nishant Munjal
Artificial Intelligence

Weights in Neural Network

Defining weights in a neural network involves initializing them to appropriate values before training begins. Proper initialization is critical for ensuring efficient and effective training. Here’s a step-by-step guide on how to define weights in a neural network:

1. Understanding Weight Initialization

Weights are the parameters that connect neurons between different layers in the network. Initializing these weights properly helps in:

  • Breaking Symmetry: Ensures that neurons learn different features.
  • Efficient Training: Avoids issues like vanishing or exploding gradients.
  • Convergence: Helps in faster convergence during training.

2. Common Initialization Techniques

  1. Random Initialization:
    • Weights are initialized randomly, usually from a normal or uniform distribution.
  2. Xavier Initialization (also known as Glorot Initialization):
    • Suitable for layers with sigmoid or tanh activation functions.
    • Weights are initialized from a normal distribution with mean 0 and variance 1/n1/n1/n, where nnn is the number of input neurons.
    • Formula: np.random.randn(fan_in, fan_out) * np.sqrt(1 / fan_in)
  3. He Initialization:
    • Suitable for layers with ReLU activation functions.
    • Weights are initialized from a normal distribution with mean 0 and variance 2/n2/n2/n, where nnn is the number of input neurons.
    • Formula: np.random.randn(fan_in, fan_out) * np.sqrt(2 / fan_in)

3. Implementation in Code

Let’s walk through an example of defining weights using Xavier initialization for a simple neural network with one hidden layer.

Step-by-Step Example

1. Import Libraries:

import numpy as np

2. Define Initialization Function:

def initialize_parameters(input_size, hidden_size, output_size):
    # Xavier Initialization for weights
    W1 = np.random.randn(input_size, hidden_size) * np.sqrt(1 / input_size)
    b1 = np.zeros((1, hidden_size))
    W2 = np.random.randn(hidden_size, output_size) * np.sqrt(1 / hidden_size)
    b2 = np.zeros((1, output_size))
    return W1, b1, W2, b2

3. Specify Network Dimensions:

input_size = 3  # Number of input features
hidden_size = 4  # Number of neurons in the hidden layer
output_size = 1  # Number of output neurons

4. Initialize Weights and Biases:

W1, b1, W2, b2 = initialize_parameters(input_size, hidden_size, output_size)

Explanation

  • W1: Weights connecting the input layer to the hidden layer.
    • Shape: (input_size, hidden_size) which is (3, 4).
    • Initialized using Xavier initialization.
  • b1: Biases for the hidden layer.
    • Shape: (1, hidden_size) which is (1, 4).
    • Initialized to zeros.
  • W2: Weights connecting the hidden layer to the output layer.
    • Shape: (hidden_size, output_size) which is (4, 1).
    • Initialized using Xavier initialization.
  • b2: Biases for the output layer.
    • Shape: (1, output_size) which is (1, 1).
    • Initialized to zeros.

Putting It All Together

Here’s a complete code snippet for initializing weights and biases using Xavier initialization:

import numpy as np

def initialize_parameters(input_size, hidden_size, output_size):
    # Xavier Initialization for weights
    W1 = np.random.randn(input_size, hidden_size) * np.sqrt(1 / input_size)
    b1 = np.zeros((1, hidden_size))
    W2 = np.random.randn(hidden_size, output_size) * np.sqrt(1 / hidden_size)
    b2 = np.zeros((1, output_size))
    return W1, b1, W2, b2

# Define the neural network structure
input_size = 3  # Number of input features
hidden_size = 4  # Number of neurons in the hidden layer
output_size = 1  # Number of output neurons

# Initialize parameters
W1, b1, W2, b2 = initialize_parameters(input_size, hidden_size, output_size)

# Print the initialized parameters
print("W1:", W1)
print("b1:", b1)
print("W2:", W2)
print("b2:", b2)

Summary

Defining weights in a neural network involves initializing them to appropriate values. Proper initialization ensures that the network can learn effectively and efficiently. Techniques like Xavier and He initialization help in setting up the weights to avoid issues like vanishing or exploding gradients, thereby facilitating better training and convergence.

Post Tags: #weights

Post navigation

Previous Previous
Understanding Bias in Neural Networks
NextContinue
What are Biases in Neural Networks?
  • Latest

    What is CSP Bypass?

    What is CSP Bypass? CSP (Content Security Policy) is a browser security feature that tries to stop attacks like XSS (Cross-Site Scripting) by controlling what…

    Read More What is CSP Bypass?Continue

  • Cybersecurity

    Cybersecurity Tools

    Cybersecurity Tools 1️⃣ Digital Forensics Tools Tool Name Type Used For Who Uses Autopsy Computer forensics Analyze hard disk, recover deleted files Investigators FTK Imager…

    Read More Cybersecurity ToolsContinue

  • Cybersecurity

    Command Injection Attack

    Command Injection Attack A Command Injection attack happens when a web application takes user input and passes it to the system shell (Linux/Windows command line)…

    Read More Command Injection AttackContinue

  • Cybersecurity

    CSRF in DVWA (for learning/demo)

    CSRF in DVWA (for learning/demo) CSRF: An attack where a logged-in user is tricked into sending unwanted requests to a web application, causing actions to…

    Read More CSRF in DVWA (for learning/demo)Continue

  • Cybersecurity

    XSS = Cross-Site Scripting using DVWA

    XSS = Cross-Site Scripting using DVWA It allows an attacker to inject JavaScript into a web page so that it runs in another user’s browser….

    Read More XSS = Cross-Site Scripting using DVWAContinue

Nishant Munjal

Coding Humanity’s Future </>


Facebook Twitter Linkedin YouTube Github Email

Tools

  • SIP Calculator
  • EMI Calculator
  • Creative QR Code
  • Image Converter

Resources

  • Blog
  • Contact
  • Refund and Returns

Legal

  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

© 2026 Nishant Munjal - All Rights Reserved

  • About
  • NMRIL Labs
  • Courses
  • Research
    • Research Publications
    • Books
    • Patents
    • Ph.D. Supervised
  • Workshop/Conferences
  • Tools
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
Download CV
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.