About this course
<p>A straight line equation is a good place to start understanding how a neural network works. It's what we explored in my previous article, <a href="https://www.freecodecamp.org/news/neural-networks-explained-using-y-ax-b/">How Neural Networks Work – Explained Using the Straight Line Equation (y=ax+b)</a>.</p>
<p>But, what if the relationship between the input and the output <strong>isn't linear?</strong></p>
<p>We all know that in the real world, the data doesn't always follow a straight line pattern. This is where things start getting interesting: sometimes a single rule isn't enough to describe the relationship between the variables.</p>
<p>That’s where non-linearity comes in. It allows neural networks to capture changing behaviour in data.</p>
<p>In this article, we'll explore the concept of non-linearity that can capture complex relationships in data. Using a simple example, we'll try to understand piecewise linear curves, ReLU functions, S-shaped curves, and sigmoid functions in relation to how they help neural networks learn patterns that a straight line can't describe.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-when-a-straight-line-is-enough">When a Straight Line Is Enough</a></p>
</li>
<li><p><a href="#heading-when-a-straight-line-isnt-enough">When a Straight Line Isn't Enough</a></p>
<ul>
<li><p><a href="#heading-piecewise-linear-curve">Piecewise Linear Curve</a></p>
</li>
<li><p><a href="#heading-what-is-a-relu-function">What is a ReLU Function?</a></p>
</li>
<li><p><a href="#heading-s-shaped-curve">S-Shaped Curve</a></p>
</li>
<li><p><a href="#heading-what-is-a-sigmoid-function">What is a Sigmoid Function?</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-modelling-a-deep-neural-network">Modelling a Deep Neural Network</a></p>
<ul>
<li><p><a href="#heading-predicting-marks-using-relu">Predicting Marks Using ReLU</a></p>
</li>
<li><p><a href="#heading-predicting-passfail-using-relu-and-sigmoid">Predicting Pass/Fail Using ReLU and Sigmoid</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-final-thoughts">Final Thoughts</a></p>
</li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li><p>Basic understanding of linear functions</p>
</li>
<li><p>Familiarity with the concept of deep neural networks</p>
</li>
</ul>
<h2 id="heading-when-a-straight-line-is-enough">When a Straight Line Is Enough</h2>
<p>A straight line equation means that the same rule applies throughout the relationship.</p>
<p>y = ax + b</p>
<pre><code class="language-plaintext">y => Output variable
x => Input variable
a => Amount by which y changes when x changes (slope)
b => Value of y when x is 0 (y intercept)
</code></pre>
<p>We can look at an example to understand it better.</p>
<p>Ms. Poly is a math teacher who wants to come up with a study plan for her students to help them ace the final exam. She creates a rule of thumb using one factor: the number of hours studied per week. This means that the mark scored by a student is directly proportionate to the hours they spend studying.</p>
<p>Based on her assumption, she comes up with an equation in the form y=ax+b.</p>
<pre><code class="language-plaintext">y => Marks scored
x => Number of hours studied
a => Increase in marks for every hour studied
b => Minimum marks every student is capable of scoring without studying.
</code></pre>
<p>So, after a quarterly exam, she interviews her students and collects information on how many hours they studied math per week. She creates a table with two columns: number of hours studied (x) per week and marks scored (y) and plots them.</p>
<table>
<thead>
<tr>
<th>Number of Hours Studied</th>
<th>Marks Scored</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>40</td>
</tr>
<tr>
<td>1</td>
<td>40</td>
</tr>
<tr>
<td>2</td>
<td>41</td>
</tr>
<tr>
<td>3</td>
<td>40</td>
</tr>
<tr>
<td>4</td>
<td>48</td>
</tr>
<tr>
<td>5</td>
<td>58</td>
</tr>
<tr>
<td>6</td>
<td>69</td>
</tr>
<tr>
<td>7</td>
<td>80</td>
</tr>
<tr>
<td>8</td>
<td>80</td>
</tr>
</tbody></table>
<p>But when she tries to apply her simple formula, it doesn't seem to align with the data she collected, proving that real-life data is rarely that straightforward.</p>
<h2 id="heading-when-a-straight-line-isnt-enough">When a Straight Line Isn't Enough</h2>
<p>Ms. Poly observes the graph closely and identifies a few patterns. Students who study for only a few hours may not see much improvement. Once they cross a certain point, their marks begin to increase with additional study. And again after a certain point, the marks remain fairly constant.</p>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/958b4685-d030-4efa-bc16-50ab231272b3.png" alt="Input Data: Marks scored Vs Study Hours" style="display: block;" width="600" height="400" loading="lazy">
<p>Thus, Ms Poly’s simple straight line equation isn’t enough to describe the relation between number of hours studied (x) per week and marks scored (y).</p>
<p>Instead, it can be divided into three parts,</p>
<pre><code class="language-plaintext">Less than t₁ hours: Marks remain constant
t₁ to t₂ hours: Marks increase with study time
More than t₂ hours: Marks remain constant
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/8c2fb722-bbf7-4ceb-856d-5a352190674d.png" alt="Input Data: Points of Change" style="display: block;" width="600" height="400" loading="lazy">
<p>So, she can have separate equations for each range and write them like this:</p>
<pre><code class="language-plaintext">y = 1st equation, x<t₁
y = 2nd equation, t₁<=x<t₂
y = 3rd equation, x>=t₂
</code></pre>
<p>This example gives us our first glimpse of non-linearity. There are changes in the relationship at specific points, so a single straight line equation is insufficient as the behaviour needs to change at different points.</p>
<p>A simple way to handle this is using a <strong>piecewise linear function</strong>.</p>
<h3 id="heading-piecewise-linear-curve">Piecewise Linear Curve</h3>
<p>To begin with, for simplicity, Ms. Poly decides to consider only two regions.</p>
<pre><code class="language-plaintext">Region1: Hours of study < t₁
Region2: Hours of study >= t₁
</code></pre>
<p>The values in the first region hover around a particular value. Even though they don’t lie perfectly in a line, she can make an approximation and represent that region as a line with slope=0. This indicates that the marks remain roughly the same.</p>
<p>In the second region, there seems to be a positive slope. So she decides on another straight line equation for that steep upward sloping line.</p>
<p>Ms. Poly ends up having two equations for the above graph because one equation can't define it. The rule changes for different ranges as the pattern changes.</p>
<p>So, she can write it like this:</p>
<pre><code class="language-plaintext">y = 1st equation, x<t₁
y = 2nd equation, x>=t₁
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/fe83f93e-2592-4300-b0ab-45dbcac0982c.png" alt="Piecewise Linear Curve" style="display: block;" width="600" height="400" loading="lazy">
<p>Individually, these may seem like straight line equations. But when put together, the overall relationship is no longer a straight line. When x crosses a certain value, the equation changes. This is called <strong>piecewise linear function.</strong> It's a function that's made up of different rules for different ranges of input.</p>
<p>This idea appears in one of the most widely used activation functions in neural networks: <strong>ReLU</strong>.</p>
<h3 id="heading-what-is-a-relu-function">What is a ReLU Function?</h3>
<p>ReLU stands for Rectified Linear Unit. Mathematically, it's this:</p>
<pre><code class="language-plaintext">y = ReLU(x) = max(0,x)
</code></pre>
<p>It keeps positive inputs unchanged and turns negative inputs to zero.</p>
<p>Here too, the individual piece is linear but the overall function is non-linear. So, ReLU is a piecewise linear function that introduces non-linearity.</p>
<p>The graph below shows the ReLU function:</p>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/589adfcc-fa25-4517-a647-ad40933ea2b4.png" alt="ReLU Function" style="display: block;" width="600" height="400" loading="lazy">
<p>This simple change in behaviour allows a neuron to respond differently to different ranges of input, giving neural networks a way to move beyond a single straight-line relationship.</p>
<h3 id="heading-s-shaped-curve">S-Shaped Curve</h3>
<p>Now, a piecewise linear curve does the job but Ms. Poly sees that there's quite a sharp transition from one region to the other. She had also only considered two regions for simplicity, even though the distribution can be divided into three regions.</p>
<p>So, she draws a smooth curve roughly passing through all the points as shown below:</p>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/7f1fab69-ca10-43be-83a0-d490fa2de057.png" alt="S-Shaped Curve" style="display: block;" width="600" height="400" loading="lazy">
<p>This shape shows that the marks remain almost the same when a student studies for up to t₁ number of hours. Then, an increase in hours of study from t₁ to t₂, increases the marks scored, so there's a slope. In the end at t₂, it flattens out again. This transition is gradual and continuous unlike the piecewise linear curve which changes directions using simple straight lines.</p>
<p>The curve that Ms Poly drew isn't some random pattern. It can be represented mathematically, too. One of the functions that produces this kind of shape is the <strong>sigmoid</strong> function.</p>
<h3 id="heading-what-is-a-sigmoid-function">What is a Sigmoid Function?</h3>
<p>A sigmoid function is a mathematical formula that takes any real-valued input and maps it to values between 0 and 1. It slowly increases from values close to 0, rises quickly in the middle and gradually levels off at values close to 1.</p>
<pre><code class="language-plaintext">y = σ(x) = 1 / (1 + exp(-x))
</code></pre>
<p>The graph below shows the sigmoid function:</p>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/851d09c8-34ed-4757-a875-debf18cfd3b8.png" alt="Sigmoid Function" style="display: block;" width="600" height="400" loading="lazy">
<p>The sigmoid function is significant in neural networks because it introduces non-linearity while also restricting the output between 0 and 1. This makes it useful when the output needs to represent a probability such as the likelihood of a student passing an exam. A higher sigmoid output indicates a higher probability of the student passing, and vice versa.</p>
<h2 id="heading-modeling-a-deep-neural-network">Modeling a Deep Neural Network</h2>
<p>Ms Poly should now combine the concepts of linear and non-linear equations to model a deep neural network. She has to take two separate approaches to ,</p>
<ol>
<li><p>Predict the marks scored in the exam</p>
</li>
<li><p>Predict Pass/Fail in the exam</p>
</li>
</ol>
<p>She uses two very simple untrained neural networks with manually chosen weights and biases for each. She is just trying it out to illustrate how calculations work. In reality she wouldn't choose the parameters herself. She would set up the network with weights and biases being learnt during training by repeatedly comparing the predicted values with the actual values and adjusting the parameters to minimise the error.</p>
<h3 id="heading-predicting-marks-using-relu">Predicting Marks Using ReLU</h3>
<h4 id="heading-step-1-set-up-the-network"><strong>Step #1: Set up the network</strong></h4>
<p>Ms Poly has the set up the deep neural network with simple equations in the following way:</p>
<ul>
<li><p>Input Layer: The number of hours studied (x) is received here.</p>
</li>
<li><p>Hidden Layer: Linear transformation and ReLU is applied to the input.</p>
<pre><code class="language-plaintext">z = w₁x + b₁, = x - 3 (w₁=1, b₁=-3)
h = ReLU(z) = max(0, z)
</code></pre>
</li>
<li><p>Output Layer: The marks scored is predicted after applying a linear transformation to the hidden layer's output(h).</p>
<pre><code class="language-plaintext">ŷ = w₂h + b₂ = 10h + 40 (w₂=10, b₂=40)
</code></pre>
</li>
</ul>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/39480008-d925-4963-a521-5dea5988e461.png" alt="39480008-d925-4963-a521-5dea5988e461" style="display: block;" width="600" height="400" loading="lazy">
<p><strong>Step #2: Calculate the Predicted Marks</strong></p>
<p>Suppose she wants to calculate the marks scored by a student who studies for 5 hours. This is how she does it:</p>
<pre><code class="language-plaintext">z = w₁x + b₁, = x - 3 = 5 - 3 = 2
h = ReLU(z) = max(0, z) = max(0, 2) = 2
</code></pre>
<p>Finally calculate the predicted marks:</p>
<pre><code class="language-plaintext">ŷ = w₂h + b₂ = 10h + 40 = 10(2) + 40 = 60
</code></pre>
<p>The neural network predicts 60 marks whereas the actual marks in the dataset is 58.</p>
<p><strong>Step #3: Compare Predictions with Actual Marks</strong></p>
<p>After Ms Poly gets all the marks predicted by the neural network, she compares them with the actual data in the table below:</p>
<table>
<thead>
<tr>
<th>Number of Hours Studied</th>
<th>Marks Scored</th>
<th>Predicted Marks</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>40</td>
<td>40</td>
</tr>
<tr>
<td>1</td>
<td>40</td>
<td>40</td>
</tr>
<tr>
<td>2</td>
<td>41</td>
<td>40</td>
</tr>
<tr>
<td>3</td>
<td>40</td>
<td>40</td>
</tr>
<tr>
<td>4</td>
<td>48</td>
<td>50</td>
</tr>
<tr>
<td>5</td>
<td>58</td>
<td>60</td>
</tr>
<tr>
<td>6</td>
<td>69</td>
<td>70</td>
</tr>
<tr>
<td>7</td>
<td>80</td>
<td>80</td>
</tr>
<tr>
<td>8</td>
<td>80</td>
<td>90</td>
</tr>
</tbody></table>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/b0210710-a870-4b3d-a829-054d9f091538.png" alt="b0210710-a870-4b3d-a829-054d9f091538" style="display: block;" width="600" height="400" loading="lazy">
<p>The actual and predicted marks don't match exactly, but they're close. The marks keep increasing and don't flatten out in the end because the value keeps increasing after the ReLU threshold.</p>
<p>Through this example Ms Poly has demonstrated how ReLU helps in modeling non-linear relationships in the data.</p>
<h3 id="heading-predicting-passfail-using-relu-and-sigmoid">Predicting Pass/Fail Using ReLU and Sigmoid</h3>
<p>Here Ms Poly will use a neural network to predict whether a student passes or fails in the final exam. She assumes a student needs to score a minimum of 50 marks to pass. This network will be used to predict the probability of passing the exam.</p>
<p><strong>Step #1: Set Up the Network</strong></p>
<p>Ms Poly has the set up the deep neural network with simple equations in the following way:</p>
<ul>
<li><p>Input Layer: The number of hours studied (x) is received here.</p>
</li>
<li><p>Hidden Layer: Linear transformation and ReLU is applied to the input.</p>
<pre><code class="language-plaintext">z₁ = w₁x + b₁, = x - 4 (w₁=1, b₁=-4)
h = ReLU(z₁) = max(0, z₁)
</code></pre>
</li>
<li><p>Output Layer: The pass/fail is predicted after applying sigmoid activation to the linear transformation of the hidden layer's output(h).</p>
<pre><code class="language-plaintext">z₂ = w₂h + b₂ = 2h - 1 (w₂=2, b₂=-1)
p = σ(z₂) = 1 / (1 + exp(-z₂))
ŷ = Pass, if p >= 0.5
ŷ = Fail, if p < 0.5
</code></pre>
</li>
</ul>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/c4508adf-b2c4-42ae-b297-29afd8ec3409.png" alt="c4508adf-b2c4-42ae-b297-29afd8ec3409" style="display: block;" width="600" height="400" loading="lazy">
<p><strong>Step #2: Calculate the Pass/Fail Prediction</strong></p>
<p>Suppose she wants to predict the result for a student who studies for 3 hours. This is how she does it:</p>
<pre><code class="language-plaintext">z₁ = w₁x + b₁, = x - 4 = 3 - 4 = -1
h = ReLU(z₁) = max(0, z₁) = max(0, -1) = 0
</code></pre>
<p>Predicting if the student passes/fails:</p>
<pre><code class="language-plaintext">z₂ = w₂h + b₂ = 2h - 1 = 2(0) - 1 = -1
p = σ(z₂) = 1 / (1 + exp(-z₂)) = 1 / (1 + exp(-(-1))) ≈ 0.269
</code></pre>
<p>Since 0.269<0.5, the student's result is <strong>Fail</strong>.</p>
<p>Now for the result of a student who studies for 5 hours:</p>
<pre><code class="language-plaintext">z₁ = w₁x + b₁, = x - 4 = 5 - 4 = 1
h = ReLU(z₁) = max(0, z₁) = max(0, 1) = 1
</code></pre>
<p>Predicting if the student passes/fails:</p>
<pre><code class="language-plaintext">z₂ = w₂h + b₂ = 2h - 1 = 2(1) - 1 = 1
p = σ(z₂) = 1 / (1 + exp(-z₂)) = 1 / (1 + exp(-1)) ≈ 0.731
</code></pre>
<p>Since 0.731>0.5, the student's result is <strong>Pass</strong>.</p>
<p><strong>Step #3: Compare Predictions with Actual Results</strong></p>
<table>
<thead>
<tr>
<th>Number of Hours Studied</th>
<th>Marks Scored</th>
<th>Actual Result</th>
<th>Predicted Score</th>
<th>Predicted Result</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>40</td>
<td>Fail</td>
<td>0.269</td>
<td>Fail</td>
</tr>
<tr>
<td>1</td>
<td>40</td>
<td>Fail</td>
<td>0.269</td>
<td>Fail</td>
</tr>
<tr>
<td>2</td>
<td>41</td>
<td>Fail</td>
<td>0.269</td>
<td>Fail</td>
</tr>
<tr>
<td>3</td>
<td>40</td>
<td>Fail</td>
<td>0.269</td>
<td>Fail</td>
</tr>
<tr>
<td>4</td>
<td>48</td>
<td>Fail</td>
<td>0.269</td>
<td>Fail</td>
</tr>
<tr>
<td>5</td>
<td>58</td>
<td>Pass</td>
<td>0.731</td>
<td>Pass</td>
</tr>
<tr>
<td>6</td>
<td>69</td>
<td>Pass</td>
<td>0.953</td>
<td>Pass</td>
</tr>
<tr>
<td>7</td>
<td>80</td>
<td>Pass</td>
<td>0.993</td>
<td>Pass</td>
</tr>
<tr>
<td>8</td>
<td>80</td>
<td>Pass</td>
<td>0.999</td>
<td>Pass</td>
</tr>
</tbody></table>
<img src="https://cdn.hashnode.com/uploads/covers/681473bee25e59d34a17668b/2bf27e7a-4a54-4671-b853-6bf16b0b0ec0.png" alt="2bf27e7a-4a54-4671-b853-6bf16b0b0ec0" style="display: block;" width="600" height="400" loading="lazy">
<p>The network correctly classifies the result of all the students in this example. As the study hours increase beyond 4, the predicted score increases, making pass prediction more likely.</p>
<p>Through this example Ms Poly has demonstrated how ReLU and sigmoid work together. ReLU transforms the input in the hidden layer and sigmoid helps in converting the output to a score that can be used to make Pass/Fail prediction.</p>
<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>The simple examples shown above help us understand an important rule about neural networks: the relationship between the outputs and the inputs can behave differently across different parts of the input. They need to be captured in order to produce meaningful results from real-world data.</p>
<p>A straight line equation is enough when the relationship in data is simple and consistent. But real-life data isn't always that straightforward. By introducing non-linearity, neural networks can move beyond straight-line relationships and learn complex relationships hidden in data.</p>