A simple perceptron demo¶
1 2 3 | import torch import numpy as np import matplotlib.pyplot as plt |
Generate two-dimensional binary data with a binary outcome determined according to the logical operators or, and, or xor. Add a bit of noise to the input data to generate multiple observations for each problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | def generate_perceptron_data(problem='or', add_noise=True, noise_level=0.1, nreplicates=4): if problem == 'or': data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) labels = np.array([0, 1, 1, 1]) elif problem == 'and': data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) labels = np.array([0, 0, 0, 1]) elif problem == 'xor': data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) labels = np.array([0, 1, 1, 0]) else: raise ValueError('Unknown problem type') if add_noise: labels = np.tile(labels, nreplicates) data = np.tile(data, (nreplicates, 1)) noise = np.random.randn(data.shape[0], data.shape[1]) * noise_level data = data + noise return data, labels def plot_perceptron_data(data, labels): plt.scatter(data[:, 0], data[:, 1], c=labels) plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.title('Perceptron Data') |
1 2 | data, labels = generate_perceptron_data(problem='or') plot_perceptron_data(data, labels) |
Fit a perceptron¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | def fit_perceptron(data, labels, num_epochs=20, learning_rate=0.01): # add bias colunm to data data = np.concatenate([data, np.ones((data.shape[0], 1))], axis=1) # Initialize weights and bias weights = np.random.randn(data.shape[1]) * 0.05 accuracy = [] for epoch in range(num_epochs): for i in range(data.shape[0]): x = data[i] y = labels[i] y_pred = np.dot(x, weights) # Apply step function if y_pred > 0: y_pred = 1 else: y_pred = 0 weights += learning_rate * (y - y_pred) * x accuracy.append(np.mean((np.dot(data, weights) > 0) == labels)) return accuracy def plot_accuracy(acc, data, labels): plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plot_perceptron_data(data, labels) plt.subplot(1, 2, 2) plt.plot(acc) plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.title('Perceptron Learning') plt.ylim([0, 1.1]) |
Generate output using or operator.
1 2 3 | data, labels = generate_perceptron_data(problem='or') acc = fit_perceptron(data, labels) plot_accuracy(acc, data, labels) |
Generate outputs using and operator.
1 2 3 | data, labels = generate_perceptron_data(problem='and') acc = fit_perceptron(data, labels) plot_accuracy(acc, data, labels) |
Generate outputs using xor (exclusive or) operator. This is a nonlinear function of the input, which is not solvable using a single-layer perceptron.
1 2 3 | data, labels = generate_perceptron_data(problem='xor') acc = fit_perceptron(data, labels) plot_accuracy(acc, data, labels) |
1 |