Creating a Neural Network: AI Machine Learning
A neural network is a type of machine learning algorithm modeled after the structure and function of the human brain. It is composed of a large number of interconnected “neurons,” which are organized into layers. These layers are responsible for processing and transforming the input data and passing it through to the output layer, where the final prediction or decision is made.
Image recognition
Neural networks can be used to classify images of (say) cats and dogs by training a model on a large dataset of labeled images. The model is presented with an input image, and its job is to predict the correct label (e.g., “cat” or “dog”) for the image.
To train the model, the input images are passed through the network and the model makes predictions based on the patterns it has learned from the training data. If the prediction is incorrect, the model adjusts the weights of the connections between neurons in order to improve its accuracy on future predictions.
Our own model
I want to create a very simple model to “recognise” faces. I first start with a 5 by 5 grid, and define what I think is a perfect face. This is shown above. I can then convert this to numerical values by defining the white spaces as 0 and the black squares as 1.
I can then represent this information as 5 column vectors:
Building a weighting model
Next I need to decide which squares would be acceptable for a face. I’ve kept the black squares for the most desirable, and then added some grey shade for squares that could also be included. I can then convert this into numerical data by deciding on a weight that each square should receive:
Here I am using 1 to represent a very desirable square, 0.5 for a somewhat desirable square and -1 for an undesirable square. I can also represent this weighting model as 5 column vectors:
Using the dot product
I can then find the sum of the dot products of the 5 x vectors with the 5 w vectors. In formal notation this is given by:
What this means is that I find the dot product of x_1 and w_1 and then add this to the dot product of x_2 and w_2 etc. For example with:
This gives:
Which is:
Doing this for all 5 vectors gives:
So my perfect face has a score of 5. So I can therefore give an upper and lower bound what what would be considered a face. Let’s say:
Testing our model: A Face
I want to see if the above image would be recognised as a face by our model. This has the following:
And when we calculate the sum of the dot products we get:
Which would be recognised as a face.
Testing our model: Not a Face
There are 2 to the power 25 different patterns that can be generated (over 33 million), so we would expect that the majority do not get recognised as a face. I randomly generated a 25 length binary string and created the image above. When we use our model it returns:
Which would not be recognised as a face.
Using Python and modifying the design
I decided to modify the weighting so that undesirable squares received -2, to make them less likely to appear. I then changed the weighting so that I wanted a score between 4.5 and 5.5 inclusive.
I then wrote some Python code that would randomly generate 200,000 images and then run this algorithm to check whether this was recognised as a face or not.
The results
You can see some of the results above – whilst not perfect, they do have a feel of a face about them. And my favourite is below:
A nice cheeky grin! You can see the power of this method – this was an extremely simple model and yet achieves some results very quickly. With images of 1 million pixels and much more advanced weighting algorithms, modern AI systems can accurately identify and categorise a huge variety of images.
Leave a comment
Comments feed for this article