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 turns numerical inputs into a decision. Inspired by biological neurons, it multiplies each input by a weight, adds the results and a bias, then applies an activation function. Weights control how sensitive the output is to each input; the bias shifts the decision point. section 5.1.3 section 5.1.5

A step activation gives a binary result: output 1 when the weighted sum exceeds a threshold, and 0 otherwise. A sigmoid activation instead gives a continuous value and makes the calculation resemble logistic regression. The weighted sum alone is linear; the activation adds a nonlinear step. section 5.1.4 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

Follow a Python forward pass using bag-of-words counts of 1, 2, 3, and 4. The untrained weights and bias produce a weighted sum of −0.76; a zero-threshold step would output 0. section 5.1.5

For language inputs, a tokenizer can turn text into word counts or TF-IDF features. The perceptron assigns a weight to each feature. Training seeks weights that improve predictions, using a loss function to measure error; a forward pass alone does not train the model. section 5.1.4 section 5.1.5

Predicting a birth-certificate label from a name

A single neuron can use characters in first names to predict recorded sex labels, with about 80% accuracy reported for the example. That label is not a definitive property of a name or a person: names can have multiple labels, and binary categories oversimplify sex. section 5.2.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through weighted sums and activation functions in a Python forward pass, stopping short of implementing training.
Examples
Uses word counts from a short phrase and first names from birth-certificate records as inputs.
What it assumes
Assumes familiarity with regression and text vectorization while explaining weights, activation, and loss in plain language.
What you can do after
Build it from scratch for a forward pass that computes a weighted sum and applies a threshold.
How it connects
Connects perceptrons to biological neurons, linear regression, logistic regression, bag-of-words features, 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.