Chapter 3: PyTorch Tensors

Quick exercises

face Luca Grillotti

Here are some quick exercises to apply what you have just read!

Exercise 1: Logistic Function

We define the logistic function \sigma(\cdot) as: \sigma (x) = \dfrac{1}{1 + e^{-x}}

This function presents relevant properties:

  • \forall x, \: 0 < \sigma (x) < 1
  • \lim_{x \rightarrow -\infty} \sigma (x) = 0
  • \lim_{x \rightarrow +\infty} \sigma (x) = 1

Those properties make it suitable to model probabilities.

Logistic Curve

Implement a function logistic, applying the above function to all elements of a tensor.

def logistic(tensor):
    """
    Args:
        tensor (torch.Tensor)

    Return:
        logistic_tensor (torch.Tensor) : resulting tensor after having applied the logistic function to all elements of tensor.
    """
    return ????

def logistic(tensor):
    """
    Args:
        tensor (torch.Tensor)

    Return:
        logistic_tensor (torch.Tensor) : resulting tensor after having applied the logistic function to all elements of tensor.
    """

    return 1 / (1 + torch.exp(-1 * tensor))
 

Exercise 2: Mean of Tensors

Implement a function mean_tensors that takes as input a list of tensors and returns the mean of all those tensors.

def mean_tensors(tensors_list):
    """
    Args:
        tensors_list (List[torch.Tensor]) list of tensors of the same shape

    Return:
        mean_tensors (torch.Tensor) : resulting tensor after having calculated the mean of all tensors passed as input.
    """

One solution would be:

def mean_tensors(tensors_list):
    """
    Args:
        tensors_list (List[torch.Tensor]) list of tensors of the same shape

    Return:
        mean_tensors (torch.Tensor) : resulting tensor after having calculated the mean of all tensors passed as input.
    """
    assert len(tensors_list) > 0
    sum_tensors = tensors_list[0]
    for tensor in tensors_list[1:]:
        sum_tensors += tensor
    mean = sum_tensors / len(tensors_list)
    return mean

Or if you want to be more concise:

def mean_tensors(tensors_list):
    """
    Args:
        tensors_list (List[torch.Tensor]) list of tensors of the same shape

    Return:
        mean_tensors (torch.Tensor) : resulting tensor after having calculated the mean of all tensors passed as input.
    """
    assert len(tensors_list) > 0
    mean = sum(tensors_list) / len(tensors_list)
    return mean