Call/WhatsApp: +1 914 416 5343

Pytorch tensors

Write some code that uses PyTorch tensors.

Create tensors with Python lists with the following dimensions (shapes): 1, 2×3, 2x2x2. Show the tensors’ shapes.
Create a 4D tensor with random numbers between 0 and 1. Use the relevant tensor function to do so.
Create a 2D tensor with all zeros and another with all ones. Use the relevant tensor function to do so.
Using a 4D tensor and using indexing: show a single scalar; show a single vector; show the first scalar from each vector.
Demonstrate adding a scalar to every scalar in a 3D tensor (using broadcasting).
Demonstrate element-wise dividing a 3D tensor by a vector (using broadcasting).
Demonstrate the use of the item() and tolist() functions to get Python values.
Demonstrate the use of min() and mean() providing the dim= parameter to find the min/mean over a certain dimension.
Combine two 2D tensors so one is “on top” of the other.
Reshape a 2D tensor to a 3D tensor.
Convert a tensor to single-precision floating point (float32), upload to the gpu, perform an arithmetic operation with another tensor, and then download from the gpu. Demonstrate that these steps worked.