Friday, December 20, 2024

Training a Perceptron

 Training a perceptron involves adjusting the weights of the model to minimize the error in predictions. The perceptron is a simple neural network with a single layer, and it performs binary classification. Here's how you can train a perceptron step by step:

1. Understanding the Perceptron Model

The perceptron model consists of:

  • Input layer: Each input feature is associated with a weight.
  • Activation function: A step function, which produces an output of 1 if the weighted sum of inputs is greater than or equal to a threshold (usually 0), otherwise 0.
  • Output: The model produces a binary output (0 or 1).

2. Initialize Weights and Bias

  • Start with random weights, often initialized to small random values close to zero.
  • Initialize the bias term (often initialized to 0 or small values).

3. The Perceptron Algorithm

The perceptron algorithm updates weights based on the error between the predicted and actual outputs. The key steps are:

  1. Forward Pass: Compute the weighted sum of inputs and pass it through an activation function to produce an output.

    y=sign(i=1nwixi+b)y = \text{sign} \left( \sum_{i=1}^n w_i x_i + b \right)

    Where:

    • wiw_i are the weights,
    • xix_i are the input features,
    • bb is the bias term,
    • sign(z)\text{sign}(z) is a step function: returns 1 if z0z \geq 0, else returns 0.
  2. Update the Weights: If the prediction is wrong, adjust the weights. The weight update rule is:

    wi=wi+Δwiw_i = w_i + \Delta w_i

    Where the change in weight Δwi\Delta w_i is:

    Δwi=η(ytrueypred)xi\Delta w_i = \eta (y_{\text{true}} - y_{\text{pred}}) x_i
    • η\eta is the learning rate (a small constant),
    • ytruey_{\text{true}} is the actual label,
    • ypredy_{\text{pred}} is the predicted label.
  3. Bias Update: Similarly, update the bias term:

    b=b+η(ytrueypred)b = b + \eta (y_{\text{true}} - y_{\text{pred}})
  4. Repeat: This process is repeated for each training example and iterated for multiple epochs (complete passes through the dataset).

4. Stopping Criteria

  • The training can stop after a fixed number of epochs or when the error stops improving (convergence).
  • For each epoch, the perceptron checks whether the weights and bias need to be adjusted.

5. Training Steps in Pseudocode

Here’s a pseudocode for training a perceptron:

def train_perceptron(X, y, epochs=1000, learning_rate=0.1):
    # Initialize weights and bias
    weights = np.zeros(X.shape[1])  # Initialize weights to zero
    bias = 0

    # Loop through the epochs
    for epoch in range(epochs):
        for i in range(len(X)):
            # Calculate the weighted sum of inputs
            linear_output = np.dot(X[i], weights) + bias

            # Apply the step activation function
            y_pred = 1 if linear_output >= 0 else 0

            # Update the weights and bias if there's an error
            if y_pred != y[i]:
                weights += learning_rate * (y[i] - y_pred) * X[i]
                bias += learning_rate * (y[i] - y_pred)
    
    return weights, bias

6. Testing the Perceptron

After training, you can test the perceptron by passing new inputs through the trained model and checking if it correctly classifies them.

def predict(X, weights, bias):
    linear_output = np.dot(X, weights) + bias
    return 1 if linear_output >= 0 else 0

Example:

Let's say you are trying to classify a set of points in 2D space (with two features) based on a linear decision boundary (e.g., "AND" logic).

  • Inputs: X=[00011011]X = \begin{bmatrix} 0 & 0 \\ 0 & 1 \\ 1 & 0 \\ 1 & 1 \end{bmatrix}
  • Targets: y=[0001]y = \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix} (AND function)

You would train the perceptron with these inputs and observe how the weights evolve to correctly classify the points.

Conclusion

Training a perceptron involves:

  • Initializing weights and bias,
  • Iterating over the dataset,
  • Adjusting weights and bias based on errors,
  • Repeating until convergence.

The perceptron is a simple and effective model for binary classification when the data is linearly separable.

No comments:

Post a Comment

How will AI transform your life in the next 5 years?

 AI is already transforming how we live and work, and over the next 5 years, this transformation is expected to accelerate in several key ar...