Learn practical skills, build real-world projects, and advance your career
Created 3 years ago
Deep Learning with PyTorch Assignment 1
For this assignment, the following PyTorch functions will be discussed along with examples.
- is_tensor - checks if the objecy is a tensor. It returns a boolean value of True or False
- numel - Counts the number of elements in a tensor
- empty - creates an empty tensor of a specified size
- full - creates a tensor of a specified size with a value for all the elements
- chunk - splits the tensor into specified chunks
- hstack - stacks the tensors horizontally
The libraries to be used are imported
import numpy as np
import torch
Function 1 - is_tensor
This function is used to return a Boolean value of True if the object is a PyTorch tensor. The only input for this function is the object checked.
Example 1 (working):
#Create a Matrix
mat1 = torch.tensor([[1,2,3.],[4,5.,6],[7,8,9],[10,11,12]])
print(mat1)
#using the function
torch.is_tensor(mat1)
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.],
[10., 11., 12.]])
True
mat1 is a tensor so the functions return a Boolean value of True
Example 2(working):