concept neuron in category python

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python.
Before explaining the metaphor, I’ll preface this discussion by reminding you that I’m not a neurologist. The rough idea is that the brain is a big clump of interconnected cells called neurons and, when you think certain thoughts, what’s actually happening is electrical activity at specific neurons. You can see this electrical activity in the right kind of brain scan where various parts of the brain light up (figure 16.1).
Figure 16.1: Different kinds of brain activity cause different neurons to electrically activate, showing bright areas in a brain scan.
![]()
As opposed to the billions of neurons in the human brain, the neural networks we build in Python have only a few dozen neurons, and the degree to which a specific neuron is turned on is represented by a single number called its activation. When a neuron activates in the brain or in our artificial neural network, it can cause adjacent, connected neurons to turn on as well. This allows one idea to lead to another, which we can loosely see as creative thinking.
Mathematically, the activation of a neuron in our neural network is a function of the numerical activation values of neurons it is connected to. If a neuron connects to four others with the activation values a1, a2, a3, and a4, then its activation will be some mathematical function applied to those four values, say f(a1, a2, a3, a4).
Figure 16.2 shows a schematic diagram with all of the neurons drawn as circles. I’ve suggestively shaded the neurons differently to indicate that they have different levels of activation, kind of like the brighter or darker areas of a brain scan.

This is an excerpt from Manning's book Classic Computer Science Problems in Python.
The human brain is the most incredible computational device in existence. It cannot crunch numbers as fast as a microprocessor, but its ability to adapt to new situations, learn new skills, and be creative is unsurpassed by any known machine. Since the dawn of computers, scientists have been interested in modeling the brain’s machinery. Each nerve cell in the brain is known as a neuron. Neurons in the brain are networked to one another via connections known as synapses. Electricity passes through synapses to power these networks of neurons—also known as neural networks.
Let’s start with a neuron. An individual neuron will store many pieces of state, including its weights, its delta, its learning rate, a cache of its last output, and its activation function, along with the derivative of that activation function. Some of these elements could be more efficiently stored up a level (in the future Layer class), but they are included in the following Neuron class for illustrative purposes.
Listing 7.3. neuron.py
from typing import List, Callable from util import dot_product class Neuron: def __init__(self, weights: List[float], learning_rate: float, activation_function: Callable[[float], float], derivative_activation_ function: Callable[[float], float]) -> None: self.weights: List[float] = weights self.activation_function: Callable[[float], float] = activation_ function self.derivative_activation_function: Callable[[float], float] = derivative_activation_function self.learning_rate: float = learning_rate self.output_cache: float = 0.0 self.delta: float = 0.0 def output(self, inputs: List[float]) -> float: self.output_cache = dot_product(inputs, self.weights) return self.activation_function(self.output_cache) !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":0,\"ch\":12},{\"line\":0,\"ch\":18}],[{\"line\":1,\"ch\":10},{\"line\":1,\"ch\":16}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":0,\"ch\":12},{\"line\":0,\"ch\":18}],[{\"line\":1,\"ch\":10},{\"line\":1,\"ch\":16}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":5}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":3}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":6,\"ch\":40},{\"line\":6,\"ch\":44}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":3}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":18,\"ch\":4},{\"line\":18,\"ch\":10}]]"} !@%STYLE%@!Most of these parameters are initialized in the __init__() method. Because delta and output_cache are not known when a Neuron is first created, they are just initialized to 0. All of the neuron’s variables are mutable. In the life of the neuron (as we will be using it) their values may never change, but there is still a reason to make them mutable: flexibility. If this Neuron class were to be used with other types of neural networks, it is possible that some of these values might change on the fly. There are neural networks that change the learning rate as the solution approaches and that automatically try different activation functions. Here, we are trying to keep the Neuron class maximally flexible for other neural network applications.
The only other method, other than __init__(), is output(). output() takes the input signals (inputs) coming to the neuron and applies the formula discussed earlier in the chapter (see figure 7.2). The input signals are combined with the weights via a dot product, and this is cached in output_cache. Recall from the section on backpropagation that this value, obtained before the activation function is applied, is used to calculate delta. Finally, before the signal is sent on to the next layer (by being returned from output()), the activation function is applied to it.