Perceptrons are the building blocks of artificial neural networks. They are the simplest form of a neural network, consisting of a single artificial neuron or node. A perceptron takes multiple inputs, each multiplied by a corresponding weight, and passes the weighted sum through an activation function. The output of the perceptron is a binary value (0 or 1) based on whether the summed value exceeds a certain threshold.
Example code for a perceptron in Python:
import numpy as np
class Perceptron:
def __init__(self, num_inputs, learning_rate=0.1):
self.weights = np.zeros(num_inputs)
self.bias = 0
self.learning_rate = learning_rate
def predict(self, inputs):
weighted_sum = np.dot(inputs, self.weights) + self.bias
return 1 if weighted_sum > 0 else 0
def train(self, training_inputs, labels, num_epochs):
for _ in range(num_epochs):
for inputs, label in zip(training_inputs, labels):
prediction = self.predict(inputs)
update = self.learning_rate * (label - prediction)
self.weights += update * inputs
self.bias += update