# Working with Tensors

## Introduction

You can know define a simple neural network using PyTorch by subclassing the nn.Module class. Before we further discuss neural networks, in this section we will explore tensors and how they are defined and manipulated using PyTorch. Prior knowledge of numpy and its associated data types, methods, and functions is useful.

As you probably already know, the numpy package allows for working with data arrays in Python, and this package is central to using Python to work with and analyze data. In fact, many other data science packages in Python, such as pandas, relies on numpy.

Since numpy already allows for working with multidimensional arrays, why is it necessary to define a new data model as part of PyTorch? First, numpy arrays cannot be moved between devices or processed on GPUs. PyTorch tensors allow for GPU computation, which is very important for implementing deep learning. Second, numpy arrays do not allow for automatic differentiation and calculation of gradients. In order to perform backpropagation, we must be able to calculate the gradient of the loss with respect to each learned parameter using derivatives and the chain rule. PyTorch tensors allow for storing a computational graph and support automatic differentiation. This functionality is central to training models and updating weights.

Tensors are multidimensional arrays, and a wide variety of data types can be represented using this data model. This allows us to be able to apply deep learning to a variety of problems across domains. In later modules we will explore how raw data can be converted into PyTorch tensors so that they can be used as input to deep learning.

## Lists, numpy Arrays, and torch Tensors

Let’s begin with a review of generating array or array-like data in Python using lists and numpy. First, I am importing torch, torch.nn, and numpy. In the next code block, I am creating a list of lists. Remember that a list is a base Python data type or model that allows you to store values or data in multiple dimensions. They are defined using square brackets. Using the type() function, we can see that the data type of lst1 is a list.

``````import torch
import torch.nn
import numpy as np``````
``````lst1 = [[123,117,91],[201,195,217],[81, 77, 101]]
type(lst1)``````
``<class 'list'>``

In the next code block, I convert the list to a numpy array using the np.array() function. I then print some information about the array. Here is a quick review:

• type returns the data type, in this case numpy.ndarray

• shape returns the shape of the array, in this case it is two dimensional with a length of three in each dimension; the shape is returned as a tuple

• dtype returns the data type of the elements in the array, in this case the default numeric data type of int64, or a 64-bit integer, is used

• the len() function when applied to a numpy array returns the number of dimensions of the array

• ndim returns the number of dimensions, in this case 2

• size returns the number of elements in the array

``````arr1 = np.array(lst1)
print(type(arr1))``````
``<class 'numpy.ndarray'>``
``print(arr1.shape)``
``(3, 3)``
``print(arr1.dtype)``
``int32``
``print(len(arr1))``
``3``
``print(arr1.ndim)``
``2``
``print(arr1.size)``
``9``

One very convenient aspect of PyTorch tensors is that they tend to have properties and methods similar to numpy arrays, making the transition from numpy to PyTorch pretty easy and intuitive.

You can create a PyTorch tensor using torch.tensor(). Below, I have created three tensors. In the first example, a tensor is created from a list. In the second example, a tensor is created from a numpy array. The last example demonstrates using the from_numpy() function to convert a numpy array to a torch tensor.

The type() and len() functions return the same result when they are applied to a torch tensor as when they are applied to a numpy array; they return the type and number of dimensions, respectively. The shape, dtype, and ndim properties are also the same for a PyTorch tensor. Note that the data type or class of a PyTorch tensor is torch.Tensor. Also, the data type of each element is 64-bit integer, but now defined using torch as opposed to numpy.

Lastly, the numel() function returns the number of elements in the tensor, similar to the size property of a numpy array.

``````t1 = torch.tensor(lst1)
t2 = torch.tensor(arr1)
t3 = torch.from_numpy(arr1)

print(type(t1))``````
``<class 'torch.Tensor'>``
``print(t1.shape)``
``torch.Size([3, 3])``
``print(t1.dtype)``
``torch.int64``
``print(len(t1))``
``3``
``print(t1.ndim)``
``2``
``print(torch.numel(t1))``
``9``
``print(type(t2))``
``<class 'torch.Tensor'>``
``print(t2.shape)``
``torch.Size([3, 3])``
``print(t2.dtype)``
``torch.int32``
``print(len(t2))``
``3``
``print(t2.ndim)``
``2``
``print(torch.numel(t2))``
``9``
``print(type(t3))``
``<class 'torch.Tensor'>``
``print(t3.shape)``
``torch.Size([3, 3])``
``print(t3.dtype)``
``torch.int32``
``print(len(t3))``
``3``
``print(t3.ndim)``
``2``
``print(torch.numel(t3))``
``9``

## Creating torch Tensors

Also similar to numpy, PyTorch provides several functions for defining tensors.

• torch.zeros: array of zeros

• torch.ones: array of ones

• torch.arange: values defined using a start value, end value, and step size

• torch.linspace: define a specified number of evenly spaced values between two values.

In all cases, the length of each dimension is defined using a tuple.

``````t4 = torch.zeros(size=(3, 12, 12))
print(t4)``````
``````tensor([[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]],

[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]],

[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]])``````
``````t5 = torch.ones(size=(2,4,4))
print(t5)``````
``````tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],

[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])``````
``````t6 = torch.arange(start=10, end=100, step=5)
print(t6)``````
``tensor([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95])``
``````t7 = torch.linspace(start=16, end=40, steps=4)
print(t7)``````
``tensor([16., 24., 32., 40.])``

The torch.concat() function is used to merge or concatenate tensors along an axis or dimension. In the example below, I am concatenating two 1D tensors along the first dimension (0). Dimension indexing starts at 0 as opposed to 1. If you have a multidimensional tensor, you can concatenate along different dimensions as long as the shapes are consistent. For example, you could concatenate tensors with the following shapes along the third dimension since the other two dimensions have the same lengths: (256, 256, 4) and (256, 256, 3). The result would be a tensor with shape (256, 256, 7).

``````t8 = torch.concat([t6, t7], dim=0)
print(t8)``````
``````tensor([10., 15., 20., 25., 30., 35., 40., 45., 50., 55., 60., 65., 70., 75.,
80., 85., 90., 95., 16., 24., 32., 40.])``````

The torch.rand() function creates random floating point values between 0 and 1. Again, the shape of the tensor is defined using a tuple.

In contrast to torch.rand(), torch.randint() returns random integers between a defined low and high value. Lastly, torch.randn() returns random values selected from a normal or Gaussian distribution with a mean of 0 and a standard deviation of 1.

``````t9 = torch.rand(size=(2,2,2))
print(t9)``````
``````tensor([[[0.5229, 0.4110],
[0.8049, 0.4904]],

[[0.9938, 0.0144],
[0.0903, 0.2684]]])``````
``````t10 = torch.randint(low=0, high=256, size=(3,6,6))
print(t10)``````
``````tensor([[[150,  99, 226,   3, 247, 108],
[ 84, 101,  68, 200, 252,  79],
[ 47,  63, 170, 203, 177, 184],
[ 54,  44,  62, 185, 253,  45],
[132, 207, 206, 223,  67, 126],
[234,  36,  26, 224, 241, 169]],

[[232,  64,  98, 207,  29,  71],
[185, 163, 106,  77,  34, 115],
[ 97, 216, 164,  19,   4, 104],
[ 51,  74, 138, 157, 168, 148],
[130, 238, 160, 164,  66, 226],
[ 53,   8, 243,  16, 222,  62]],

[[218,  17, 155, 222,  25,  14],
[ 64,  48,  92,   6,  31,  23],
[ 17,  65, 172, 176, 132, 169],
[ 87, 177,  89, 165,   5,  67],
[161, 218,  34,  57,   3, 204],
[ 45,  91,  91, 185, 179,  58]]])``````
``````t11 = torch.randn(size=(2,4,5))
print(t11)``````
``````tensor([[[-0.4229,  1.4795,  1.0540, -0.8161, -0.4300],
[ 0.0400,  0.3073, -0.4494, -0.2889, -1.6694],
[-0.9287,  0.8235,  0.3570, -0.0346,  0.5878],
[ 1.2796, -0.0594,  1.1897, -0.4700, -1.9942]],

[[-0.9457,  0.2366, -0.4438, -1.5625, -1.6444],
[ 0.6081,  0.5879,  1.1233, -0.7612, -1.2686],
[ 1.7133,  1.5519, -1.5015, -0.6646,  1.3000],
[ 2.4743,  0.1505, -1.5281, -1.2296,  1.4443]]])``````

## torch Data Types

Similar to numpy, PyTorch has a set of defined numeric data types, which can be specified using the dtype parameter. Below, I have provided examples of some commonly used numeric data types. The default data type is 32-bit float. Other options include 64-bit float, 16-bit float, unsigned 8-bit integer, and signed 8-bit integer. Throughout the later modules, I will make notes about numeric data types. For example, some operations may expect float data while others may expect integer data.

``````t12 = torch.ones(size=(2,4,4))
print(t12.dtype)``````
``torch.float32``
``````t13 = torch.ones(size=(2,4,4), dtype=torch.float64)
print(t13.dtype)``````
``torch.float64``
``````t14 = torch.ones(size=(2,4,4), dtype=torch.float16)
print(t14.dtype)``````
``torch.float16``
``````t15 = torch.ones(size=(2,4,4), dtype=torch.uint8)
print(t15.dtype)``````
``torch.uint8``
``````t16 = torch.ones(size=(2,4,4), dtype=torch.int8)
print(t16.dtype)``````
``torch.int8``

## Reshaping Tensors

We will now explore methods for reshaping tensors. First, I instantiate a 1D tensor of 1s with a length of 32. The torch.reshape() function can be used to reshape a tensor as long as the dimensions of the new tensor can be filled with the values from the original tensor. For example, our original (32) tensor could be reshaped into any of the following shapes since the total number of elements is still 32: (2, 16), (8, 4), (4, 8), (2, 4, 4), (2, 2, 8), and (2, 2, 2, 2, 2). In the second code block below, I have reshaped the 1D tensor into a (2, 4, 4) 3D tensor.

``````arr2 = np.ones(32)
t17 = torch.tensor(arr2)
print(t17)``````
``````tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
dtype=torch.float64)``````
``````t18 = torch.reshape(t17, (2, 4, 4))
print(t18)``````
``````tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],

[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]], dtype=torch.float64)``````

You can use -1 in the tensor shape tuple if the length of the tensor in that dimension is unknown. PyTorch will then calculate the required length of that dimension to accommodate all of the elements. An error will be raised if there is no length that will accommodate all of the elements.

``````t19 = torch.reshape(t17, (-1, 4, 4))
print(t19)``````
``````tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],

[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]], dtype=torch.float64)``````
``````t20 = torch.reshape(t17, (2, 4, -1))
print(t20)``````
``````tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],

[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]], dtype=torch.float64)``````

The torch.flatten() function will convert any tensor into a 1D tensor. In other words, all dimensions will be combined to a single dimension to generate a vector.

``````t21 = torch.flatten(t17)
print(t21)``````
``````tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
dtype=torch.float64)``````

The view() method allows for representing the values stored in a tensor using a different shape. This is memory efficient since the original tensor and the view of the tensor reference the same values in memory.

``````t22 = t17.view((2,16))
print(t22)``````
``````tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]],
dtype=torch.float64)``````

## Subsetting Tensors

Subsetting or slicing out values from a larger tensor uses the same indexing and bracket notation that is used with numpy and base Python lists. Here is a review of indexing rules:

1. Indexing starts at 0 as opposed to 1

2. A colon is used to indicate a range of indices (3:8)

3. The first index is included while the last index is not

4. For multidimensional arrays, you can provide indices to subset within each dimension

5. A colon means to select all elements in that dimension

6. A number followed by a colon means to select from that index and up to the last element in that dimension

7. A colon followed by a number indicates to select all elements up to but not including that index

Work through the examples below and make sure you understand why each specific subset of the original array was returned.

``````t23 = torch.randint(low=0, high=256, size = (8,8))
print(t23)``````
``````tensor([[211, 141, 235,  11, 201,  99,   5, 208],
[ 73, 180, 218,  93, 159,  67,  75,  25],
[ 91,  89, 104,  28, 112, 192, 149,  26],
[173, 120, 214, 168,  94, 116, 209, 170],
[ 69, 255,   4, 252, 104, 255, 179,   7],
[ 63, 211, 124,  94, 176,   7,  86, 180],
[172, 184, 147, 190,  72,  52, 132,  98],
[ 81, 241, 170,  84, 183,  69, 164,  61]])``````
``````t24 = t23[:,0:4]
print(t24)``````
``````tensor([[211, 141, 235,  11],
[ 73, 180, 218,  93],
[ 91,  89, 104,  28],
[173, 120, 214, 168],
[ 69, 255,   4, 252],
[ 63, 211, 124,  94],
[172, 184, 147, 190],
[ 81, 241, 170,  84]])``````
``````t25 = t23[0:4,:]
print(t25)``````
``````tensor([[211, 141, 235,  11, 201,  99,   5, 208],
[ 73, 180, 218,  93, 159,  67,  75,  25],
[ 91,  89, 104,  28, 112, 192, 149,  26],
[173, 120, 214, 168,  94, 116, 209, 170]])``````
``````t26 = t23[0:4,0:4]
print(t26)``````
``````tensor([[211, 141, 235,  11],
[ 73, 180, 218,  93],
[ 91,  89, 104,  28],
[173, 120, 214, 168]])``````
``````t27 = t23[:,0:4]
print(t27)``````
``````tensor([[211, 141, 235,  11],
[ 73, 180, 218,  93],
[ 91,  89, 104,  28],
[173, 120, 214, 168],
[ 69, 255,   4, 252],
[ 63, 211, 124,  94],
[172, 184, 147, 190],
[ 81, 241, 170,  84]])``````
``````t28 = t23[:,3:]
print(t28)``````
``````tensor([[ 11, 201,  99,   5, 208],
[ 93, 159,  67,  75,  25],
[ 28, 112, 192, 149,  26],
[168,  94, 116, 209, 170],
[252, 104, 255, 179,   7],
[ 94, 176,   7,  86, 180],
[190,  72,  52, 132,  98],
[ 84, 183,  69, 164,  61]])``````

## Squeeze and Unsqueeze

The squeeze() and unsqueeze() methods are also useful for reshaping arrays; squeeze() is used to remove dimensions with a length of 1. For example, a tensor with shape (1, 3, 256, 256) could be converted to (3, 256, 256). Note that both of these shapes have the same number of elements.

In contrast, unsqueeze() can add a dimension with a length of 1 at a defined position. In the last two examples, I have added a new dimension with a length of 1 at the end and then at the beginning of the original tensor by specifying the dim argument.

In later modules you will see some uses of these methods, such as changing the representation of a grayscale image between a 2D and 3D tensor and adding or removing the mini-batch dimension.

``````t29 = torch.ones((1,4,4))
print(t29.shape)``````
``torch.Size([1, 4, 4])``
``````t30 = t29.squeeze()
print(t30.shape)``````
``torch.Size([4, 4])``
``````t31 = t30.unsqueeze(dim=2)
print(t31.shape)``````
``torch.Size([4, 4, 1])``
``````t32 = t30.unsqueeze(dim=0)
print(t32.shape)``````
``torch.Size([1, 4, 4])``

## Reordering Dimensions

The torch.permute() function can be used to reorder the dimensions of a tensor. One common application of this method is to convert a tensor representing a multiband image between channels-first and channels-last configurations: (Channels, Height, Width) vs. (Height, Width, Channels). This is required since not all Python packages represent images as arrays or tensors using the same ordering.

``````t33 = torch.randint(low=0, high=256, size = (3, 8, 8))
print(t33)``````
``````tensor([[[230,  97, 135, 238, 155, 222, 248, 211],
[106,  64,   0, 125, 194,  41, 156, 116],
[ 88,  31,  87,  89,  80, 222,  60,  11],
[175, 238,  75,  26, 207, 250, 144,  69],
[ 13, 192,  44, 214,  85,  21,  33, 155],
[253, 207,  37, 131,  85, 236,  66, 197],
[  7,  91,  61, 196,  39,  68, 180, 142],
[147, 243, 217, 133, 165,  19, 228, 186]],

[[102, 185, 117,  77, 247, 114, 185, 125],
[240, 141,  25, 174,  35, 198, 179, 238],
[119, 221, 191, 153,   0,  98,  10,  46],
[237, 204, 215,   0, 184,  90, 124,  83],
[144, 227, 166,  16,  34, 225, 159, 171],
[132, 217, 125, 123, 248,  68,  42,  18],
[ 99,  62,  28, 135, 240, 115, 229,  19],
[ 26, 245, 244, 186, 167, 224, 235, 173]],

[[ 15, 103, 243,  21, 255, 241,  64, 255],
[140, 224, 173,  55, 132, 159, 144, 176],
[ 45,  58, 140,  49, 158,  36, 136,  44],
[243, 179, 122, 236, 198, 103, 203, 246],
[122, 111, 225, 168, 227, 196,  62,  40],
[167,  40,  83,  73, 159, 217, 155,  95],
[ 23, 225, 164, 136,  74, 213, 228,  58],
[234, 195,  32,  76, 208,  83, 143, 124]]])``````
``````t34 = torch.permute(t33, (1,2,0))
t34``````
``````tensor([[[230, 102,  15],
[ 97, 185, 103],
[135, 117, 243],
[238,  77,  21],
[155, 247, 255],
[222, 114, 241],
[248, 185,  64],
[211, 125, 255]],

[[106, 240, 140],
[ 64, 141, 224],
[  0,  25, 173],
[125, 174,  55],
[194,  35, 132],
[ 41, 198, 159],
[156, 179, 144],
[116, 238, 176]],

[[ 88, 119,  45],
[ 31, 221,  58],
[ 87, 191, 140],
[ 89, 153,  49],
[ 80,   0, 158],
[222,  98,  36],
[ 60,  10, 136],
[ 11,  46,  44]],

[[175, 237, 243],
[238, 204, 179],
[ 75, 215, 122],
[ 26,   0, 236],
[207, 184, 198],
[250,  90, 103],
[144, 124, 203],
[ 69,  83, 246]],

[[ 13, 144, 122],
[192, 227, 111],
[ 44, 166, 225],
[214,  16, 168],
[ 85,  34, 227],
[ 21, 225, 196],
[ 33, 159,  62],
[155, 171,  40]],

[[253, 132, 167],
[207, 217,  40],
[ 37, 125,  83],
[131, 123,  73],
[ 85, 248, 159],
[236,  68, 217],
[ 66,  42, 155],
[197,  18,  95]],

[[  7,  99,  23],
[ 91,  62, 225],
[ 61,  28, 164],
[196, 135, 136],
[ 39, 240,  74],
[ 68, 115, 213],
[180, 229, 228],
[142,  19,  58]],

[[147,  26, 234],
[243, 245, 195],
[217, 244,  32],
[133, 186,  76],
[165, 167, 208],
[ 19, 224,  83],
[228, 235, 143],
[186, 173, 124]]])``````

## Operations on Tensors

A variety of mathematical operations can be performed on tensors. A series of examples are provided below using the same input data. The first few examples demonstrate adding a constant value to each element, clamping values between defined minimum and maximum values, calculating the cosine, and calculating the square root. If you need to perform a specific mathematical operation, please reference the PyTorch documentation for the required syntax.

``````t35 = torch.randint(low=0, high=256, size=(3,6,6))
print(t35)``````
``````tensor([[[ 20, 171, 180, 144, 164,  86],
[ 13,  39,  95,  20,  63, 169],
[ 64, 169, 228,  25,  74,  63],
[135,  38,  45,  80,  93, 163],
[ 47,  14,  58, 107, 118, 222],
[190, 102, 188,  29, 126,  73]],

[[106,  75,   5, 158, 115, 176],
[103,  69,  84,  75, 222, 233],
[196, 199, 137, 129,  53,  21],
[ 84,  32, 206,  55, 139,  28],
[  3, 241,  48, 141, 173,  75],
[ 67,  22, 123, 164, 239,  97]],

[[132,  54,  87, 239, 249,  77],
[127, 255, 218,  35,  49,  30],
[210,  14,  27, 253, 115,  53],
[145, 231, 171,  35, 247, 217],
[153, 148,  84, 251,  38, 139],
[166, 149,  44, 169,  12, 144]]])``````
``````t36 = torch.add(t35, 4)
print(t36)``````
``````tensor([[[ 24, 175, 184, 148, 168,  90],
[ 17,  43,  99,  24,  67, 173],
[ 68, 173, 232,  29,  78,  67],
[139,  42,  49,  84,  97, 167],
[ 51,  18,  62, 111, 122, 226],
[194, 106, 192,  33, 130,  77]],

[[110,  79,   9, 162, 119, 180],
[107,  73,  88,  79, 226, 237],
[200, 203, 141, 133,  57,  25],
[ 88,  36, 210,  59, 143,  32],
[  7, 245,  52, 145, 177,  79],
[ 71,  26, 127, 168, 243, 101]],

[[136,  58,  91, 243, 253,  81],
[131, 259, 222,  39,  53,  34],
[214,  18,  31, 257, 119,  57],
[149, 235, 175,  39, 251, 221],
[157, 152,  88, 255,  42, 143],
[170, 153,  48, 173,  16, 148]]])``````
``````t37 = torch.clamp(t35, min=100,max=200)
print(t37)``````
``````tensor([[[100, 171, 180, 144, 164, 100],
[100, 100, 100, 100, 100, 169],
[100, 169, 200, 100, 100, 100],
[135, 100, 100, 100, 100, 163],
[100, 100, 100, 107, 118, 200],
[190, 102, 188, 100, 126, 100]],

[[106, 100, 100, 158, 115, 176],
[103, 100, 100, 100, 200, 200],
[196, 199, 137, 129, 100, 100],
[100, 100, 200, 100, 139, 100],
[100, 200, 100, 141, 173, 100],
[100, 100, 123, 164, 200, 100]],

[[132, 100, 100, 200, 200, 100],
[127, 200, 200, 100, 100, 100],
[200, 100, 100, 200, 115, 100],
[145, 200, 171, 100, 200, 200],
[153, 148, 100, 200, 100, 139],
[166, 149, 100, 169, 100, 144]]])``````
``````t38 = torch.cos(t35)
print(t38)``````
``````tensor([[[ 0.4081,  0.2151, -0.5985,  0.8711,  0.8038, -0.3837],
[ 0.9074,  0.2666,  0.7302,  0.4081,  0.9859,  0.7985],
[ 0.3919,  0.7985, -0.2324,  0.9912,  0.1717,  0.9859],
[-0.9961,  0.9551,  0.5253, -0.1104,  0.3174,  0.9349],
[-0.9923,  0.1367,  0.1192,  0.9828,  0.1891, -0.4949],
[ 0.0663,  0.1016,  0.8797, -0.7481,  0.9440, -0.7362]],

[[ 0.6865,  0.9218,  0.2837,  0.6055, -0.3258,  0.9975],
[-0.7822,  0.9934, -0.6800,  0.9218, -0.4949,  0.8668],
[ 0.3425, -0.4716,  0.3342, -0.9811, -0.9183, -0.5477],
[-0.6800,  0.8342,  0.2238,  0.0221,  0.7180, -0.9626],
[-0.9900, -0.6195, -0.6401, -0.9317, -0.9775,  0.9218],
[-0.5178, -1.0000, -0.8880,  0.8038,  0.9716, -0.9251]],

[[ 0.9986, -0.8293,  0.5698,  0.9716, -0.6865, -0.0310],
[ 0.2324, -0.8623, -0.3341, -0.9037,  0.3006,  0.1543],
[-0.8839,  0.1367, -0.2921, -0.1016, -0.3258, -0.9183],
[ 0.8839,  0.0928,  0.2151, -0.9037, -0.3755, -0.9736],
[-0.5914, -0.9410, -0.6800,  0.9469,  0.9551,  0.7180],
[-0.8755, -0.2237,  0.9998,  0.7985,  0.8439,  0.8711]]])``````
``````t39 = torch.sqrt(t35)
print(t39)``````
``````tensor([[[ 4.4721, 13.0767, 13.4164, 12.0000, 12.8062,  9.2736],
[ 3.6056,  6.2450,  9.7468,  4.4721,  7.9373, 13.0000],
[ 8.0000, 13.0000, 15.0997,  5.0000,  8.6023,  7.9373],
[11.6189,  6.1644,  6.7082,  8.9443,  9.6437, 12.7671],
[ 6.8557,  3.7417,  7.6158, 10.3441, 10.8628, 14.8997],
[13.7840, 10.0995, 13.7113,  5.3852, 11.2250,  8.5440]],

[[10.2956,  8.6603,  2.2361, 12.5698, 10.7238, 13.2665],
[10.1489,  8.3066,  9.1652,  8.6603, 14.8997, 15.2643],
[14.0000, 14.1067, 11.7047, 11.3578,  7.2801,  4.5826],
[ 9.1652,  5.6569, 14.3527,  7.4162, 11.7898,  5.2915],
[ 1.7321, 15.5242,  6.9282, 11.8743, 13.1529,  8.6603],
[ 8.1854,  4.6904, 11.0905, 12.8062, 15.4596,  9.8489]],

[[11.4891,  7.3485,  9.3274, 15.4596, 15.7797,  8.7750],
[11.2694, 15.9687, 14.7648,  5.9161,  7.0000,  5.4772],
[14.4914,  3.7417,  5.1962, 15.9060, 10.7238,  7.2801],
[12.0416, 15.1987, 13.0767,  5.9161, 15.7162, 14.7309],
[12.3693, 12.1655,  9.1652, 15.8430,  6.1644, 11.7898],
[12.8841, 12.2066,  6.6332, 13.0000,  3.4641, 12.0000]]])``````

You can perform element-wise addition, subtraction, multiplication, and division between two tensors as long as they have the same shape. It is possible to perform these operations on arrays of different shapes as long as they adhere to broadcasting rules. However, we will not explore broadcasting rules here since broadcasting is not generally something that is required for implementing deep learning.

``````t40 = torch.randint(low=0, high=256, size=(3,6,6))
t41 = t38+t35
print(t41)``````
``````tensor([[[ 20.4081, 171.2151, 179.4015, 144.8712, 164.8038,  85.6163],
[ 13.9074,  39.2666,  95.7302,  20.4081,  63.9859, 169.7985],
[ 64.3919, 169.7985, 227.7676,  25.9912,  74.1717,  63.9859],
[134.0039,  38.9551,  45.5253,  79.8896,  93.3174, 163.9349],
[ 46.0077,  14.1367,  58.1192, 107.9828, 118.1891, 221.5051],
[190.0663, 102.1016, 188.8797,  28.2519, 126.9440,  72.2638]],

[[106.6865,  75.9218,   5.2837, 158.6055, 114.6742, 176.9975],
[102.2178,  69.9934,  83.3200,  75.9218, 221.5051, 233.8667],
[196.3425, 198.5284, 137.3342, 128.0189,  52.0817,  20.4523],
[ 83.3200,  32.8342, 206.2238,  55.0221, 139.7180,  27.0374],
[  2.0100, 240.3805,  47.3599, 140.0683, 172.0225,  75.9218],
[ 66.4822,  21.0000, 122.1120, 164.8038, 239.9716,  96.0749]],

[[132.9986,  53.1707,  87.5697, 239.9716, 248.3135,  76.9690],
[127.2324, 254.1377, 217.6659,  34.0963,  49.3006,  30.1543],
[209.1161,  14.1367,  26.7079, 252.8984, 114.6742,  52.0817],
[145.8839, 231.0928, 171.2151,  34.0963, 246.6245, 216.0264],
[152.4086, 147.0590,  83.3200, 251.9469,  38.9551, 139.7180],
[165.1245, 148.7763,  44.9998, 169.7985,  12.8439, 144.8712]]])``````

It is possible to add or subtract a constant value or multiply or divide by a constant value on an element-wise basis as demonstrated in the code block below.

``````t42 = t35/2
print(t42)``````
``````tensor([[[ 10.0000,  85.5000,  90.0000,  72.0000,  82.0000,  43.0000],
[  6.5000,  19.5000,  47.5000,  10.0000,  31.5000,  84.5000],
[ 32.0000,  84.5000, 114.0000,  12.5000,  37.0000,  31.5000],
[ 67.5000,  19.0000,  22.5000,  40.0000,  46.5000,  81.5000],
[ 23.5000,   7.0000,  29.0000,  53.5000,  59.0000, 111.0000],
[ 95.0000,  51.0000,  94.0000,  14.5000,  63.0000,  36.5000]],

[[ 53.0000,  37.5000,   2.5000,  79.0000,  57.5000,  88.0000],
[ 51.5000,  34.5000,  42.0000,  37.5000, 111.0000, 116.5000],
[ 98.0000,  99.5000,  68.5000,  64.5000,  26.5000,  10.5000],
[ 42.0000,  16.0000, 103.0000,  27.5000,  69.5000,  14.0000],
[  1.5000, 120.5000,  24.0000,  70.5000,  86.5000,  37.5000],
[ 33.5000,  11.0000,  61.5000,  82.0000, 119.5000,  48.5000]],

[[ 66.0000,  27.0000,  43.5000, 119.5000, 124.5000,  38.5000],
[ 63.5000, 127.5000, 109.0000,  17.5000,  24.5000,  15.0000],
[105.0000,   7.0000,  13.5000, 126.5000,  57.5000,  26.5000],
[ 72.5000, 115.5000,  85.5000,  17.5000, 123.5000, 108.5000],
[ 76.5000,  74.0000,  42.0000, 125.5000,  19.0000,  69.5000],
[ 83.0000,  74.5000,  22.0000,  84.5000,   6.0000,  72.0000]]])``````

You can also use comparison operators to perform logical tests. The result will generally be a Boolean (True/False) tensor with the torch.bool data type.

``````t43 = t35 > 180
print(t43)``````
``````tensor([[[False, False, False, False, False, False],
[False, False, False, False, False, False],
[False, False,  True, False, False, False],
[False, False, False, False, False, False],
[False, False, False, False, False,  True],
[ True, False,  True, False, False, False]],

[[False, False, False, False, False, False],
[False, False, False, False,  True,  True],
[ True,  True, False, False, False, False],
[False, False,  True, False, False, False],
[False,  True, False, False, False, False],
[False, False, False, False,  True, False]],

[[False, False, False,  True,  True, False],
[False,  True,  True, False, False, False],
[ True, False, False,  True, False, False],
[False,  True, False, False,  True,  True],
[False, False, False,  True, False, False],
[False, False, False, False, False, False]]])``````
``print(t43.dtype)``
``torch.bool``

Similar to numpy, it is possible to perform linear algebra operations on tensors. This is actually very important for deep learning, machine learning, and statistical analysis, since they rely on linear algebra. Below, I have provided an example of obtaining a dot product between two matrices, which consists of element-wise multiplication followed by addition to obtain a single value.

As an example, the dot product is used to calculate the activation at each node by performing an element-wise multiplication of each input value and its associated weight and then adding the results.

``````t44 = torch.dot(t35.flatten(), t35.flatten())
t44``````
``tensor(2001863)``

## Reducing Tensors

In this last section, I will provide some examples of how tensors can be reduced. Generally, this consists of calculating a summary statistic across a defined dimension.

After creating a random tensor with shape (5, 5), I apply torch.argmax(). This function returns the index of the highest value in that dimension. With dim=0, I am finding the index of the row with the highest value within each column. For example, the largest value in the first column is 0.9339, which is in row 2 (which has an index of 1).

In the next code block, I have changed the dim argument to 1 so that I am now finding which column has the highest value within each row.

You will see this applied when you want to find which class had the highest predicted logit or probability since it offers a means to find the index, which corresponds to a class, associated with the highest logit or probability. This allows for obtaining a “hard” classification from a set of class probabilities.

``````t45 = torch.rand(size=(5,5))
print(t45)``````
``````tensor([[0.6129, 0.2621, 0.0878, 0.1633, 0.4847],
[0.3738, 0.3109, 0.2772, 0.2823, 0.2375],
[0.2812, 0.9297, 0.8929, 0.9727, 0.4219],
[0.0740, 0.7144, 0.9823, 0.7692, 0.5447],
[0.3781, 0.3590, 0.6917, 0.5759, 0.5266]])``````
``````t46 = torch.argmax(t45, dim=0)
print(t46)``````
``tensor([0, 2, 3, 2, 3])``
``````t47 = torch.argmax(t45, dim=1)
print(t47)``````
``tensor([0, 0, 3, 2, 2])``

In contrast to argmax(), amax() returns the largest value along each dimension as opposed to its associated index. Functions are also available to return a single measure or statistics for the entire tensor, such as max() and mean().

``````t48 = torch.amax(t45, dim=0)
print(t48)``````
``tensor([0.6129, 0.9297, 0.9823, 0.9727, 0.5447])``
``````t49 = torch.max(t45)
print(t49)``````
``tensor(0.9823)``
``t50 = torch.mean(torch.tensor(t45, dtype=torch.float32))``
``<string>:1: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).``
``print(t50)``
``tensor(0.4883)``

## Concluding Remarks

Now that you have a better understanding of tensors, which serve as a means to represent a wide variety of data types as inputs to neural networks, we can continue our discussion of neural networks. In the next section, we will implement linear regression using PyTorch as a means to explain stochastic gradient decent and how to train a neural network to create a model.