← PyTorch learning path
PyTorch · Module 2 of 10

Tensors

Create and manipulate tensors on CPU and GPU.

📖 Key ideas

  1. torch.tensor([1, 2, 3]) creates a tensor.
  2. Shapes: x.shape; reshape with x.view or x.reshape.
  3. Move to GPU: x.to("cuda").
  4. NumPy interop: torch.from_numpy, x.numpy().
  5. Operations are vectorised and broadcast like NumPy.
💡 Real-life example: Tensors are NumPy arrays that can also travel to the GPU and remember gradients.
TensorMulti-dimensional array
.to(device)Moves tensor to CPU/GPU
view/reshapeChange tensor shape
dtypeData type like float32