0
0
PyTorchml~12 mins

Reshaping (view, reshape, squeeze, unsqueeze) in PyTorch - Model Pipeline Trace

Choose your learning style9 modes available
Model Pipeline - Reshaping (view, reshape, squeeze, unsqueeze)

This pipeline shows how a tensor changes shape using PyTorch functions: view, reshape, squeeze, and unsqueeze. These operations help prepare data for models by changing dimensions without altering data values.

Data Flow - 5 Stages
1Input Tensor
1 row x 12 columnsStart with a 1D tensor of 12 elements1 row x 12 columns
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
2Reshape to 3x4
1 row x 12 columnsUse reshape(3,4) to make 3 rows and 4 columns3 rows x 4 columns
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
3View as 4x3
3 rows x 4 columnsUse view(4,3) to change shape to 4 rows and 3 columns4 rows x 3 columns
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
4Unsqueeze dimension
4 rows x 3 columnsAdd a new dimension at position 1 using unsqueeze(1)4 rows x 1 x 3 columns
[[[1, 2, 3]], [[4, 5, 6]], [[7, 8, 9]], [[10, 11, 12]]]
5Squeeze dimension
4 rows x 1 x 3 columnsRemove the dimension of size 1 at position 1 using squeeze(1)4 rows x 3 columns
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
Training Trace - Epoch by Epoch
Loss
0.5 |****
0.4 |****
0.3 |***
0.2 |**
0.1 |*
    +---------
     1 2 3 4 5 Epochs
EpochLoss ↓Accuracy ↑Observation
10.450.60Initial training with original tensor shape
20.350.70After reshaping, model learns better structure
30.280.78Continued improvement with correct input shape
40.220.83Loss decreases steadily, accuracy rises
50.180.87Model converges well with reshaped data
Prediction Trace - 5 Layers
Layer 1: Input tensor
Layer 2: reshape(3,4)
Layer 3: view(4,3)
Layer 4: unsqueeze(1)
Layer 5: squeeze(1)
Model Quiz - 3 Questions
Test your understanding
What does the PyTorch function 'unsqueeze' do to a tensor?
AChanges the order of elements in the tensor
BRemoves all dimensions of size 1
CAdds a dimension of size 1 at a specified position
DFlattens the tensor into 1D
Key Insight
Reshaping tensors correctly is crucial for machine learning models to understand data structure. Functions like view, reshape, squeeze, and unsqueeze let us change tensor shapes without changing data, enabling smooth data flow through models.