Learn practical skills, build real-world projects, and advance your career

Assignment #1

by Christoph Hess

A short introduction about PyTorch and about the chosen functions.

  • logical_xor
  • stack
  • arange
  • reshape
  • combinations
# Import torch and other required modules
import torch

Function 1 - logical_xor

This function takes two tensors as input and returns a tensor where each tensor element was X-ORed with the matching element of the other tensor. XOR returns true (or 1) only when one of the two inputs was falsy and the other thuthy.

# Example 1 - working example of logical_xor
tens1 = torch.tensor([1., .2, 0., .5, 0.])
tens2 = torch.tensor([0, 5., 0., 10.5, .2])
torch.logical_xor(tens1, tens2)
tensor([ True, False, False, False,  True])

The returned tensor has the expected output. 1.0 XOR 0 is true as expected since one of the values is truthy while the other is falsy (0).