Machine Learning - Dense Layer
Tags: machinelearning, beginners, computerscience
Published: 2020-01-19 16:26:20

---
title: Machine Learning - Dense Layer
published: true
description: Machine Learning studies
cover_image: https://i.ibb.co/SQv2JZN/terminator-genisys-2.jpg
tags: machinelearning, Beginners, Computer Science
series: Making Of Terminator
---
Hey there,
This is most likely the fourth one in the series. How is your weekend going so far?  

Lets start off with an intersting story

<i>Lets say there is this boy namely Isaac Newton walking by an elephant camp. This is way before that apple tree incident by the way. He notice that all of the elephants weren’t being kept in cages or held by the use of chains.All that was holding them back from escaping the camp, was a small piece of rope tied to one of their legs . Him being curious approached the trainer asked why the elephants were just standing there and never tried to escape. The trainer replied </i>

<blockquote>"when they are very young and much smaller we use the same size rope to tie them and, at that age, it’s enough to hold them. As they grow up, they are conditioned to believe they cannot break away. They believe the rope can still hold them, so they never try to break free"</blockquote>

<code>No matter how much the world tries to hold you back, always continue with the belief that what you want to achieve is possible. Believing you can become successful is the most important step in actually achieving it.</code>

<blockquote>Get an elephant first.</blockquote>

#Main Content From Here
We allready created a model to convert Celsius to Fahrenheit,where we used a simple neural network to find the relation between degrees Celsius and degrees Fahrenheit. We can see that our network has a single dense layer.

![details](https://i.ibb.co/3pBFJM0/denselayer.png)

<code>But what exactly is a dense layer?</code>  In order to understand what a dense layer is, 
let's create a slightly more complicated neural network that has 
<ul>
<li>3 inputs</li> 
<li>1 hidden layer with 2 units</li> 
<li> An output layer with only a single unit.</li>
</ul> 
![details](https://i.ibb.co/wR43DS5/basic-desnse-layer.png)

Recall, that you can think of a neural network as a stack of layers, where each layer is made up of units. These units are also called <code>neurons</code>.The neurons in each layer can be connected to neurons in the following layer. 
For example, 

![details](https://i.ibb.co/9GckfSj/newurons-connection.png)

Here we can see this neuron in the hidden layer receives the data from all the inputs. Now, we can do the same with all the other neurons. Let's see what this will look like.

![details](https://i.ibb.co/602NMCb/all-connected.png)

These types of layers are <code>fully connected or dense layers</code>. So when we use a dense layer in keras , we're simply stating that the neurons in that layer are fully connected to the neurons in the previous layer.

For example, to create there both neural networking keras, we simply have to use the following statements

![details](https://i.ibb.co/frsLFpT/following-code-dense.png)

But, what is the dense layer actually doing? 
To understand what's going on, we have to take a quick look at the math. 

![details](https://i.ibb.co/mJfJcfv/final.png)

Let's say we have three inputs to a model,<code>x1, x2, and x3</code> and that <code>a1 and a2</code> are the neurons in our hidden layer, and <code>a3</code> is the neuron in our output layer. Since it's the last layer, it's also the result of the model. Remember  a layer has map that's applied to internal variables in it. Well, the <code>w's</code> and the <code>b's</code> that you see here within the neurons are the internal variables, also known as <code>weights and biases</code>. It's their values that get adjusted during the training process to enable the model to best match the inputs to the outputs. So the <code>w's</code> and the <code>b's</code> are the weights and the biases of our model. 

Here you can see the math going on in a dense layer. 
For example, the output value of neuron <code>a1</code> is calculated by multiplying the input <code>x1</code> with weight <code>w11</code>, and then adding input <code>x2</code> multiplied by weight <code>w12</code>, and then adding input <code>x3</code> multiplied by <code>w13</code>, and then add the signal weight <code>b1</code>. Similarly, in order to calculate the output value <code>a3</code>, we multiply the result of <code>a1</code> with weight <code>w31</code>, and then add the result of <code>a2</code> multiplied by <code>w32</code>, and then add the single weight <code>b3</code>.

As I said about before, what happens in the training process is that these <code>weights</code> and <code>biases</code> are being tuned to the best possible values to be able to match the inputs to the outputs. What's important to note however, is that the math never changes. In other words, the training process only changes the <code>w</code> and <code>b</code> variables to be able to match the input to the output. When you start with machine learning, it may seem strange that this actually works, but that's conceptually how machine learning works. Let's now get back to our example converting Celsius to Fahrenheit.

![details](https://i.ibb.co/XZtTrw3/fahrenheit.png)

With the single neuron, we only have one weight, <code>w11</code> and <code>b1</code> available to tune. 



But you know what? 
That's exactly what we needed to solve the Celsius and Fahrenheit problems since the formula to convert Celsius to Fahrenheit is the linear equation <code>F =C * 1.8 + 32</code>.

If weight w11 is set to 1.8, and b1 to 32, then we would have exactly the formula to solve this conversion problem. 

In fact, when we look at the printout from the Colab, you can see that our weight and bias get tuned to the correct values to solve Celsius to Fahrenheit conversion.
![details](https://i.ibb.co/DRzC6wR/reallife.png)

Namely, the weight is tuned to close to 1.8, and the bias is 32. 
When doing practical machine learning, we can never match the variable against the target algorithm like this.
How could we? 

We don't even know the target algorithm. 

<code>That's why we use machine learning in the first place.</code> Duhhh!!!
Otherwise, we would just hard code the algorithm as a function.
So let's look at how we would approach this problem in real life.

![details](https://i.ibb.co/6WLvDHq/example.png)

Without knowing the target algorithm, we would just give a model a number of layers and weights to tune. Then, we just hope the model will be able to figure out how to tune the model algorithm to match the input to the output.

As you can see in this example, it also successfully did so with this model having three layers and lots more neurons. Looking at the weights now, you can see that there's no direct mapping between them and the conversion formula. 

<code>In general, when we do machine learning, we typically just try different neural networks with different numbers of layers and neurons in a trial and error away, and see if the model is able to solve the problem during the the training phase</code>

Lets make the intersting part in the next post.