Two Dimensional TensorTwodimensional tensor is similar to the twodimensional metrics. A twodimensional metrics have n number of rows and n number of columns. Similarly, twodimensional tensor has n rows and n columns also. A twodimensional tensor has the following representation A gray scalar image is a twodimensional matrix of pixels. Each pixel's intensity denoted by a numeric value that ranges from 0 to 255 such that intensity value of 0 indicates no intensity something being completely black and 255 representing of maximum intensity something being completely white. We can store this twodimensional grid of values. Creating twodimensional tensorFor creating a twodimensional tensor, you have first to create a onedimensional tensor using arrange () method of the torch. This method contains two parameters of integer type. This method arranges the elements in tensor as per the given parameters. Once your onedimensional tensor is created, then our next step is to change its view in twodimensional form and store this view in the twodimensional type of variable. Let see an example of creating a two dimensional tensor Output: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8]) tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) Note: To check the dimension of tensor we have to use dim() method of tensor.Output: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8]) tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) 1 2 Accessing twodimensional tensor elementsLet see an example of twodimensional tensor to understand how to access a particular element from twodimensional tensor using index. ExampleOutput: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8]) tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) tensor(2) Tensors MultiplicationThe multiplication is done in the same manner as metrics multiplication. Tensor multiplication is done with multiplying corresponding row with the corresponding column. Tensor multiplication plays a vital role in the deep learning model. Tensors can be one dimensional, two dimensional, three dimensional, and so on. Multiplication of tensor is done only with compatible size. Let see an example of Tensor Multiplication Output: tensor([[1, 3, 5], [7, 9, 2], [4, 6, 8]]) tensor([[1, 3, 5], [7, 9, 2], [4, 6, 8]]) tensor([[ 42, 60, 51], [ 78, 114, 69], [ 78, 114, 96]]) Three Dimensional TensorThreedimensional tensor is made with the help of view () method. A threedimensional tensor has the following structure Accessing element from 3D TensorAccessing elements from the 3Dtensor is quite easy. It will be done using the index. ExampleOutput: tensor([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17]]]) tensor(10) Slicing of threedimensional tensorSegment slices are very similar to how we would slice a onedimensional tensor. Slicing a tensor means to slice the elements of a tensor into a new tensor, or we can say slicing is a process of creating a new tensor by dividing a tensor. ExampleLet we have a three dimensional tensor which contains elements from 0 to 17 and we want to slice the tensor from 6 to 11. Output: tensor([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17]]]) tensor([[ 6, 7, 8], [ 9, 10, 11]])
Next TopicGradient with PyTorch
