liveBook · Manning

Perceptron

As explained in Natural Language Processing in Action, Second Edition

A perceptron is a single-layer computational model that computes a weighted sum of inputs and applies a thresholding activation to make a decision.

A perceptron is a small decision-making model inspired by a biological neuron. Each numerical input has a weight that controls its influence. The perceptron adds the weighted inputs, then applies an activation function to produce an output. For text, the inputs can be word counts or TF-IDF values rather than signals from physical sensors. section 5.1.3 section 5.1.4

The calculation is a weighted sum: z = w₀ + w₁x₁ + … + wₙxₙ. Here w₀ is the intercept, and each other weight changes the model’s sensitivity to one feature. A step activation returns 1 when z exceeds a threshold and 0 otherwise. A logistic activation instead gives a continuous output through a soft threshold. This makes the calculation closely related to logistic regression. section 5.1.4 section 5.1.5

Count words, then make a decision

The phrase “green egg egg ham ham ham spam spam spam spam” becomes a bag-of-words vector with counts 1, 2, 3, and 4. With an intercept of 0.07 and weights 0.12, −0.16, 0.03, and −0.18, the weighted sum is −0.76. A step activation with threshold zero therefore returns 0. These weights are random: the result demonstrates the calculation, not a trained prediction. section 5.1.5

>>> from collections import Counter

>>> np.random.seed(451)
>>> tokens = "green egg egg ham ham ham spam spam spam spam".split()
>>> bow = Counter(tokens)
>>> x = pd.Series(bow)
>>> x
green    1
egg      2
ham      3
spam     4

>>> x1, x2, x3, x4 = x
>>> x1, x2, x3, x4
(1, 2, 3, 4)

>>> w0 = np.round(.1 * np.random.randn(), 2)
>>> w0
0.07
>>> w1, w2, w3, w4 = (.1 * np.random.randn(len(x))).round(2)
>>> w1, w2, w3, w4
(0.12, -0.16, 0.03, -0.18)

>>> x = np.array([1, x1, x2, x3, x4])   #1
>>> w = np.array([w0, w1, w2, w3, w4])    #2
>>> y = np.sum(w * x)       #3
>>> y
-0.76

The Python listing constructs the word counts, includes the intercept as an input of 1, and computes the weighted sum with NumPy. section 5.1.5

To learn useful weights, a model needs examples and a loss function that measures prediction error; the calculation above shows only its feed-forward path. One application predicts a birth-certificate sex label from the characters in a first name. The reported single-neuron accuracy is about 80%, not certainty about any individual: names can occur with more than one label, and binary birth-certificate categories oversimplify sex. Keep names in the test set out of training when evaluating such a model. section 5.1.5 section 5.2.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through weights, an intercept, and step or logistic activation in Python, but leaves weight training at a conceptual level.
Examples
Calculates a weighted sum from word counts and applies perceptrons to predicting birth-certificate sex labels from first names.
What it assumes
Assumes familiarity with Python and earlier text-vectorization and regression examples while spelling out the neuron's arithmetic.
What you can do after
Build it from scratch as a Python feed-forward neuron that weights inputs and applies a threshold.
How it connects
Connects biological neurons and logistic regression to bag-of-words, TF-IDF, activation functions, and loss functions.

Also explained in

Compare how 5 books explain perceptron →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.