You are currently browsing the tag archive for the ‘neural network’ tag.

Screen Shot 2022-12-24 at 1.31.32 PM

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

Screen Shot 2022-12-24 at 1.30.00 PM

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

Screen Shot 2022-12-24 at 1.32.42 PM

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.

Screen Shot 2022-12-24 at 1.32.48 PM

I can then represent this information as 5 column vectors:

Screen Shot 2022-12-24 at 1.33.00 PM

Building a weighting model

Screen Shot 2022-12-24 at 1.33.07 PM

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:

Screen Shot 2022-12-24 at 1.33.12 PM

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:

Screen Shot 2022-12-24 at 1.33.17 PM

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:

Screen Shot 2022-12-24 at 1.33.30 PM

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:

Screen Shot 2022-12-24 at 1.33.36 PM

This gives:

Screen Shot 2022-12-24 at 1.33.41 PM

Which is:

Screen Shot 2022-12-24 at 1.33.50 PM

Doing this for all 5 vectors gives:

Screen Shot 2022-12-24 at 1.33.53 PM

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:

Screen Shot 2022-12-24 at 1.33.58 PM

Testing our model: A Face

Screen Shot 2022-12-24 at 1.34.12 PM

I want to see if the above image would be recognised as a face by our model.  This has the following:

Screen Shot 2022-12-24 at 1.34.21 PM

And when we calculate the sum of the dot products we get:

Screen Shot 2022-12-24 at 1.34.25 PM

Which would be recognised as a face.

Testing our model: Not a Face

Screen Shot 2022-12-24 at 1.34.32 PM

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:

Screen Shot 2022-12-24 at 1.34.39 PM

Which would not be recognised as a face.

Using Python and modifying the design

Screen Shot 2022-12-24 at 1.35.35 PM

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.

Screen Shot 2022-12-24 at 1.59.06 PM

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

Screen Shot 2022-12-24 at 2.11.22 PM

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:

Screen Shot 2022-12-24 at 1.35.58 PM

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.

Website Stats

  • 9,480,585 views

About

All content on this site has been written by Andrew Chambers (MSc. Mathematics, IB Mathematics Examiner).

New website for International teachers

I’ve just launched a brand new maths site for international schools – over 2000 pdf pages of resources to support IB teachers.  If you are an IB teacher this could save you 200+ hours of preparation time.

Explore here!

Free HL Paper 3 Questions

P3 investigation questions and fully typed mark scheme.  Packs for both Applications students and Analysis students.

Available to download here

IB Maths Super Exploration Guide

A Super Exploration Guide with 168 pages of essential advice from a current IB examiner to ensure you get great marks on your coursework.

Available to download here.

Recent Posts

Follow IB Maths Resources from Intermathematics on WordPress.com