0
0
PyTorchml~12 mins

GPU tensors (to, cuda) in PyTorch - Model Pipeline Trace

Choose your learning style9 modes available
Model Pipeline - GPU tensors (to, cuda)

This pipeline shows how data moves from CPU memory to GPU memory using PyTorch tensors. It demonstrates creating tensors on CPU, transferring them to GPU for faster computation, performing a simple operation, and then moving results back to CPU.

Data Flow - 4 Stages
1Create tensor on CPU
N/ACreate a tensor with shape (3, 3) on CPU memory3 rows x 3 columns
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
2Transfer tensor to GPU
3 rows x 3 columnsUse .to('cuda') or .cuda() to move tensor to GPU memory3 rows x 3 columns (on GPU)
Tensor with same values but stored on GPU device
3Perform operation on GPU
3 rows x 3 columns (GPU tensor)Add 10 to each element on GPU3 rows x 3 columns (GPU tensor)
[[11, 12, 13], [14, 15, 16], [17, 18, 19]]
4Transfer result back to CPU
3 rows x 3 columns (GPU tensor)Use .to('cpu') to move tensor back to CPU memory3 rows x 3 columns (CPU tensor)
[[11, 12, 13], [14, 15, 16], [17, 18, 19]]
Training Trace - Epoch by Epoch
Loss
0.5 |****
0.4 |***
0.3 |**
0.2 |*
0.1 | 
    +------------
     Epochs 1-5
EpochLoss ↓Accuracy ↑Observation
10.450.65Initial training on CPU, loss starts moderate, accuracy low
20.300.78Training on GPU speeds up computation, loss decreases, accuracy improves
30.200.85Continued GPU training shows steady improvement
40.150.90Loss decreases further, accuracy reaches good level
50.120.92Training converges well on GPU
Prediction Trace - 4 Layers
Layer 1: Input tensor on CPU
Layer 2: Transfer tensor to GPU
Layer 3: Add 5 to each element on GPU
Layer 4: Transfer result back to CPU
Model Quiz - 3 Questions
Test your understanding
Why do we move tensors to GPU using .to('cuda') or .cuda()?
ATo make tensors smaller in size
BTo save memory on CPU
CTo speed up calculations by using GPU hardware
DTo convert tensors to a different data type
Key Insight
Moving tensors to GPU allows faster math operations, which helps training models converge quicker with better accuracy. The tensor shape stays the same; only the device changes. This is key for efficient deep learning.