1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Deep Learning with PyTorch

Exercise

Estimating a sample

In previous exercises, you used linear layers to build networks.

Recall that the operation performed by nn.Linear() is to take an input \(X\) and apply the transformation \(W*X + b\) ,where \(W\) and \(b\) are two tensors (called the weight and bias).

A critical part of training PyTorch models is to calculate gradients of the weight and bias tensors with respect to a loss function.

In this exercise, you will calculate weight and bias tensor gradients using cross entropy loss and a sample of data.

The following tensors are provded:

  • weight: a \(2 \times 9\)-element tensor
  • bias: a \(2\)-element tensor
  • preds: a \(1 \times 2\)-element tensor containing the model predictions
  • target: a \(1 \times 2\)-element one-hot encoded tensor containing the ground-truth label

Instructions

100 XP
  • Use the criterion you have defined to calculate the loss value with respect to the predictions and target values.
  • Compute the gradients of the cross entropy loss.
  • Display the gradients of the weight and bias tensors, in that order.