Skip to content
  • About
  • Courses
  • ResearchExpand
    • Research Publications
    • Books
    • Patents
  • Workshop/Conferences
  • ToolsExpand
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
Download CV
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

    Explore Generative AI with the Gemini API in Vertex AI

    Read More Explore Generative AI with the Gemini API in Vertex AIContinue

  • Latest

    Inspect Rich Documents with Gemini Multimodality and Multimodal RAG

    Read More Inspect Rich Documents with Gemini Multimodality and Multimodal RAGContinue

  • Latest

    ๐ŸŽ“ Why Original Work Matters in Your Final Year Project (And How It Can Shape Your Career)

    In engineering colleges across the country, final year projects are often treated as just another academic task. But what many students fail to realize is…

    Read More ๐ŸŽ“ Why Original Work Matters in Your Final Year Project (And How It Can Shape Your Career)Continue

  • Latest

    ๐ŸŽ“ How to Choose Your Final Year Project: A Practical Guide for BTech Students

    Choosing the right final year project is one of the most important decisions of your engineering journey. Itโ€™s more than just a submission โ€” itโ€™s…

    Read More ๐ŸŽ“ How to Choose Your Final Year Project: A Practical Guide for BTech StudentsContinue

  • Latest

    ๐Ÿง  MCP Server: Model Context Prototyping with Gemini + MySQL + FastAPI

    GitHub: https://github.com/nishantmunjal2003/mcp-server-gemini ๐Ÿ“Œ Project Overview MCP Server is a lightweight, extendable API server that: โš™๏ธ Features ๐Ÿ“ Folder Structure bashCopyEditmcp-server/ โ”‚ โ”œโ”€โ”€ app.py # Main…

    Read More ๐Ÿง  MCP Server: Model Context Prototyping with Gemini + MySQL + FastAPIContinue

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

© 2025 - All Rights Reserved

  • About
  • Courses
  • Research
    • Research Publications
    • Books
    • Patents
  • 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.