Five(5) Interesting Functions Related To PyTorch Tensors
The torch package contains data structures for multi-dimensional tensors and defines mathematical operations over these tensors. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types, and other useful utilities.
— TORCH.HEAVISIDE
— TORCH.TRANSPOSE
— TORCH.COPYSIGN
— TORCH.DIV
— TORCH.ISIN
You can use any of the following code snippet to install pytorch depending on the operating system you are working on.
# Uncomment and run the appropriate command for your operating system, if required
# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# MacOS
# !pip install numpy torch torchvision torchaudio
To begin, we would first need to import the pytorch module this would do just that.
Explanation about example
The input and values were passed into the heaviside funtion. Based on the definition, it returns 0 if the input is less than zero, it returns that value(5) if the input is equal to zero, and finally, it returns 1 if the input is greater than zero.
Explanation about example
Building on the above explanation, the value 1.2 returns a heaviside of 0 because the input (-1.5) is less than zero. A similar principle is applied to subsequent values.
Explanation about example
The heaviside function does not take strings.
Closing comments about when to use this function
We can use this function in mathematical operations where we need to compute their heaviside values. Also, we could use this function when we need to group number within the range of 0–1.
Function 2 — torch.transpose(input, dim0, dim1)
Returns a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.
If input is a strided tensor then the resulting out tensor shares its underlying storage with the input tensor, so changing the content of one would change the content of the other.
If input is a sparse tensor then the resulting out tensor does not share the underlying storage with the input tensor.
Parameters
input (Tensor) — the input tensor.
dim0 (int) — the first dimension to be transposed
dim1 (int) — the second dimension to be transposed
Explanation about example
To transpose a matrix simply means to make the row(s) elements become the column elements an vice versa. From this example, we can see that elements from the rows have become elements in the column after transposing them.
Explanation about example
This is similar to the example above however, the major difference is that the above matrix is 2-dimentional while this is 3-dimentional. The process of transposing is quite similar.
Explanation about example
The dimentins specified must be of type int hence the error.
Closing comments about when to use this function
The torch.transpose() function is useful in finding the transpose of a given matrix.
Explanation about example
The copysign function creates a new floating-point tensor with the magnitude of input and the sign of other, elementwise.
Explanation about example
The copysign function does not recognise strings.
Function 4 — torch.div()
Divides each element of the input input by the corresponding element of other.
Parameters
input (Tensor) — the dividend
other (Tensor or Number) — the divisor
Explanation about example
Each element from the input is divided by the output(0.5) and the results is what we have as the output.
Every element in each row of the input is divided by the corresponding other(b).
The division of a number by 0 gives infinity.
Function 5 — torch.isin()
Tests if each element of elements is in test_elements. Returns a boolean tensor of the same shape as elements that is True for elements in test_elements and False otherwise.
NOTE
One of elements or test_elements can be a scalar, but not both.
Parameters
elements (Tensor or Scalar) — Input elements
test_elements (Tensor or Scalar) — Values against which to test for each input element
assume_unique (bool, optional) — If True, assumes both elements and test_elements contain unique elements, which can speed up the calculation. Default: False
invert (bool, optional) — If True, inverts the boolean return tensor, resulting in True values for elements not in test_elements. Default: False
Returns
A boolean tensor of the same shape as elements that is True for elements in test_elements and False otherwise
Here our test element contains 2 and 3. Each element from the first entry is checked if it is found in the test element and returns True if so otherwise, False is returned.
One of elements or test_elements can be a scalar, but not both.
THANK YOU FOR YOUR TIME