Artificial Neural Networks: Many Small Decisions Adding Up to a Smart One
Imagine a committee. Each member looks at a few pieces of evidence, forms a quick opinion, and passes it on to the next row of members. Those members combine the opinions they've received and pass theirs on again. By the time it reaches the last person, the group has reached a verdict that none of the individuals could have reached alone.
That's about as close as I can get to describing an artificial neural network without a diagram. Let's build the picture up properly.
The neuron
The basic unit is a neuron, and despite the name it's a very modest piece of maths. It does three things:
- Takes some numbers in.
- Multiplies each by a weight (how much this input matters), adds them up, and adds a bias (a built-in nudge).
- Runs the result through an activation function, which decides how strongly the neuron fires. In symbols: output = activation(w1*x1 + w2*x2 + ... + b).
One neuron on its own can't do much. The magic comes from wiring lots of them together.
Layers
Neurons are arranged in layers:
- The input layer just holds your raw data: pixel values, a customer's age and spending, sensor readings.
- One or more hidden layers sit in the middle. Each neuron there looks at everything in the previous layer and builds something new from it.
- The output layer gives the answer: a price, a class label, a set of probabilities. The word "deep" in deep learning just means "lots of hidden layers.
Why the activation function matters so much
Here's something that surprised me when I first learned it. If you left out the activation functions, you could stack a hundred layers and the whole network would still behave like one single weighted sum. All that depth would be wasted.
Activation functions add the bend. Common ones:
- ReLU: returns the input if it's positive and zero otherwise. Simple, fast, and the default choice for hidden layers.
- Tanh: squeezes values between -1 and 1.
- Sigmoid: squeezes values between 0 and 1, handy for an output that should be a probability.
- Softmax: turns a group of outputs into probabilities that sum to 1, used when choosing among several classes. With these bends in place, a network can carve out wildly curved decision regions and fit patterns no straight rule could capture.
How a network learns
A new network is just random numbers, so its first guesses are nonsense. Learning is a loop:
- Forward pass. Feed an example in and let the signal flow through to the output.
- Measure the error. Compare the output with the correct answer using a loss function (mean squared error for numbers, cross-entropy for classes).
- Backpropagation. Work backwards from the output, using the chain rule from calculus to figure out how much each weight contributed to the mistake.
- Update. Nudge every weight a little in the direction that reduces the error. Optimizers like SGD and Adam handle this step. Do that over thousands of examples, many times over, and the weights slowly settle into values that work. One full pass through the training data is called an epoch.
A tiny network in code
python
import tensorflow as tf
from tensorflow import keras
model = keras.Sequential([
keras.layers.Input(shape=(10,)),
keras.layers.Dense(32, activation="relu"),
keras.layers.Dense(16, activation="relu"),
keras.layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"])
model.fit(X_train, y_train, epochs=20, batch_size=32,
validation_split=0.2)
That's the whole thing: two hidden layers, an output, and a training call. The validation_split holds back a slice of data so you can watch how the network does on examples it hasn't trained on.
Choices you'll have to make
How big? More layers and neurons mean more capacity, but also more appetite for data and a greater risk of memorizing instead of learning. Start small and grow only if you need to.
Learning rate. Probably the most important setting. Too high and training bounces around wildly, too low and it crawls.
Batch size. Instead of updating after every single example, networks usually learn from small batches. Typical values are 32, 64 or 128.
Overfitting: the classic trap
A network can get so good at its training examples that it learns their quirks, even the noise. You'll see training accuracy keep climbing while validation accuracy stalls or falls. The usual fixes:
- Get more data.
- Dropout: randomly switch off a share of neurons during training so the network can't lean on any single path.
- Weight decay (L2 regularization): discourage very large weights.
- Early stopping: halt training when the validation score stops improving.
- Make the network smaller.
Honest limitations
Neural networks are hungry for data, they take more compute than simpler models, and they're hard to interpret. When one gives a strange answer, it's tough to say why. On small, tidy, tabular datasets, simpler methods often do just as well with far less fuss.
But when the patterns are tangled and you have plenty of examples, a network can find structure that nobody wrote down by hand.
Takeaways
- A neuron is a weighted sum followed by an activation.
- Layers of neurons build progressively richer features.
- Activation functions supply the non-linearity that makes depth worthwhile.
- Training is forward pass, loss, backpropagation, update, repeated many times.
- Watch validation performance to catch overfitting early.
Top comments (0)