PyTorch tensors: operations and CUDA basics
Create PyTorch tensors, inspect shape and device, practice arithmetic and indexing, and follow a CPU versus CUDA matrix multiplication timing example.
Player not loading? Watch on YouTube
This English lesson introduces PyTorch tensors for Python programmers. It explains vectors, matrices and higher-dimensional tensors, then creates tensors with torch.tensor(). The examples inspect size, dtype and device and show a tensor transfer to a GPU.
The arithmetic examples cover addition, in-place updates, scalar multiplication, element-wise multiplication and matrix multiplication. In-place method names end in an underscore. Matrix multiplication requires the first matrix's column count to match the second matrix's row count. An incompatible addition example raises an error; a later scalar broadcasting example shows that addition does not always require identical shapes.
The lesson reshapes a tensor while preserving its element count and demonstrates an invalid reshape. It also covers sum and mean reductions, max values and indices, boolean masking and indexing with arrays. The examples use floating-point data when calculating means. The values-and-indices behavior of max depends on the reduction form used.
The final section checks CUDA availability and presents timing examples for a large matrix multiplication on CPU and, when available, GPU. The transcript gives no numerical timings or hardware specifications and does not establish whether the timing handles asynchronous CUDA execution correctly. Treat this section as an introduction to device selection and timing, rather than a verified performance comparison.
The source does not establish whether the Python runtime is local or hosted. It leaves neural network construction for another lesson.
Source: Socratica, "PyTorch Tensors: The Data Structure Behind Deep Learning". https://www.youtube.com/watch?v=lOGd6ysc2j4