Tensors in PyTorch
摘要
This chapter introduces the reader to tensors, the foundational data structure of PyTorch. We start the chapter by introducing tensors in PyTorch, building from the simplest 1D tensors to 2D and then 3D tensors. We gradually build intuition by fixing one dimension at a time and then introduce basic operations like indexing and slicing. We give a glimpse of how tensors are stored in memory. Then we look at different ways of initializing tensors and describe the attributes of tensors. We give a bird’s-eye view of tensor operations belonging to different categories to make the reader aware of all that PyTorch has to offer in terms of tensor operations. We then describe how PyTorch’s autograd mechanism computes gradients by constructing computational graphs and also go over the high-level recipe that we typically use for model training by leveraging PyTorch’s autograd mechanism. We end the chapter with a series of exercises designed to improve your understanding of PyTorch tensors.