HomeServicesTeamTestimonialsBlog
company logo
ServicesTeamTestimonialsBlog

Machine and Deep Learning: 2023 Beginner Learning Guide

Posted on June 12, 2023
Written by Evan Hepp

It's never too late to learn something new, even a topic as vast and mind-boggling as Machine and Deep Learning. If you're just starting out on your journey to understand all of this, look no further than PyTorch. Learning PyTorch, one of the most popular open source ML and Deep Learning Python frameworks, will teach you many of the intricacies of ML and Deep Learning. In this beginner's guide for ML and Deep Learning, you can dive into the adventure today, alongside industry leaders utilizing PyTorch.

Facebook: PyTorch was developed by Facebook's AI Research lab (FAIR), and it remains widely used within the company for various research and development projects.

Tesla: Utilizes PyTorch for its autonomous driving technology and computer vision applications.

NVIDIA: A leading company in graphics processing units (GPUs) and AI technology, supports PyTorch and integrates it with their CUDA platform for accelerated deep learning. I'll share how you can use this for free later :)

Twitter: PyTorch has been adopted by Twitter for several machine learning applications, including natural language processing, recommendation systems, and anomaly detection.

OpenAI: An artificial intelligence research lab, has used PyTorch in various projects, including language models like GPT-3.

Pick Up the PyTorch

If at any point you feel overwhelmed with the concepts and new vocabulary you're hearing in the Artificial Intelligence world, I recommend three things.

1: Use AI to learn AI. For example, ask a language model like ChatGTP to clarify a term you're hearing for the first time or phrase its definition in multiple ways. Sometimes you'll notice something a little off, and you can correct a response. Evaluation will help you keep track of your own understanding as it improves.

2: Remember ML and Deep learning is experimental. Foster that mindset early!

3: Fall back into continuing to learn, write, and read PyTorch code. Reflect on how that piece fits into the bigger picture, and don't be afraid to keep moving along even if something isn't 100% clear yet.

PyTorch is the tool I recommend to light your path forward to learn the language and concepts within the domain of Machine Learning. In other words, as you learn PyTorch, you'll learn about Machine Learning which is more important. Let's look at some PyTorch code together to get an idea of what this looks like.

import torch from torch import nn import matplotlib.pyplot as plt weight = 0.8 bias = 0.2 #Create input and output data for the neural network. start = 0 end = 1 step = 0.02 x = torch.arange(start, end, step).unsqueeze(dim=1) #Input data y = weight * x + bias #Output data #Split data into training and test sets. The training set is where the model #learns patterns,test set is data the model has never seen before, #We'll evaluate how well it does later. train_split = int(0.8 * len(x)) #80% training 20% test data split x_train, y_train = x[:train_split], y[:train_split] #First 80% of the data x_test, y_test = x[train_split:], y[train_split:] #Last 20% of the data #Visualizing our current data with a function using matplotlib def plot_predictions(train_data=x_train, train_labels=y_train, test_data=x_test, test_labels=y_test, predictions=None): plt.figure(figsize=(10,7)) #plot training data in blue plt.scatter(train_data, train_labels, c="b", s=4, label="train data" ) #plot test data in green plt.scatter(test_data, test_labels, c="g", s=4, label="test data") #are there predictions? if predictions is not None: plt.scatter(test_data, predictions, c="r", s=4, label="predictions") #show the legend and graph from terminal plt.legend(prop={"size": 14}); plt.show() plot_predictions(); #no predictions made at first

So far we've just done our first step: getting data ready. Don't worry if you don't understand much from the code above. Some key parts to notice are: models need data to learn from, x and y , there may be some unfamiliar Python syntax like array access x[:train_split], there are training and testing sets (make note of the 80/20 split), visualizing data is used, and perhaps the most important piece that we'll use later to set up our model.

from torch import nn

1. What are Neural Networks? (Preparation Stage)

Zelda Zonai Crop Circle

Start with flying by and enjoying the view! If you were to walk your way through a complicated crop circle, it would be hard to know the shape it's making at each turn. In a similar way, navigating data through a neural network for the first time can be especially complicated if you haven't gotten an ariel perspective of what a neural network is.

Before you start using PyTorch, it's essential to understand the fundamentals of moving through a neural network to make data predictions. Neural networks form the backbone of many Machine Learning algorithms, and comprehending at a basic level their inner workings is crucial. A great starting point is this informative video on Neural Networks, which provides a solid conceptual foundation.

2. Python (Gather your tools and prerequisite skills)

Once you're familiar with the basics of a neural network, it's time to get practical. Python, being the language of choice for many Machine Learning practitioners, is an essential prerequisite. Ensure you have a good grasp of Python fundamentals such as variables, data types, basic operations, accessing data in arrays, conditional statements, loops, functions and indentation syntax, file I/O, OOP, exception handling, installing modules with Conda, and getting your coding environment set up with this easy to use Python environment/package manager. You'll need this in order to import and save the tools you used on a particular project of yours, and it will also set you up for being able to share those same tools you used with someone else.

In this phase, you should also familiarize yourself with the options you have for where you'll write your ML code. You might like using Visual Studio Code, an IDE that supports many languages and offers numerous extensions and plugins that can enhance your machine learning workflow, such as IntelliSense for code completion and debugging support. A powerful tool that is popular in the community for data science project development is Jupyter Notebook so be sure to check that out too; it can be part of your initial practice of using the Anaconda data science toolkit.

The last piece of tool gathering you should be aware of is the ability to speed up deep learning with GPUs. Setting this up will depend on your computer's hardware. If you would like to begin using this PyTorch capability right away, look into Google Colab. This tool offers free GPU cloud service. If you're not interested in that route, I found setting this up locally on M1 or M2 Mac a breeze. Check it out!

print(f"PyTorch version: {torch.__version__}") # Check PyTorch has access to MPS (Metal Performance Shader, Apple's GPU architecture) print(f"Is MPS (Metal Performance Shader) built? {torch.backends.mps.is_built()}") print(f"Is MPS available? {torch.backends.mps.is_available()}") # Set the device device = "mps" if torch.backends.mps.is_available() else "cpu" print(f"Using device: {device}") # Create data and send it to the device x = torch.rand(size=(3, 4)).to(device)

Installing Python Modules with Conda

MPS for Faster Computing on Mac

This is Where the fun begins

3. PyTorch

Once you have a basic understanding of a neural network, your Python fundamentals down, and tools gathered, you'll be ready to start learning PyTorch and in turn ML and Deep Learning. The cliff hanger we ended on earlier was setting up our data and getting it ready to pass into a model. Let's look at just how easy that would be with PyTorch and a simple linear regression model.

#Model inherits from nn.Module class LinearRegressionModel(nn.Module): #almost everything in pytorch inherits from nn.Module. def __init__(self): super().__init__() self.weights = nn.Parameter(torch.randn(1, requires_grad=True, dtype=torch.float)) self.bias = nn.Parameter(torch.randn(1, requires_grad=True, dtype=torch.float)) #override forward method to define the computation in the model def forward(self, x: torch.Tensor) -> torch.Tensor: return self.weights * x + self.bias #create a random seed for our model torch.manual_seed(7) #create an instance of the model (sublcass of nn.module) my_model = LinearRegressionModel() #get predictions from x_test input data with torch.inference_mode(): y_preds = my_model(x_test) print(y_preds) #our predictions are way off at first because we need to write training code plot_predictions(predictions=y_preds) #L1loss for MEAN ABSOLUTE ERROR (define our loss function: how far off were our predictions on average?) loss_fn = nn.L1Loss() optimizer = torch.optim.SGD(params = my_model.parameters(), lr=0.01) epochs = 1000 #passes through the data 1000 times for epoch in range(epochs): my_model.train() # 1. Forward pass/forward propagation (this involves data moving through our model's forward() to make predictions on the data) y_pred = my_model(x_train) # 2. Calculate the loss loss = loss_fn(y_pred, y_train) print(f"Loss: {loss}") # 3. Optimizer zero grad optimizer.zero_grad() # 4. Loss backward - move backwards through the network for calculations loss.backward() # 5. Optimizer step - use the optimizer to adjust our model's parameters to try and improve the loss optimizer.step() #by default how the optimizer changes will accumulate through the loop so we have to zero them above in step 3. #testing my_model.eval() #turns off gradient tracking print(my.model.state_dict()) with torch.inference_mode(): y_preds_new = my_model(x_test) #trained model makes almost perfect predictions! plot_predictions(predictions=y_preds_new)

I hope you can see how PyTorch serves as an excellent tool for beginners to dive into the world of ML and Deep Learning. PyTorch allows you to understand the core concepts of neural networks, data handling, and model training. While there are some prerequisites before using it, overall it's beginner friendly. By mastering PyTorch, you will gain valuable knowledge that can be applied to other ML/Deep Learning frameworks and libraries like TensorFlow by Google. So don't hesitate to embark on your journey with PyTorch. I hope you start today.

Resource Links

Evan Hepp photo
Evan Hepp
Junior Software Engineer
company logo
ServicesTeam IntegrationStrategy and DesignCustom TeamsProof of Concept
TeamOur EmployeesCareersAbout UsOur Customers
TestimonialsAtomic ObjectSimple ThreadZEAL
Contact Us
176 W. Logan St. #335
Noblesville IN, 46060