對線性層套用剪枝
我們已經使用全連接(線性)層訓練了一個影像分類模型。現在,你需要對這個層套用「非結構化剪枝」,在不明顯影響效能的前提下縮小模型大小。我們會使用 PyTorch 的 l1_unstructured 方法來完成。
模型已預先載入在變數 model 中。
本練習屬於課程
Scalable AI Models with PyTorch Lightning
練習說明
- 從
torch.nn.utils匯入剪枝模組。 - 對最後一層
model[3]套用l1_unstructured剪枝,並剪掉 30% 的權重。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import pruning module
import torch.nn.utils.prune as ____
# Before pruning
print(model)
# Apply L1 unstructured pruning to model[3]
prune.____(model[3], name="____", amount=____)
# After pruning
print(model)