Posts

A Mathematical Explanation of Gradient Descent

Image
Example of a close-to-optimal Gradient descent for a quadratic function. Credit: Dylan Introduction Although I wrote an article recently explaining Gradient Descent, I feel like a mathematical explanation of how it works would be beneficial not only to understand how it works, but also how you derive the functions.  Explanation First, we need a way to calculate how far off the model is. For our example, we will use the L2 Loss Function,  Let N represent the total amount of data points we have, i the iteration we are on, yᵢ represent the expected output, and ŷ represent the output predicted by the model.  Let's look more into ŷ. Since ŷ is the predicted value, this is the function that needs to have their parameters changed. Our goal is to change these parameters enough to minimize the loss of ŷ. For our example, let's set ŷ to be a linear line.  The values in "a" and "b" are our parameters. These will be the values we will have to change within f(x) to chang...

Top 3 Breakthroughs in Computer Vision in 2024

Image
Introduction Over the past few year, AI has become popular; however, no year matches the pace of the past few years. 2024 was an extremely crucial year for Computer Vision, with new models and breakthroughs occurring rapidly. Today, I'll be talking about the 3 biggest advancements in the Computer Vision field during 2024.  3. Vision Language Modeling Simply put, Vision Language Models, or VLMs for short, are Large Language Models (LLMs) that take an image and/or text as input. Think of ChatGPT, where you import an image and then type out a question that you have over the image. It will be able to understand both and output an answer. These are also what you would call a "multimodal model," which is a model that can understand multiple different types of sources/inputs.  Although Vision Language Models have existed for a while, they have recently become better and more accurate. However, this doesn't mean it's all sunshine an rainbows: there are still several barri...

Intro to Gradient Descent

Image
Credit: Medium Introduction In a Neural Network, there are several possibilities for what the output could be. For example, lets say we are training a Neural Network to guess the tone of language of a person, going from 0 (chill), 0.5 (decent mood) to 1.0 (angry). Well, we have to train the Neural Network to guess the passivity somehow! So, we assign random values to each neuron because we don't know what the function is to accurately guess somebody's tone. This means in the final/output layer, our probability distribution could look like,  This is a problem since it's just guessing and hasn't been able to come to a conclusion. So, we need something to train the Neural Network and tell it how far off it is. That's where Gradient Descent comes in.  How Does it Work?  First, we find the cost function, which is a fancy term that is to find the output value - expected output value square for all neurons summed. For our example, it would look like,  f(x) = (0.5-1.0)^2 + ...

Using ResNet18 To Classify Dogs

Image
Image classified using a ResNet18 architecture.  Image Source: Stanford-Dogs Dataset What is a ResNet? A ResNet, short form of “Residual Network,” created to solve the vanishing gradient (modifying the parameters does not result in an increase in performance) and exploding gradient (modifying the parameters results in large updates to the parameters and fails in the learning process) problems.  This allows us to create larger, and more deep networks. They solved this problem by using a skip connection, where they skip some layers to find as potentially more layers are unable to learn the identity well, and allows it to try and find the difference between the output and the input. Then, you add the transformed input (output of the network) to the initial input to pass back into the network again. Implementation *This code is a modification from the PyTorch website ( https://pytorch.org/hub/pytorch_vision_resnet ). You can get my modified code on GitHub ( https://github.com/smrt...

Introduction to Markov Chains

Image
Introduction Markov Chains are just a fancy way of saying what the probability between two or more things would be using current present state date. They are ran on thousands of pieces of data to estimate what the next "state" will be. Let's take below as an example,  Cite: Medium Suppose we feed the past 5 years' data on the weather into this simulation. The three big circles that we have identified are what we call "states." The arrows are the possibility for one state to go to another in a certain time interval you choose. We will be doing a one day interval. In our case, we have three states of the weather: rainy, sunny, and cloudy. The chance for the weather going from sunny to cloudy is 40% (as represented by the arrow). The chance for the weather going from sunny to rainy is 10%. It could also be sunny the next day, and that chance is 50%. Notice that the chances of the Sunny state all add up to 100%. This happens for all the other states as well.   T...

What are Convolutions?

Image
Introduction After writing my article over how to build a CNN, I forgot that I should've explained what a convolution is, in terms of image recognition.  What are Convolutions? They are essentially manners of seeing the probability of a distinct set of figures. Suppose we have 2 lists,  ["parrot","dog","cat"] ;  ["bagel","bread","cheese"] What is the probability of getting a bagel?  (3/9, or ~33%) What is the probability of getting a cat or a bagel?  (5/9, or ~56%) This is what a convolution tries to do.  In the context of image generation, we use a kernel, a matrix to look over every pixel (if you decide to keep it that way) of the image with certain values. The kernel will scan every pixel and then form a new matrice constructed with the scan of the pixels that want to be highlighted.    (It's the number 2) There is also this thing called stride when it comes to kernels. Stride is used to tell the kernel how many pix...

Using PyTorch to create a Convolutional Neural Network for Image Classification of Numbers

Image
Introduction Just a few notes before I get started: 1) Be on the lookout for overfitting in your models. Do not use to many layers, because when you eventually test it, it will be so tuned so much to your trained dataset that it will actually provide the wrong answer during your test (personal experience). 2) .to('cuda') vs .to('cpu'): if you have a compatible NVDIA graphics card with the CUDA drivers downloaded, you should check if you can use this function before proceeding with my code. If you don't have an NVDIA graphics card or don't want to, you can use ".to('cpu')" to use your CPU instead. 3) I am using the MNIST Number Dataset on Google Colab with a T4 GPU and have tested it on a RTX 3060. You can use ".to('cuda')" for either. Framing + Implementation  Our goal is to get the computer to understand integers 0-9. To do this, we will use a Convolutional Neural Network. Personally, I will be using the PyTorch library to ac...