0
0
PyTorchml~12 mins

NumPy bridge (from_numpy, numpy) in PyTorch - Model Pipeline Trace

Choose your learning style9 modes available
Model Pipeline - NumPy bridge (from_numpy, numpy)

This pipeline shows how PyTorch tensors and NumPy arrays can be converted back and forth using the NumPy bridge. It helps in using both libraries smoothly in machine learning workflows.

Data Flow - 4 Stages
1Create NumPy array
NoneCreate a NumPy array with shape (3, 3)3 rows x 3 columns
[[1 2 3] [4 5 6] [7 8 9]]
2Convert NumPy to PyTorch tensor
3 rows x 3 columnsUse torch.from_numpy() to create a tensor sharing memory3 rows x 3 columns
[[1 2 3] [4 5 6] [7 8 9]] (tensor)
3Modify PyTorch tensor
3 rows x 3 columnsAdd 1 to all elements in the tensor3 rows x 3 columns
[[2 3 4] [5 6 7] [8 9 10]] (tensor)
4Convert PyTorch tensor back to NumPy
3 rows x 3 columnsUse tensor.numpy() to get NumPy array sharing memory3 rows x 3 columns
[[2 3 4] [5 6 7] [8 9 10]]
Training Trace - Epoch by Epoch
Loss
0.5 |****
0.4 |****
0.3 |***
0.2 |**
0.1 |*
    +---------
    Epochs 1-3
EpochLoss ↓Accuracy ↑Observation
10.450.65Initial training with tensor data converted from NumPy
20.300.80Loss decreased as model learns from tensor data
30.200.90Model accuracy improves with consistent tensor inputs
Prediction Trace - 4 Layers
Layer 1: Input NumPy array
Layer 2: Convert to PyTorch tensor
Layer 3: Model prediction (dummy operation: multiply by 0.1)
Layer 4: Convert prediction tensor back to NumPy
Model Quiz - 3 Questions
Test your understanding
What happens to the data when you use torch.from_numpy()?
AA tensor is created sharing the same memory as the NumPy array
BA copy of the NumPy array is made with different memory
CThe NumPy array is deleted
DThe tensor is converted to a list
Key Insight
The NumPy bridge allows seamless data sharing between PyTorch tensors and NumPy arrays without copying data. This makes it easy to switch between PyTorch and NumPy for data processing and model training, saving memory and time.