liveBook · Manning

Neuron

As explained in Natural Language Processing in Action, Second Edition

An artificial neuron is a parameterized computational unit that combines inputs to produce an output, typically through an activation function.

An artificial neuron turns numerical inputs into an output. It multiplies each input feature by a weight, combines the results, and may pass the sum through an activation function. A network connects neurons to make predictions from data such as words or names. section 5.1.5 section 5.0

The weights express how strongly each input affects the result. The biological analogy is a neuron whose inputs have different sensitivities, but an artificial neuron is a mathematical abstraction. A bias adds a constant term to the weighted sum; an activation function then determines the output. For example, a threshold can turn the sum into a binary decision, while a logistic activation produces a continuous score. section 5.1.3 section 5.1.5 section 5.2

A neuron for word counts

Represent a short passage by counts of four words: 1, 2, 3, and 4. With weights 0.12, -0.16, 0.03, and -0.18, and a bias of 0.07, the weighted sum is -0.76. A threshold at zero turns this into a false decision. Changing a weight changes the influence of its corresponding word count. 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

Inspect the Python calculation from word counts through the weighted sum. section 5.1.5

A single neuron with a logistic, or sigmoid, activation is mathematically equivalent to logistic regression, though the models can be trained differently. To train a neuron, choose a loss function that measures prediction error, then use backpropagation and an optimizer to adjust its parameters. Binary cross-entropy is a common loss for binary classification. section 5.2 section 5.2.5

Predicting a birth-record label from a name

A single PyTorch neuron can take TF-IDF features for character sequences in first names and output a score for a recorded sex label. The illustrated model has 3,663 inputs and one output. Its forward method applies a linear layer followed by sigmoid; training uses binary cross-entropy and stochastic gradient descent. section 5.2.5

>>> import torch
>>> class LogisticRegressionNN(torch.nn.Module):

...    def __init__(self, num_features, num_outputs=1):
...         super().__init__()
...         self.linear = torch.nn.Linear(num_features, num_outputs)

...    def forward(self, X):
...        return torch.sigmoid(self.linear(X))

>>> model = LogisticRegressionNN(num_features=vecs.shape[1], num_outputs=1)
>>> model
LogisticRegressionNN(
  (linear): Linear(in_features=3663, out_features=1, bias=True)
)

See the single-layer PyTorch model and its forward computation. section 5.2.5

A name does not have one correct binary sex label. Birth-record categories are assigned and can oversimplify biological diversity, so a model’s score should not be treated as a definitive judgment about a person. Keep names in the test set separate from those used for training, and be alert to brittle behavior and adversarial attacks in NLP systems. section 5.2.2 section 5.1.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through weighted sums, activation and a trainable single-neuron implementation.
Examples
Uses word counts and first-name character features to demonstrate neuron predictions.
What you can do after
Apply a single logistic neuron via PyTorch to a binary classification task.
How it connects
Connects neurons to perceptrons, logistic regression, activation functions and backpropagation.

Also explained in

Compare how 6 books explain neuron →

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.