# Ens'IA - Session 2 - Intro to neural networks 1/2

Welcome the **second session** of Ens'IA.
Today, things are gonna get interesting !
We are gonna focus on **neural networks**
But first, (what a surprise), neural networks are make of neurons ! So what is a neuron ?  


To make sure you understand what it is, we will go back in 1958 and give a look at **the perceptron**.  

During the oral presentation, you should have seen that a perceptron have one or more inputs and a unique output.  
The output is given by :  
\begin{equation}
    s = \left\{
        \begin{array}{ll}
            1 & \mbox{if } \sum_{i=0}^{n} a_{i} \times w_{i} + b > 0 \\
            0 & \mbox{otherwise}
        \end{array}
    \right.
\end{equation}


Now, it's your time to *create your own perceptron*.

### Perceptron implementation


```
class Perceptron:

  """
  Build of a perceptron
  weights : List of weights
  bias : The bias.
  """
  def __init__(self,weights,bias):
    #TODO
    self.bias = bias
    self.weights = weights

  """
  Function called when you want to get the output from the input
  input : List of input values.
  """
  def forward(self,input):
    assert(len(input)==len(self.weights))
    #TODO
    sum = 0
    for inp,w in zip(input,self.weights):
      sum += inp*w
    if sum + self.bias > 0:
      return 1
    else:
      return 0
```

### Test

Now, let's **test** it !


```
#TODO
perceptron = Perceptron([1,1],-1)
assert(perceptron.forward([1,1])==1)
assert(perceptron.forward([1,0])==0)
assert(perceptron.forward([0,1])==0)
assert(perceptron.forward([0,0])==0)
```

If you have no message, then it must work!  
Here we have created a perceptron with weights 1 and 1 and the bias is 2.  
Do you notice any link between the inputs and the output? Maybe something you have seen in your processor architecture class...





### NAND implementation

Your next mission will be to create a perceptron that reproduces a NAND gate. It will have 2 inputs and a bias, but you have to find which values are the right ones ...



```
perceptron_nand = Perceptron([-2,-2],3)
assert(perceptron_nand.forward([1,1])==0)
assert(perceptron_nand.forward([1,0])==1)
assert(perceptron_nand.forward([0,1])==1)
assert(perceptron_nand.forward([0,0])==1)
#If you don't get any error messages when running this code, then you have found the right weights and bias!
```

### XOR implementation

And if you now try to find a perceptron that reproduces an XOR gate...?


```
#TODO
#Spoil : It's impossible :p
```

1540e544b08f18cb27799be0eaff142d8b1f4fec
ipynb
Jupyter Notebook
session2/perceptron.ipynb
YannSia/tutorials
eb9847e5ee354e57240ef3c07961674d7cd34803
MIT
2021-10-03T20:49:11.000Z
2021-10-19T12:46:24.000Z
session2/perceptron.ipynb
YannSia/tutorials
eb9847e5ee354e57240ef3c07961674d7cd34803
MIT
session2/perceptron.ipynb
YannSia/tutorials
eb9847e5ee354e57240ef3c07961674d7cd34803
MIT
Qwen/Qwen-72B
1. YES
2. YES