Logistic Regression - Images and Machine Learning

Previously we discussed linear regression. Which you can read more in depth in my last blog. As a refresher linear regression is tracking back the linear relationship that produces outputs when you feed a model inputs. All the data we worked with was numerical (i.e. integer/float inputs and integer/float outputs). This is just breaks the surface of what Machine Learning is capable of. In this blog post I will discuss Logistic Regression. What is it? How is it different from Linear Regression? What are the different types? Do we treat the implementations differently than we would with Linear Regression?
What is Logistic Regression?
Logistic regression is a problem where we predict the probability of a yes or no event occurring. In other words we call this classification. The reason we call it classification is self explanatory and I will highlight it in comparison with linear regression.
Classification
With linear regression the problems we solved were predicting strictly numerical outputs that could be produced with the y = mx + b equation. Out outputs would be strictly float or integer and produced as a result of multiplying inputs by a certain weight and adding a bias. Classification outputs will often be "categorical", which in other terms means they are abstract non linear descriptions of outcomes. Examples of categorical outputs would be Yes or No outputs. Labeling outputs (i.e. this picture is a dog, this picture is a handwritten "2" digit). It is the act of taking an input and tracking back patterns that uniquely identify it with a certain class. Ask yourself how you know a dog or a cat when you see it? Once you have answered this ask yourself, "How would a Machine Learning model know a picture of a cat or a dog?".
Different Types of Logistic Regression
There are 3 types of logistic regression. There is binary logistic regression which is usually in the form of yes or no. This is Multinomial logistic regression which is when there are 3 or more categories, and your input has to be classified as only one category. Lastly we have Ordinal logistic regression which is when there are 3 or more categories similarly to Multinomial. However the difference is that inputs can belong to more than one classification. For this blog and sequence of blogs I will mainly focus on binary and multinomial but feel free to read more about ordinal here.
Binary Logistic Regression
Binary Logistic Regression is a binary answer. If you recall binary is a term used to refer to 0's and 1's. It is called binary logistic regression, because the outputs are usually yes or no they can be numerically represented by 0's and 1's. Examples include:
1) Predicting loan eligibility given different variables regarding loan data
2) Whether someone has cancer given different health and environmental data
3) Whether a vehicle is a truck or a car
Multinomial Logistic Regression
Multinomial Logistic Regression is the act of uniquely classifying an input as belonging to no more than one category. This is not as self explanatory. Multi is a prefix that usually means "2-3 or more". Nomial can mean either "plural", or "name or term". Putting these two together with the appropriate root definitions you get 3 or more names, or terms. Hence there are more than a binary amount of classes, however the input can only belong to one class. Some examples are:
1) Taking an image of a handwritten digit and predicting which digit between 0-9 it is
2) Taking an image and predicting whether it is a car, plane, truck, tree, boat, etc. . .
What are the implementational differences between Log. and Lin. Regression?
Before we would import numerical data. Process it. Define a model. Define a loss function. Define an optimizer using Gradient Descent. Give it some additional parameters such as learning rate, number of epochs to train it for. As a refresher learning rate is a proportion which the optimizer adjusts our weights and biases by. Epochs are simply the amount of times to loop through/ train the model for. The model was fairly simple it would just be a simple linear model that took the inputs, multiplied them by weights, add the bias and provide us with our outputs ("predictions"). With images and logistic regression not much changes. The images are represented by matrixes of pixel intensities. The intensities correlate to a color channel. So if your color channel is just black and white (1 color channel) the intensities are the amount of black in a given pixel. If you have Red, Green and Blue (3 color channel) the intensities are the amount of Red, Green and Blue in a given pixel. We will focus on 1 color channel for simplicity sake. The model will essentially look for relationships between the pixels and provide weights to them according to each classification. So if you are working with black/white handwritten digits. It is searching for patterns in certain pixelated regions that tell it if that specific region with a certain sequence of pixel intensities have previously been classified as a 1, 2, 3, or whatever other digit. To put it simply it is searching for patterns that resemble each digit. So the model will still provide output in the same manner as our linear regression. The only difference is instead of 1 output it will need 10 outputs. Each output will be a probability that a certain image can be uniquely identified as that specific category where the categories are represented by their index. Being represented by an index simply means that in order to keep our outputs numerical instead of letting our outputs be string, or some other data type we convert them to index numbers. That way we can still use mathematical operations to train our model. In order to get our outputs in the form of a probability we have different loss functions available to us. I will go more in depth on loss functions in a separate blog, but for now all you have to know is the loss function chosen will convert our outputs to probabilities. The 10 output probabilities will add to 1. The largest probability will be chosen as the prediction. It will use the prediction and target label to calculate loss. Once loss has been calculated it can than compute gradients and utilize gradient descent to optimize our weights and biases.
Will this model be effective?
This model will learn to some extent. You will find that it does not achieve a great accuracy, and often wrongly predicts images. The reason being is that our model is still for the most part linear. Every part of manipulation our data is going through is linear. So the multiplication of weights and adding biases. The computation of loss. The calculation of gradients. We haven't taken any of the linearity away. If you try to reason in your head, classifying images is not exactly a linear operation. An example you can think about is when you look at a dog. Are you looking at it and doing linear math to figure out it is a dog. Perhaps you are identifying the dog abstractly through shapes and other relationships instead. Maybe you are doing a combination of things in your head that leads to you identifying that animal as a dog. This model right now only has the option of looking at linear relationships but the abstractness and room for variance in the data negatively affects our models max capability. In order to make our model more effective we have to provide it non linear methods to identify other relationships. I will talk about this in later blog posts.
Summary
We have discussed that logistic regression is a term for a problem we call "Classification". You have now learned about two of the most common problems in machine learning, "Regression" and "Classification". The difference is mainly in the type of data and mainly the type of output data you are working with. If data and output data is stricly numerical or float we can reason that is probably a regression problem. If data and more specifically output data is categorical we can say that we are trying to solve a classification problem. Asking yourself these questions in the future can save you a lot of time when you are trying to build models to solve problems.
Would you like to study the sources I have in order to learn what I know?
Jovian is a company that provides the free ML course I have taken that taught me:
1) The introductory Machine Learning concepts
2) How to implement these concepts through PyTorch
Masters in data science is a supplementary site that provides well constructed articles explaining Machine Learning. Read more from them here!




Comments