Now we are going to be creating our owl personal Perceptron, be creative with what you would do with him. In this example we are going to use it as a Logic AND Gate
Learn how to create your Perceptron
We're going to be creating in 3 different ways
- Normal Python Lists
- Last but not least, with PyTorch
The AND Gate
The AND Gate is a piece of electronics that receives two values as inputs. He follows this table, we call this a "truth table". So for our example we need to use the perceptron to predict the outcome given the inputs x1 and x2
Using only native python code
When we are using just native code we don't have much space to change.
- Create a List with the Test inputs
- Create the weights and bias ( you can play around here )
- Calculate the output and check the results
Easing things up with Numpy
When we are using numpy we can have a more versatile approach, imagine if you had 1000 inputs, in the previous approach, you would have to make a for loop to calculate the outcome, here we take advantage of matrix multiplications with np.dot.
Getting started with PyTorch
PyTorch is a library that is heavily used and backed by facebook. As PyTorch make some things more simple, in this case we added the activation function ( Sigmoid ) to transform the output in probabilities. If the value is greater than 0.5, then the label is 1.0, else 0.0.
- Create your tensors with torch.Tensor(...)
- Same approach for your weights and bias
- Calculate the matrix multiplication using torch.mm
- Use the activation function ( sigmoid ) to generate the outcomes
The library doesn't matter that much
If you aren't doing top research or making production code, just playing around. Any of these ways are great to learn. Going forward, it's pretty much essential for you to develop a knowledge at least in one of the most used libraries today such as TensorFlow, Keras, PyTorch, Scikit. If you want more content like this, I have a YouTube Channel
If you are interested in the code, you cand find it in my github page here