##A Single Neuron with 3 inputs (Vanilla Network)
As in most cases, when you initialize parameters in neural networks, our network will have weights initialized randomly, and biases set as zero to start or 1 but in this case we set up at 2


```python
inputs = [1, 2, 3]
weights = [0.2, 0.8, -0.5]
bias = 2 

output = (inputs[0]*weights[0] + inputs[1]*weights[1]+inputs[2]*weights[2] + bias)
print(output)
```

    2.3


What might we need to change if we have 4 inputs, rather than the 3


```python
inputs = [ 1.0 , 2.0 , 3.0 , 2.5 ]
weights = [ 0.2 , 0.8 , - 0.5 , 1.0 ]
bias = 2.0

output = (inputs[0]*weights[0] + inputs[1]*weights[1]+inputs[2]*weights[2] + inputs[3]*weights[3] + bias)
print(output)

# Alternative for above

# output = 0
# for i in range(len(inputs)):

#   output = inputs[i] * weights[i] + output

# output += bias

# print(output)
```

    4.8


#A layer of Neurons
Neural networks typically have layers that consist of more than one neuron. Layers are nothing more than groups of neurons. Each neuron in a layer takes exactly the same input — the input given to the layer (which can be either the training data or the output from the previous layer) but contains its own set of weights and its own bias, producing its own unique output. For example Let’s say we have a scenario with 3 neurons in a layer and 4 inputs:

we have three sets of weights and three biases, which define three neurons


```python
inputs = [ 1, 2, 3, 2.5 ]
weights1 = [ 0.2 , 0.8 , - 0.5 , 1 ]
weights2 = [ 0.5 , - 0.91 , 0.26 , - 0.5 ]
weights3 = [ - 0.26 , - 0.27 , 0.17 , 0.87 ]

bias1 = 2
bias2 = 3
bias3 = 0.5


output1 = 0 
output2 = 0
output3 = 0

# for i in range(len(inputs)):
#   output1 = inputs[i] * weights1[i] + output1
#   output2 = inputs[i] * weights2[i] + output2
#   output3 = inputs[i] * weights3[i] + output3

# weighted1 = output1 + bias1
# weighted2 = output2 + bias2
# weighted3 = output3 + bias3

# print([weighted1, weighted2, weighted3])

outputs = [
# Neuron 1:
inputs[0] * weights1[0] +
inputs[1] * weights1[1] +
inputs[2] * weights1[2] +
inputs[3] * weights1[3] + bias1,
# Neuron 2:
inputs[0] * weights2[0] +
inputs[1] * weights2[1] +
inputs[2] * weights2[2] +
inputs[3] * weights2[3] + bias2,
# Neuron 3:
inputs[0] * weights3[0] +
inputs[1] * weights3[1] +
inputs[2] * weights3[2] +
inputs[3] * weights3[3] + bias3]

print(outputs)



```

    [4.8, 1.21, 2.385]



```python
#Alternative for above

inputs = [1, 2, 3, 2.5]
weights = [[0.2, 0.8, - 0.5, 1 ], 
           [0.5, - 0.91, 0.26, - 0.5 ],
           [-0.26, - 0.27, 0.17, 0.87 ]]
biases = [2, 3, 0.5 ]



# Output of current layer
layer_outputs = []


for neuron_weights, neuron_biases in zip(weights, biases):
  #output of given neuron
  neuron_output = 0

  for n_input, n_weights in zip (inputs, neuron_weights):
    neuron_output = n_input * n_weights + neuron_output
  ## Add bias
  neuron_output += neuron_biases
  layer_outputs.append(neuron_output)

print(layer_outputs)
```

    [4.8, 1.21, 2.385]


#Dot Product and Vector Addition in Python

\begin{equation}
\vec{a} \cdot \vec{b}=\sum_{i=1}^{n} a_{i} b_{i}=a_{1} b_{1}+a_{2} b_{2}+\ldots+a_{n} b_{n}
\end{equation}

\begin{equation}
\vec{a}+\vec{b}=\left[a_{1}+b_{1}, a_{2}+b_{2}, \ldots, a_{n}+b_{n}\right]
\end{equation}




```python
a = [1, 2, 3]
b = [2, 3, 4]

dot_product = a[0]*b[0] + a[1]*b[1] + a[2]*b[2]
print(dot_product)
```

    20


#Succinct way to perform in numpy with dot operator 


```python
import numpy as np
print(np.dot(a, b))
```

    20


#A Single Neuron with NumPy


```python
inputs = [ 1.0 , 2.0 , 3.0 , 2.5 ]
weights = [ 0.2 , 0.8 , - 0.5 , 1.0 ]
bias = 2.0

output = np.dot(inputs, weights) + bias
print(output)
```

    4.8


#A Layer of Neurons with NumPy


```python
inputs = np.array([1.0, 2.0, 3.0, 2.5])
weights = np.array([[0.2, 0.8, -0.5, 1],
                [0.5, - 0.91, 0.26, -0.5],
                [- 0.26, - 0.27 , 0.17 , 0.87]])
biases = [2.0, 3.0, 0.5]

output = np.dot(weights, inputs) + biases
print(output)
```

    [4.8   1.21  2.385]


#Matrix Product

\begin{equation}
c_{i j}=a_{i 1} b_{1 j}+a_{i 2} b_{2 j}+\cdots+a_{i n} b_{n j}=\sum_{k=1}^{n} a_{i k} b_{k j}
\end{equation}


#Transposition of the Matrix product


\begin{equation}
\vec{a}.\vec{b}= a.b^{T}
\end{equation}

Transposition simply modifies a matrix in a way that its rows become columns and columns become rows.




```python
#To transform a list into a matrix containing a single row (perform an equivalent operation of turning a vector into row vector)
(perform an equivalenarray of shape (n, 1):
a = [1, 2, 3]
np.expand_dims(np.array(a), axis = 0)
```




    array([[1, 2, 3]])



#A Layer of Neurons & Batch of Data with Numpy


```python
inputs = [[1.0, 2.0, 3.0, 2.5 ],
          [2.0, 5.0, - 1.0, 2.0],
           [-1.5, 2.7, 3.3, - 0.8]]


weights = np.array([[0.2, 0.8, - 0.5, 1.0],
                    [0.5, - 0.91, 0.26, - 0.5],
                    [-0.26, -0.27, 0.17, 0.87]]).T


biases = [2.0 , 3.0 , 0.5 ]
#np.dot(inputs, np.array(weights).T) + biases
output_layer = np.dot(inputs, weights) + biases
print(output_layer)
```

    [[ 4.8    1.21   2.385]
     [ 8.9   -1.81   0.2  ]
     [ 1.41   1.051  0.026]]


d5c405d5f112d059996f4433017112a7de71a166
ipynb
Jupyter Notebook
NNscratch.ipynb
ashyibo23/NeuralNetwork
256bf39de34d2006cb3455d3b6a5c19f84587a1f
MIT
2021-09-14T12:52:05.000Z
2021-09-14T12:52:05.000Z
NNscratch.ipynb
ashyibo23/NeuralNetwork
256bf39de34d2006cb3455d3b6a5c19f84587a1f
MIT
NNscratch.ipynb
ashyibo23/NeuralNetwork
256bf39de34d2006cb3455d3b6a5c19f84587a1f
MIT
Qwen/Qwen-72B
1. YES
2. YES