A Guide for Biologists on Quickly Mastering Machine Learning and AI Concepts - (iii)

A Guide for Biologists on Quickly Mastering Machine Learning and AI Concepts - (iii)

Alright class ! If you are still here, you already completed part (i) covering Scikit-learn for machine learning and became familiar with the Pytorch library for AI in part (ii). As part of the first lesson, you also watched the StatQuest videos explaining four machine learning concepts.

This lesson will be a bit different from the previous two. The tasks you completed so far were relatively easy, but the module title uses the word “master”. As you know, you cannot become a master by running codes available online. You need to understand what the code is doing and how. That requires some deep thinking as you will do in this lesson.

Also, this lesson (iii) will be split into two parts. I will leave you with a homework in the first part (this one), and then come back to complete it in the next part.

For broader context, I am trying to create a module different from the traditional AIML classes offered by CS and STAT departments. In a typical class, you will learn a lot of math first and then spend weeks to months implementing them in Scikit-learn (and later Pytorch). Also, most students are not allowed to join in, because they do not satisfy all prior math requirements. For example, here are the prerequisites for CSE446 at the University of Washington -

Prerequisites: Students entering the class should be comfortable with programming and should have a pre-existing working knowledge of linear algebra (MATH 308), vector calculus (MATH 126), probability and statistics (CSE 312/STAT390), and algorithms. For a brief refresher, we recommend that you consult the linear algebra and statistics/probability reference materials on the Textbooks page.

I can safely say that no biology student ever encountered Pytorch in these CS/stat department classes, because by the time the class reached its Pytorch phase, Mr. biologist either dropped out or changed his field to math/CS. In contrast, you already ran Pytorch implementation of linear regression in the previous lesson. That is because we took a huge shortcut here, and most likely the code did not make sense to you at all.

The goal of this lesson is to fully understand the Pytorch code, first block by block and then line by line. It will take a bit of time, but I can promise you that the effort will pay off over the long run. All Pytorch codes have similar general structures, and therefore by understanding the format of one, you can explore plenty of available online materials. Moreover, this process will help you go through a number of relevant concepts. Although we are not going deep into linear algebra and (vector) calculus, it is necessary to have somewhat superficial understanding of the concepts.

Below I outline two steps for this lesson (1. learn the concepts, 2. understand the code), but they do not come one after another. You will need to go back and forth between the concepts and the code several times.

Another thing - do not read too much into terms such as “neuron”, “intelligence” and so on. People in the computing world often choose terminology partly for its marketing appeal. You do not think about the scientist Tesla, when you ride the car with the same brand name. Similarly do not connect “neuron” in neural network with a brain cell, and know that it is simply a mathematical function. Similarly “machine learning” is more accurately described as “statistical learning”, while “artificial intelligence” and “deep learning” should be called “massively parameterized statistics”. However, “learning statistics” does not sound as glamorous as learning “artificial intelligence”.

For this task, I will recommend two video series: one from StatQuest and another from 3Blue1Brown. Both channels have several videos, but I will shortlist only a few so that you are not overwhelmed.

Let us start with StatQuest, which you are already familiar with. This channel has an excellent series on neural networks. Among all videos in the series, watch these following ones to introduce yourself to four important concepts - tensors, neural networks, gradient descent and backpropagation.

Tensors

Neural Networks

Gradient Descent

Backpropagation

Neural Networks - 3Blue1Brown

Because of the importance of neural networks, I will suggest another video before moving on to the Pytorch code.

Step 2: Understand Pytorch Code for Linear Regression

Now it is time to connect between theory and practice. You understand linear regression, you know a bit about neural networks and you have seen the Pytorch code performing linear regression using neural networks. How do they all fit together?

I will not give an answer here and instead ask you to do some work on your own. That way we will be able to compare notes in the next part of this lesson.

Class in Python

You already know how to write functions in Python, but may not be familiar with “class”. For our purpose, you can think of it as a more elaborate way to organize code into related blocks. Here is the shortest video I could find on the topic.

You can now recognize that the Pytorch code for linear regression includes a class (LinearRegressionModel) with two functions: “init” and “forward”. That block represents the neural network. Make note of the structure, because you will encounter it again and again.

Use AI as Your Personalized Code Tutor

Many people in the programming world are horrified about coders losing their natural ability to code due to using tools like ChatGPT/Claude all the time. They compare it with a kid using calculator before learning arithmetics.

I found a way to use AI tools to enhance coding ability. When I do not understand code, I often copy and paste it into ChatGPT and say - “Explain this code” or sometimes “Explain line by line”. Then I read the explanation. If some part is still unclear, I ask ChatGPT to explain in more detail.

For example, you might take one line like “optimizer.zero_grad()” and ask “What exactly does this do, and why do I need to call it before calculating the gradients?” Keep asking questions until you understand the entire linear regression code. That way you can create a personalized learning journey.

I will let all of you explore the content covered here before going through the code in the next part of this lesson.

[to be continued]


Written by M. //