Tensors & Datasets Exercise

Programming for Data Science is a subject we’ve designed to explore the various programming components of data science.
Author

Juma Shafara

Published

August 29, 2024

Keywords

data science, data analysis, programming, dataidea

Photo by DATAIDEA

Week 1: Tensors and Datasets (5 Questions)

1. 1 Dimension Tensors:

  • Exercise: Create a 1D tensor with 10 elements ranging from 0 to 9. Perform the following operations: find the mean, sum, and standard deviation of the tensor. Print the results.

2. Two Dimension Tensors:

  • Exercise: Generate a 2D tensor with shape (3, 4) filled with random numbers. Perform matrix multiplication with another 2D tensor of shape (4, 2) filled with ones. Print the resulting tensor and its shape.

3. Derivatives and Graphs in PyTorch:

  • Exercise: Define a simple function \(f(x) = x^2\) in PyTorch and compute its derivative at \(x = 3\). Use PyTorch’s autograd to compute the gradient.

4. Simple Dataset:

  • Exercise: Create a custom dataset of 100 samples with 1 feature and a corresponding label using PyTorch’s Dataset class. Implement the __getitem__ and __len__ methods. Print the first 5 samples.

5. Pre Built Datasets:

  • Exercise: Load the MNIST dataset using PyTorch’s torchvision.datasets. Display the first image in the dataset along with its label.

What’s on your mind? Put it in the comments!

Back to top