PyTorch Tensors — quick reference

geekgirldecodes
HowsOfCoding
4 min readJan 21, 2022

--

Hello!

This blog is part of the Torch Thursdays series. The plan is to share some tidbits on PyTorch usage through this. Today’s blog particularly is to share some quick notes based on PyTorch’s video tutorial on tensors. This blog assumes familiarity with PyTorch framework and numpy.

Tensor

PyTorch provides torch.Tensor to represent a multi-dimensional array containing elements of a single data type.

It is basically the same as a numpy array: it does not know anything about deep learning or computational graphs or gradients, and is just a generic n-dimensional array to be used for arbitrary numeric computation. The biggest difference between a numpy array and a PyTorch Tensor is that a PyTorch Tensor can run on either CPU or GPU. To run operations on the GPU, just cast the Tensor to a cuda datatype.

By default, the array elements are stored contiguously in memory leading to efficient implementations of various array processing algorithms that relay on the fast access to array elements. (note — contiguous arrays may not be efficient with sparse arrays , read more).

torch.tensor attributes

There are three attributes : torch.dtype, torch.device and torch.layout

Tensor type — torch.dtype

  • Torch tensor is created with FP32 data type by default, use dtype argument to set other types as needed (ex: int8 etc).

Making copies of tensors

  • when you copy a tensor using “ = “ assignment, it creates another reference pointing to same location under the hood (ex : if a is a torch.tensor and we perform an assignment like b = a, any update to a , will also reflect on b. Essentially, they reference to same memory allocation).
  • instead use b = a.clone(), this ensures you have made a separate copy. However, note that torch.requires_grad setting is copied as is from source tensor.
  • In case we do not wish to copy the requires_grad setting, we should use detach() on source tensor during copy, like : c = a.detach().clone()

Tensor GPU usage — using torch.device

  • check if faster hardware (GPU) is available : torch.cuda.is_available()
  • create tensor on GPU device using device argument: a = torch.rand(2,2, device=”cuda”)
  • moving data to device of interest :

Changing dimensions of tensors

  • example when you want to change dimensions : you want to work on “batch” of input passed to model rather than single instance of input.
  • use squeeze() and unsqueeze() (works on only dimension of extent 1)
  • they can also be done in-place, using squeeze_() and unsqueeze_()

(did you know? — PyTorch methods with names ending with an underscore indicate they perform in-place operations).

  • reshape
  • conv → linear (FC layer) in image classifier
  • features * h * d → 1-D vector of FC layer — use reshape() method.

numpy bridge

  • switching between numpy ndarrays and PyTorch tensor :

As always, if you found this blog useful, don’t forget to drop some claps (did you know? — you can add more than one clap :)) — so it can reach more folks. Thank you! Also, don’t forget to follow @howsofcoding and come along~

References

--

--

geekgirldecodes
HowsOfCoding

Full-time engineer, part-time procrastinator — always overdosing on coffee! 8). Author at publication : @howsofcoding