Welcome to the second lesson in our course "Neural Network Fundamentals: Neurons and Layers"! Now that you've been introduced to the basics of neural networks, we're ready to dive deeper into their core components. In this lesson, we'll focus on the fundamental building block of neural networks: the artificial neuron. You'll learn what a neuron is, how it processes information, and implement a simple artificial neuron in JavaScript
.
By the end of this lesson, you'll understand how an artificial neuron works, both conceptually and mathematically, and you'll have hands-on experience coding one from scratch using math.js
. This practical, code-focused approach will give you a much deeper understanding of neural networks than simply using existing high-level libraries.
Before diving into code, let's recap where the concept of an artificial neuron comes from. Artificial neurons are inspired by the biological neurons in our brains.
In your brain, a biological neuron receives signals from other neurons through structures called dendrites. These signals are processed in the cell body, and if the combined signal is strong enough, the neuron "fires," sending a signal through its axon to other neurons.
An artificial neuron mimics this behavior in a simplified way:
- Inputs represent the signals received from other neurons (like dendrites);
- Weights represent the strength of each connection;
- Bias represents the neuron's tendency to fire regardless of inputs;
- Activation function (which we'll cover in future lessons) mimics the "firing" behavior.
This simple model, when combined with many other neurons, forms the basis of neural networks that can learn complex patterns and make predictions. The beauty of this approach is that even though individual neurons perform very simple operations, their collective behavior can solve incredibly complex problems — from recognizing faces to translating languages.
Let's formalize the mathematical model of an artificial neuron. A neuron takes multiple inputs, multiplies each by a corresponding weight, sums these weighted inputs, adds a bias, and produces an output.
Mathematically, for a neuron with inputs (), we calculate the output as:
Where:
- are the input values
- are the weights for each input
- is the bias
This calculation is also known as a weighted sum plus bias. We can express this more concisely using vector notation:
Where represents the dot product of the weight vector and the input vector. This compact notation highlights why tools like math.js
are so useful — they're optimized for these vector operations that are central to neural network calculations.
Think of the weights as importance factors — they determine how much attention the neuron pays to each input. The bias acts like a threshold adjustment that makes it easier or harder for the neuron to produce a high output regardless of the inputs.
Let's implement an artificial neuron in JavaScript using the math.js
library. In the CodeSignal IDE, math.js
is already installed and available for use, so you do not need to install it manually. If you're working locally, you can install it with the following command:
Now, let's define a Neuron
class that can process a specified number of input features. The neuron will initialize its weights randomly (small values) and set its bias to zero:
When you create a new neuron, you specify how many inputs it will accept. The constructor initializes:
- weights: An array of small random values, one for each input
- bias: A single value, starting at 0.0
Random initialization of weights is important to ensure that each neuron starts with a unique configuration, which is crucial for learning.
The forward pass is the process of calculating the neuron's output for a given set of inputs. This is done by computing the dot product of the input array and the weights, then adding the bias:
This method checks that the input array matches the expected size, computes the weighted sum using math.dot()
, and adds the bias to produce the output.
Let's test the neuron by creating an instance with 3 input features and passing in two different sample input arrays:
When you run this code, you'll see the neuron's initialization details, the sample inputs, and the corresponding outputs. This demonstrates how a single artificial neuron processes different input data and produces outputs based on its weights and bias.
Congratulations! You've just implemented your first artificial neuron from scratch using JavaScript and math.js. You've learned how neurons are inspired by biology, understood their mathematical representation, and coded a working implementation that can process inputs and produce outputs. This seemingly simple weighted sum operation forms the foundation for even the most sophisticated neural networks used in modern AI systems.
In the upcoming practice exercises, you'll get hands-on experience working with your neuron implementation. You'll try out different input values, weights, and biases to see how they affect the neuron's output, helping you build intuition about how neural networks learn from data. This practical understanding will prepare you for the next lessons, where you'll add activation functions and combine multiple neurons into layers to build more powerful networks.
