A Guide for Biologists on Quickly Mastering Machine Learning and AI Concepts - (ii)
In part (i) of this guide, you learned to use Python library Scikit-learn for machine learning. Today you will get familiar with Pytorch for AI.
Let me explain how the methods for learning and solving problems in AI/ML changed over the last 20 years.
If you were planning to use the algorithms in 2006, you had to start with understanding the math behind them first, and then implementing the equations on your own in Python or C.
By 2016 (i.e. 10 years back), various libraries like Scikit-learn, Tensorflow and Pytorch were available, but you had to hire a skilled coder to go through the manuals and use the relevant functions.
Fast forward to today, these libraries are a lot more accessible to biologists for two reasons. First, the documentations are fantastic as you have seen with Scikit-learn. In addition, a number of execellent books available today. Second, you can use chatGPT or similar tools to get code for your specific problem or bug fixes. For these two reasons, you are writing code in Pytorch right in the second session of this tutorial instead of spending months on math basics.
Having said that, you do need a good foundation, and there is no way around that. We are simply learning in reverse order by developing the codes first, and then using them to understand the concepts.
If I want to learn Pytorch today, here is how I would do it. I will develop codes for linear regression in four ways - Numpy shortcut, Numpy detailed method, Scikit-learn and Pytorch. Then I will compare the codes to learn AI concepts like tensor, neural unit, gradient, loss functions, etc. Let us do that.
A. Linear Regression using Numpy Function polyfit
import numpy as np
# Sample data
x = np.array([1, 2, 3])
y = np.array([2, 4, 6])
# Fit a polynomial of degree 1 (linear regression)
# Returns: slope (m) and intercept (b)
m, b = np.polyfit(x, y, 1)
print(f"Slope (m): {m:.4f}")
print(f"Intercept (b): {b:.4f}")
print(f"Equation: y = {m:.2f}x + {b:.2f}")
B. Linear Regression using Detailed Numpy
Check this tutorial.
C. Linear Regression using Scikit-learn
You already did that in part i of this guide. Here is the specific link.
D. Linear Regression using Pytorch
I got the following code from this link.
import torch
from torch.autograd import Variable
x_data = Variable(torch.Tensor([[1.0], [2.0], [3.0]]))
y_data = Variable(torch.Tensor([[2.0], [4.0], [6.0]]))
class LinearRegressionModel(torch.nn.Module):
def __init__(self):
super(LinearRegressionModel, self).__init__()
self.linear = torch.nn.Linear(1, 1) # One in and one out
def forward(self, x):
y_pred = self.linear(x)
return y_pred
# our model
our_model = LinearRegressionModel()
criterion = torch.nn.MSELoss(size_average = False)
optimizer = torch.optim.SGD(our_model.parameters(), lr = 0.01)
for epoch in range(500):
# Forward pass: Compute predicted y by passing
# x to the model
pred_y = our_model(x_data)
# Compute and print loss
loss = criterion(pred_y, y_data)
# Zero gradients, perform a backward pass,
# and update the weights.
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('epoch {}, loss {}'.format(epoch, loss.item()))
new_var = Variable(torch.Tensor([[4.0]]))
pred_y = our_model(new_var)
print("predict (after training)", 4, our_model(new_var).item())
After you make these code work, we will go to the next part of the tutorial. Once again, I would highly recommend Google collab to run them if you do not have other alternatives set up.