#Exercise: Working with Tensors in PyTorch
Objective: Create, manipulate, and analyze tensors using various methods in PyTorch. This exercise will help you understand different tensor operations, their applications, and how to leverage GPU capabilities.
Tasks:
Create Tensors:
- Create a 1-D tensor with values
[1, 2, 3, 4, 5]. - Create a 2-D tensor with shape
[3, 2]and values: - Create a tensor filled with zeros of shape
[2, 3]and another tensor filled with ones of the same shape.
- Create a 1-D tensor with values
Tensor Datatypes:
- Create a tensor with dtype
float32and values[1.5, 2.5, 3.5]. - Create a tensor with dtype
int64and values[1, 2, 3].
- Create a tensor with dtype
Basic Operations:
- Compute the sum, mean, maximum, and minimum of the 2-D tensor created in Task 1.
- Perform element-wise addition of 10 to the 1-D tensor created in Task 1.
Reshape and Manipulate Tensors:
- Reshape the 1-D tensor from Task 1 into a 2-D tensor with shape
[5, 1]. - Stack two of the tensors created in Task 1 (zeros and ones) along a new dimension to form a 3-D tensor.
- Squeeze and unsqueeze the 3-D tensor created in the previous step.
- Reshape the 1-D tensor from Task 1 into a 2-D tensor with shape
GPU Operations:
- Check if a GPU is available and move one of the tensors created to the GPU.
- Perform an operation (e.g., add a scalar) on the tensor located on the GPU.
Don't look at the solution before you have attempted to solve the exercise on your own.
