Adding the Layers - Neural Networks Part 1.5

vishalkamath

Vishal

Posted on September 9, 2023

Adding the Layers - Neural Networks Part 1.5

We have worked with a single neuron in the previous blog, now let us look at the code part

Coding Neurons in Python

Neuron

We can calculate the value using Python's dot product, Here's how

Dot product

Dot product equation

So the output of a neuron can also be calculated as

Implementing neuron calculation using dot product

Implementation in Python

import numpy as np

inputs = [1, 2, 3, 2.5]
weights = [0.2, 0.8, -0.5, 1.0]
bias = 2

output = np.dot(inputs, weights) + bias
print(output)
Enter fullscreen mode Exit fullscreen mode

Working with Layers

Now that we have found a way to calculate the output of a neuron we can move up by calculating the output of a layer with 3 neurons. The inputs for all three neurons will be the same the input is the output of the previous layer so the only way for us to influence the output of each neuron is with the help of weights and biases. We will pass weights as a 2D array of shapes (3,4), and biases as an array of shape (4,).

Layer

import numpy as np

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

output = np.dot(weights, inputs ) + biases
# output [4.8  , 1.21 , 2.385]
Enter fullscreen mode Exit fullscreen mode

Note: It is important to note that weights are passed before inputs as the shape of weights is (3,4) and inputs is (4,). If we were to put inputs before weights the (4,) and (3,4) will clash and give an ERROR as 4 doesn't match with 3.


Previous Part
Neural Networks describe the World!! - Neural Networks Part 1


GitHub: https://github.com/Vishal-Kamath.
LinkedIn: https://www.linkedin.com/in/vishalkamath853
Medium: https://medium.com/@vishalkamath853
Twitter: https://twitter.com/VishalKamath853

💖 💪 🙅 🚩
vishalkamath
Vishal

Posted on September 9, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related