Search

PyTorch and NumPy Basics

Note

Advantage
Interoperability with NumPy
Popular open source library used for mathematical and scientific computing in Python. It enables efficient operations on large multi-dimensional arrays, and has a large ecosystem of supporting libraries.
MatPlotLib for plotting and visualization
OpenCV for image and video processing
Pandas for file I/O and data analysis
Compute derivatives and display gradients: here
Converting Torch tensors and NumPy arrays: here

PyTorch Tensor Basic

import torch x = torch.tensor(3.) #(1) w = torch.tensor(4., requires_grad=True) b = torch.tensor(5., requires_grad=True) y = w * x + b # Compute derivatives y.backward() # Display gradients print('dy/dx: ', x.grad) print('dy/dw: ', w.grad) print('dy/db: ', b.grad) >>> dy/dx: None #(2) >>> dy/dw: tensor(3.) >>> dy/db: tensor(1.)
Python
복사
# (1) Calculate derivative only when we need them, each gradient calculation has a certain cost and it can slow down the model in large dataset. #(2) x.grad is None, because x does not have requires_grad set to True. The ‘.grad’ function stands for gradient, another term for derivative.

NumPy Basic

import numpy as np # Create array in NumPy x = np.array([[1, 2], [3, 4]]) # Converting a NumPy array to a torch tensor (either one works) y = torch.from_numpy(x) #(1) y = torch.tensor(x) #(1) # Converting a torch tensor to a numpy array z = y.numpy() #(2) # Checking data types x.dtype, y.dtype >>> (dtype('float64'), torch.float64) #(3)
Python
복사
# ( 1 ) torch.from_numpy uses a same space and memory, whereas torch.tensor creates a copy of the data # (2) Converting PyTorch to NumPy is important as most datasets you’ll work with will likely be read and preprocessed as NumPy arrays. # (3) dtype('float64') is a NumPy specific data type, whereas torch.float64 is a PyTorch specific data type

NumPy array vs Torch tensor

PyTorch is specialized in working with GPUs
GPUs are optimized for matrices operations
NumPy is implemented for CPUs whereas PyTorch is implemented for GPUs
PyTorch helps to move data to GPUs