Welcome to part 2 of our PyTorch course for 2022! In this tutorial, we will be focusing on building basic neural networks using PyTorch. Neural networks are a fundamental concept in deep learning, and understanding how to build and train them is essential for working with machine learning models.
Before we get started, make sure you have PyTorch installed on your machine. If you haven’t already installed it, you can do so by following the instructions on the official PyTorch website.
Now, let’s dive into building our first neural network using PyTorch!
Step 1: Import the necessary libraries
The first step is to import the necessary libraries for building neural networks in PyTorch. We will need to import torch and torchvision for creating and working with neural networks.
import torch
import torch.nn as nn
Step 2: Define the neural network architecture
Next, we will define the architecture of our neural network. This includes specifying the number of input and output nodes, as well as the number of hidden layers and nodes in each layer.
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = nn.Linear(784, 128) # input size: 28x28=784, output size: 128
self.fc2 = nn.Linear(128, 64) # input size: 128, output size: 64
self.fc3 = nn.Linear(64, 10) # input size: 64, output size: 10 (number of classes)
def forward(self, x):
x = x.view(-1, 784) # flatten the input tensor
x = nn.functional.relu(self.fc1(x)) # apply ReLU activation function
x = nn.functional.relu(self.fc2(x)) # apply ReLU activation function
x = nn.functional.softmax(self.fc3(x), dim=1) # apply softmax activation function
return x
In this example, we are creating a simple feedforward neural network with three fully connected layers. The input size is 784 (28×28 pixels), and the output size is 10 (number of classes). We are using the ReLU activation function for the hidden layers and the softmax activation function for the output layer.
Step 3: Instantiate the neural network
Now that we have defined the neural network architecture, we can instantiate an instance of the SimpleNN class.
model = SimpleNN()
Step 4: Define the loss function and optimizer
Next, we need to define a loss function and an optimizer for training the neural network. For this example, we will use the cross-entropy loss function and the stochastic gradient descent (SGD) optimizer.
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
Step 5: Training the neural network
Now, we are ready to train the neural network. We will iterate over our training dataset and update the model’s weights based on the loss calculated by the loss function.
for epoch in range(num_epochs):
model.train()
for inputs, labels in train_loader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
In this code snippet, we are looping over the training dataset for a specified number of epochs. We are calculating the output of the model and the loss using the defined loss function. We then backpropagate the loss and update the model’s weights using the optimizer.
Step 6: Testing the neural network
After training the neural network, we can test its performance on a separate testing dataset to evaluate its accuracy.
model.eval()
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in test_loader:
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = correct / total
print(f'Accuracy: {accuracy}')
In this code snippet, we are iterating over the testing dataset and calculating the accuracy of the model by comparing the predicted labels with the ground truth labels.
Congratulations! You have now successfully built and trained a basic neural network using PyTorch. This is just the beginning, and there is so much more to explore in the world of deep learning with PyTorch. Keep practicing and experimenting with different neural network architectures and hyperparameters to improve your skills and understanding of deep learning. Good luck!
the way he said "work of art" to every beauty of machine learning depicts that he loves machine learning so much. the whole video is "work of art" by the way best ever video i have seen on youtube
The Best PyTorch video I have ever seen! THANKS!
don't lecture ML, just tell us about library
Hands down a great tutorial! Helps me a bunch on my current classes. If only the lectures at school were as good as these…
I feel like you missed the part about transposing the weight matrix before multiplying with inputs, this kind of threw me off a bit and I had to research about how the nn.Linear actually works. Also in general the way nn.Linear works is very different from how neural networks are taught in classes so it was a bit hard to grasp right off the bat. Thanks for the explanations!
thanks, its become much clearer how it works!
Thank you for such great Tutorial on Pytorch!
In the Idea,
first multiplication is by a 2×8 matrix
then by a 8×1 matrix.
10:42 Aren't they still a 2d vector? It's 8-element not 8-dimensional right?
10:42 Aren't they still a 2d vector? It's 8-element not 8-dimensional right?
Hello, I like your videos. How does `opt` know about the loss function (or value) if we didn't gave it a reference to it.
Anyone understands why the NN got "stuck" on a "wrong" output even though it reached a minimum loss function?
My understanding is it got to a local minimum point and cant get out of there. If so – is there any other way to release it? other than setting new random values to the matrices and start over.
❤🤓thank u very much
at 8:00, arent we multiplying by a 2×8 matrix rather than 8×2? Since multiplying a 1×2 vector with an 8×2 matrix is undefined.
Great tutorial, I like your style and speed!
Great class.
Keep up the good work.
Thank You,
Natasha Samuel
aren't you supposed to transpose the matrix before multiplying?
1,2 array cannot be multiplied to 8,2 matrix, but it can with matrix transpose. Am I wrong?
This was amazingly comprehensive thank you so much.
More of this please! Great tutorial as usual
Keep them coming