What is a Tensor? Some common Tensor Ops.
An Explaination using PyTorch
PyTorch is a scientific package, commonly used as a replacement for traditional NumPy, with accelerated performance using the power of GPUs.
It is also a Production Ready library for developing and deploying Deep Learning models for real-world applications.
About Tensors
Tensors are the primary data-structure used in Deep Learning. Tensors are a generalization of the mathematical concept of scalars, vectors, matrices & such n-dimensional structures.
Let's not confuse tensors from Deep Learning with Tensors from Physics, or you'll go down a rabbit hole of information that will only leave you more confused.
Tensors are going to be essential for designing neural-networks. And before jumping into those, we need to make sure we understand some basic operations using Tensors. In this notebook, we'll be looking into 5 such operations on Tensors, using PyTorch.
Getting Started with Tensors
# Importing torch and other required modules
import torch
import numpy as np
# Some common ways to initializing Tensors
t1 = torch.Tensor() # Initialize an empty Tensor
t2 = torch.Tensor(2,3) # Initialize a tensor of size (2x3)
t3 = torch.tensor([10,5,6]) # Initialize tensor using a python list
t4 = torch.as_tensor([2,3]) # Initializ tensor from refering the data in list
t5 = torch.from_numpy(np.array([1,1,1])) # Initialize tensor using a np.array()
t6 = torch.rand(2,2) # Initialize tensor with random values of a uniform distrubition
t7 = torch.randn(2,2) # Initialize tensor with random values of a normal distribution
#Initilize
t1 = torch.Tensor(3,4)
print(t1.shape, '\n') # Will show you the shape of a Tensor
t1 = t1.reshape(2,6) #Example1
print(t1, '\n')
t1 = t1.reshape(1,12) #Example2
print(t1, '\n')
t1 = t1.reshape(2,3,2) #Example3
print(t1, '\n')
torch.Size([3, 4])
tensor([[8.4359e+26, 1.6457e+19, 4.4845e+30, 5.5388e-14, 1.5103e-39, 1.9090e-28],
[2.5353e+30, 2.5223e-44, 1.5947e-42, 1.4184e-39, 1.3120e-33, 2.9642e+29]])
tensor([[8.4359e+26, 1.6457e+19, 4.4845e+30, 5.5388e-14, 1.5103e-39, 1.9090e-28,
2.5353e+30, 2.5223e-44, 1.5947e-42, 1.4184e-39, 1.3120e-33, 2.9642e+29]])
tensor([[[8.4359e+26, 1.6457e+19],
[4.4845e+30, 5.5388e-14],
[1.5103e-39, 1.9090e-28]],
[[2.5353e+30, 2.5223e-44],
[1.5947e-42, 1.4184e-39],
[1.3120e-33, 2.9642e+29]]])