pytorch
  1. pytorch-perceptron-introduction

Perceptron Introduction - ( Perceptron in PyTorch )

Heading h2

Syntax

import torch

class Perceptron(torch.nn.Module):
    def __init__(self):
        super(Perceptron, self).__init__()
        self.fc = torch.nn.Linear(2, 1)

    def forward(self, x):
        out = self.fc(x)
        return out

Example

import torch

class Perceptron(torch.nn.Module):
    def __init__(self):
        super(Perceptron, self).__init__()
        self.fc = torch.nn.Linear(2, 1)

    def forward(self, x):
        out = self.fc(x)
        return out

model = Perceptron()
x = torch.tensor([[0., 0.], [0., 1.], [1., 0.], [1., 1.]])
y = torch.tensor([[0.], [0.], [0.], [1.]])

# Define loss function and optimizer
criterion = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

# Training loop
for epoch in range(1000):
    # Forward pass
    y_pred = model(x)
    loss = criterion(y_pred, y)

    # Backward pass and updates
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if epoch % 100 == 0:
        print(f'Epoch [{epoch+1}/1000], Loss: {loss.item():.4f}')

Output

Epoch [1/1000], Loss: 0.7439
Epoch [101/1000], Loss: 0.4643
Epoch [201/1000], Loss: 0.3235
Epoch [301/1000], Loss: 0.2409
Epoch [401/1000], Loss: 0.1904
Epoch [501/1000], Loss: 0.1571
Epoch [601/1000], Loss: 0.1336
Epoch [701/1000], Loss: 0.1164
Epoch [801/1000], Loss: 0.1030
Epoch [901/1000], Loss: 0.0922

Explanation

The perceptron is one of the simplest types of neural networks. It is a single-layer neural network that takes in multiple inputs and produces a binary output. It is a linear classifier that is trained to correctly classify data by adjusting weights and biases.

In the PyTorch implementation above, we define a Perceptron class that inherits from the parent Module class in PyTorch. We initialize the perceptron weights and bias using torch.nn.Linear function. We then define the forward() method that takes in input and passes it through the linear function to produce the output.

In the example, we define a Perceptron model and set up input and output tensors. We use the Binary Cross-entropy loss function and stochastic gradient descent optimizer to train the model. We train the model for 1000 epochs and print the loss for every 100th epoch.

Use

Perceptrons can be used for binary classification problems and are especially useful for linearly separable datasets. They can be used in tasks such as spam filtering, credit fraud detection, and medical diagnosis.

Important Points

  • The perceptron is a single-layer neural network that takes in multiple inputs and produces a binary output
  • It is a linear classifier that adjusts weights and biases to correctly classify data
  • The PyTorch implementation involves defining the Perceptron class and using the Binary Cross-entropy loss function and stochastic gradient descent optimizer to train the model
  • Perceptrons can be used for binary classification problems, especially those that are linearly separable

Summary

Perceptrons are one of the simplest types of neural networks and are useful for binary classification problems. PyTorch provides an easy implementation for creating perceptron models using the Perceptron class and defines the forward() method to pass input through the linear function.

Published on: