Search⌘ K

Build a Neural Network With Pytorch

Explore how to build and program neural networks from scratch using PyTorch. Understand tensors, linear layers, and activation functions. Gain practical skills in constructing, initializing, and running simple and sequential neural networks to deepen your machine learning expertise.

PyTorch basics

In the previous lessons, we looked at some Pytorch code, but this time we will take a closer look at it.

Pytorch is an open-source Python deep learning framework that enables us to build and train neural networks. Pytorch is a library that helps you perform mathematical operations between matrices in their basic form because, as you will realize, deep learning is just simple linear algebra.

The fundamental building block of a Pytorch is the tensor. A tensor is an N-dimensional array. We can have an 1d array (or a vector) x=[1,2,3,4,5], a 2d-array y=[[1,2],[3,4]], and so on.

In Pytorch, these can be defined as:

X= torch.tensor([ 1,2,3,4,5]) 
Y= torch.tensor([1,2],[3,4]]) 

From there we can define almost all mathematical operations between tensors.

Z = torch.add(X,Y) 
Z = torch.matmul(X,Y) 
Z = 1 / (1+torch.exp(x)) 

Let’s revisit the neuron’s equation: a=f(w1a1+w2a2+w3a3+bo)a =f(w_1*a_1 + w_2*a_2 + w_3*a_3 + b_o) ...