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 how to implement a simple artificial neuron in R.
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 base R functions. This practical, code-focused approach will give you a much deeper understanding of neural networks than simply using existing 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:
Now that you understand the concept, let's start implementing an artificial neuron in R. Unlike object-oriented languages like Python, R typically uses a functional approach. We'll create a neuron using R's list structure to store its state:
This function creates a neuron structure with the following components:
weights: A random vector with one value per input, scaled to be small (0.1times a random number between0and1).bias: A single value, starting at0.0.n_inputs: The number of input features the neuron expects.
We use small random values for weights rather than zeros because if all weights were identical, all neurons in a network would learn the same features during training. This randomness gives each a unique starting point, which is crucial for the learning process later on.
The forward pass refers to the process of calculating the neuron's output given a set of inputs. This is where we implement the mathematical model we discussed earlier:
This forward function is the heart of our neuron. It takes a neuron structure and an array of inputs, then performs these steps:
- First, it validates that the input vector has the correct number of elements.
- Then, it calculates the weighted sum by multiplying each input by its corresponding weight and summing the results.
- Finally, it adds the bias to get the final output value.
R's vectorized operations make this calculation efficient without needing external libraries. The element-wise multiplication (inputs * neuron$weights) followed by sum() is equivalent to a dot product operation.
Now that you've implemented a neuron, let's test it with some sample inputs to see how it works in practice:
When you run this code, you'll see:
- The neuron's initialization information, including the random weights it generated.
- The first input values you're providing:
1.0 2.0 3.0. - The resulting output calculation (which will vary depending on the random weights).
- A second example with different inputs:
0.5 -1.0 2.0. - The output for this second example.
This simple test demonstrates how a single artificial neuron processes input data. In a real application, these inputs might represent features like temperature, pressure, or pixel values in an image, and the neuron's job would be to learn which features are important for a specific task.
Congratulations! You've just implemented your first artificial neuron from scratch using R. 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 explore how changing weights and biases affect outputs and gain intuition about how neural networks begin to learn patterns in data. This practical understanding will prepare you for our next lessons, where we'll add activation functions and combine multiple neurons into layers to build more powerful networks.
