The perceptron method is one of the earliest and simplest artificial neural network supervised learning methods. It involves the single function H(w · i + b) where H is the Heaviside step function and i is the input. w and b are referred to as the weights and the bias. For every input output pair (i, o), a scaled version of i is added to w, and, the scale factor of i is added to b. The scale factor for every i is r(o – H(w · i + b)) for some small r referred to as the learning rate.

Here is sample Python perceptron method code:

#!/usr/bin/env python3

Implements the perceptron method.

        ./perceptron <data file> <data split> <learning rate> <number of epochs>

Data files must be space delimited with one input output pair per line.

initialization steps:
        Input output pairs are shuffled.
        Inputs             are min max normalized.
        Weights            are set to random values.

Requires NumPy.

import numpy
import sys

def minmax(data):
        Finds the min max normalizations of data.

        return (data - numpy.min(data)) / (numpy.max(data) - numpy.min(data))

def init_data(data_file, data_split):
        Creates the training and testing data.

        data         = numpy.loadtxt(data_file)
        data[:, :-1] = minmax(data[:, :-1])
        ones         = numpy.ones(data.shape[0])[None].T
        data         = numpy.hstack((data[:, :-1], ones, data[:, -1][None].T))
        data_split   = int((data_split / 100) * data.shape[0])

        return data[:data_split, :], data[data_split:, :]

def accuracy(data, weights):
        Calculates the accuracies of models.

        model_ = model(data[:, :-1], weights)

        return 100 * (model_ == data[:, -1]).astype(int).mean()

def model(inputs, weights):
        Finds the model outputs.

        return (numpy.matmul(inputs, weights) > 0).astype(int)

def learn(data, learn_rate, n_epochs):
        Learns the weights from data.

        weights = numpy.random.rand(data.shape[1] - 1) / (data.shape[1] - 1)
        for i in range(n_epochs):
                for e in data:
                        model_   = model(e[:-1], weights)
                        weights += learn_rate * (e[-1] - model_) * e[:-1]

        return weights

train_data, test_data = init_data(sys.argv[1], float(sys.argv[2]))
weights               = learn(train_data, float(sys.argv[3]), int(sys.argv[4]))
print(f"weights and bias:       {weights}")
print(f"training data accuracy: {accuracy(train_data, weights):.2f}%")
print(f"testing  data accuracy: {accuracy(test_data,  weights):.2f}%")

Here are sample results for a subset of the popular MNIST (Modified National Institute Of Standards And Technology) dataset available from many sources such as Kaggle. Outputs denote whether the inputs correspond to the number eight or not:

% ./perceptron MNIST_subset_dataset.csv 80 0.000001 100
weights and bias:       [ 1.26322608e-03  1.08497202e-03  1.03465701e-03  6.20197534e-05
  8.92840895e-04  3.13696893e-04  9.32305752e-04  5.30571491e-04
  9.57601044e-04  9.92650699e-04  4.41735355e-04  9.50010528e-04
  7.11471738e-04  1.26831615e-03  7.15789174e-04  1.59426438e-04


  9.04247841e-04  7.11406621e-04  2.85485411e-04 -3.17756922e-05
  6.38906024e-04  9.42761704e-04  1.01108588e-03  3.51662937e-04
  8.18848025e-04  5.85304004e-04  1.77400185e-05  1.27172550e-03
training data accuracy: 90.04%
testing  data accuracy: 85.89%

Here is a plot of the accuracy versus the number of epochs for a data split of 80 / 20 and a learning rate of 0.000001. Blue denotes the training data accuracy and orange denotes the testing data accuracy:

Leave a Reply

Your email address will not be published.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>