Getting Started with Machine Learning (Part 3): An Absolute Beginner’s Guide — Intro to PyTorch and TensorFlow
Machine learning has made huge strides in the past few years and has become pervasive in many industries. But if you’re new to machine learning, it can be difficult to know where to get started. In this guide, we’ll help you navigate the basics of machine learning so that you can start using it in your own projects.
First, it’s essential to understand what machine learning is. At its core, machine learning is a type of artificial intelligence that enables machines to make predictions and decisions without explicitly being programmed. Instead, it uses algorithms to “learn” from data, identify patterns and make decisions with minimal human intervention.
Now that we’ve covered the basics, let’s dive into how to get started with machine learning. Here are some steps to follow:
1. Collect data
The first step is to take a look at your data. The data should reflect the issue you’re trying to solve. It should include enough data points to make accurate predictions.
2. Choose a Machine Learning Algorithm
Depending on your project, there are a variety of algorithms to choose from. The algorithm should reflect the issue you are trying to solve.
3. Pre-process the data
Once you’ve collected the data and your algorithm, you’ll need to pre-process it. This step might include removing unnecessary data, normalizing the data, or dealing with missing values.
4. Split the data
Next, you’ll need to split the data into training and testing sets. The training set will be used to “train” your machine learning model, while the testing set will be used to test its accuracy.
5. Train the model
Once the data is ready, it’s time to train the model. This is done by feeding the training set into your chosen machine learning algorithm and allowing it to adjust its parameters until it fits the data well.
6. Evaluate and Test the Model
Once the model is trained, you’ll need to evaluate its performance. You can run it on the testing set and compare its results with the ground truth.
7. Deploy the Model
Now that the model is trained and evaluated, you can deploy it. Depending on your project, this could entail putting the model into production on a web server, running it locally on a device, or integrating it into an existing application.
By following these steps, you’ll be well on your way to using machine learning in your projects. While it can be intimidating at first, once you’re familiar with the basics, you’ll be able to apply machine learning in various scenarios.

Linear Regression
Linear regression is a supervised machine learning algorithm that predicts a continuous target variable, such as a price or a probability. It is a linear approach, meaning it makes predictions by constructing a linear equation that predicts the response variable given the predictors.
PyTorch
PyTorch is an open-source machine learning library based on Python that is used to help with deep learning and artificial intelligence. PyTorch is used to train different types of neural networks, such as convolutional neural networks and recurrent neural networks. PyTorch can solve classification and regression problems and clustering and dimensionality reduction problems. It is a popular tool for machine learning practitioners, allowing them to quickly and easily build powerful models. With PyTorch, linear regression can be implemented in Python with the help of the torch.nn library:
import torch
import torch.nn as nn
# Define the dataset
X = torch.Tensor([[1,2],[3,4],[5,6]])
y = torch.Tensor([7,8,9])
# Define the model
model = nn.Linear(2,1)
# Train the model
# Note: MSELoss measures the mean squared error b/w each element in the
# input x and the target y where N is the batch size
criterion = nn.MSELoss()
# Note: SGD implements stochastic gradient decent (optionally with momentum)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
# Train for 100 epoch
for epoch in range(100):
pred = model(X)
loss = criterion(pred, y.unsqueeze(1))
optimizer.zero_grad()
loss.backward()
optimizer.step()TensorFlow
TensorFlow is an open-source library for machine learning that enables developers to build and deploy machine learning models in web and mobile applications. It is designed to be scalable, allowing developers to easily deploy models on various platforms, from desktop and server to mobile and edge devices. It is also designed for fast prototyping, making it easy to explore ideas before building more complex models.
TensorFlow can be used to solve a variety of machine learning problems, including supervised learning tasks such as classification and regression. supervised learning tasks aim to predict the output given the inputs. For example, in a classification task, the goal is to predict a class label given a set of features.
import tensorflow as tf
import numpy as np
# Set learning rate
learning_rate = 0.01
# Set number of loops for training all your data to update the parameters
epochs = 100
# Define training data set
x = np.linspace(0, 10, 100)
y = x + np.random.normal(0,1,100)
# Define model parameters / declare weights and bias
m = tf. Variable(0.0)
c = tf.Variable(0.0)
# Define linear model
def linreg(x):
y = m * x + c
return y
# Define loss function MSE
def sq_error(y_pred, y_true):
return tf.reduce_mean(tf.square(y_pred - y))
# Train the model
for epoch in range(epochs):
# Calculate loss with gradient tape context
with tf.GradientTape() as tape:
y_predicted = linreg(x)
loss = sq_error(y_predicted, y)
# Get gradients
gradients = tape.gradient(loss, [m,c])
# Adjust weights
m.assign_sub(gradients[0]*learning_rate)
c.assign_sub(gradients[1]*learning_rate)
# Print output
print(f'Epoch count {epoch}: Loss value: {loss.numpy()}')Machine Learning is a powerful and versatile tool that can be used to solve various problems. It involves collecting data, pre-processing it, splitting it into training and testing sets, selecting an appropriate machine learning algorithm, training the model, evaluating its performance, and deploying it. Depending on the project, there are a variety of algorithms to choose from, such as linear regression and neural networks. Popular libraries and frameworks like PyTorch and TensorFlow can help you quickly and easily implement machine learning into your project.
For additional PM and ML reading and resources (mixture of free and subscription services): Bits, Bytes, and Bots
For Education & Analytics reading and resources (mixture of free and subscription services): Education on Education
