Activation Functions: Introducing Non-Linearity with Sigmoid

Introduction

Welcome back to our course "Neural Network Fundamentals: Neurons and Layers"! In this third lesson, we're building upon what you learned in our previous lesson about the basic artificial neuron. You've already implemented a simple neuron that computes a weighted sum of inputs plus a bias.

Today, we're taking an important step forward by introducing activation functions — a crucial component that enables neural networks to learn complex patterns. In particular, we'll focus on the Sigmoid activation function, one of the classical functions used in neural networks.

In our previous lesson, our neuron could only produce linear outputs. While this is useful for some tasks, it severely limits what our neural networks can learn. Today, we'll overcome this limitation by adding non-linearity to our neurons, allowing them to model more complex relationships in data.

The Need for Non-Linearity

Before diving into specific activation functions, let's understand why we need them in the first place.

The neuron we built in the previous lesson computes a weighted sum of inputs plus a bias. This is a linear transformation — mathematically, it can only represent straight lines (in 2D) or flat planes (in higher dimensions).

Consider what would happen if we stacked multiple layers of these linear neurons:

Layer 1 output is a linear function of the input:

Layer1out=W1X+b1\text{Layer}_1^{out} = W_1 X + b_1

Layer 2 output is a linear function of Layer 1's output:

Layer2out=W2(W1X+b1)+b2=(W2W1)X+(W2b1+b2)\text{Layer}_2^{out} = W_2 (W_1 X + b_1) + b_2 \\ = (W_2 W_1) X + (W_2 b_1 + b_2)

We can define new parameters W3=(W2W1)W_3 = (W_2 W_1) and b3=(W2b1+b2)b_3 = (W_2 b_1 + b_2), so:

Layer2out=W3X+b3\text{Layer}_2^{out} = W_3 X + b_3

As we can see, this simplifies to just another linear function! No matter how many linear layers we stack, the entire network would still only be able to learn linear relationships.

But real-world problems are rarely linear. Think about image recognition — the relationship between pixel values and whether an image contains a cat is highly non-linear. To model such complex patterns, we need to introduce non-linearity into our networks.

This is precisely the role of activation functions — they apply a non-linear transformation to the neuron's output, enabling neural networks to learn and represent more complex patterns.

Understanding Activation Functions

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal