Skip to content
  • About
  • CoursesExpand
    • Problem Solving using C Language
    • Mastering Database Management
    • Linux System Administration
    • Linux and Shell Programming
  • Publications
  • Professional Certificates
  • BooksExpand
    • Books Authored
  • Patents
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

Advance AI PPT

Read More Advance AI PPTContinue

Latest

Prompts for Image Descriptions

Describe the scene using three vivid sensory details — one for sight, one for sound, and one for touch. Summarize the mood of the image…

Read More Prompts for Image DescriptionsContinue

Latest

Dimensionality Reduction

Dimensionality reduction is the process of reducing the number of features (variables) in a dataset while preserving important information. It helps in: ✅ Reducing computational…

Read More Dimensionality ReductionContinue

Artificial Intelligence

Tanh Function in Neural Network

The tanh function, short for hyperbolic tangent function, is another commonly used activation function in neural networks. It maps any real-valued number into a value…

Read More Tanh Function in Neural NetworkContinue

Latest

Why Initialize Weights in Neural Network

Initializing weights and biases is a crucial step in building a neural network. Proper initialization helps ensure that the network converges to a good solution…

Read More Why Initialize Weights in Neural NetworkContinue

Nishant Munjal

Coding Humanity’s Future </>

Facebook Twitter Linkedin YouTube Github Email

Tools

  • SIP Calculator
  • Write with AI
  • SamplePHP
  • Image Converter

Resources

  • Blog
  • Contact
  • Refund and Returns

Legal

  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

© 2025 - All Rights Reserved

  • About
  • Courses
    • Problem Solving using C Language
    • Mastering Database Management
    • Linux System Administration
    • Linux and Shell Programming
  • Publications
  • Professional Certificates
  • Books
    • Books Authored
  • Patents
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.Ok