เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Apply pruning to a linear layer

We've trained an image classification model using a fully connected (linear) layer. Now, we're tasked with applying unstructured pruning to this layer to reduce model size without significantly impacting performance. We'll use PyTorch's l1_unstructured method for this task.

Model is pre-loaded as in model variable.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Scalable AI Models with PyTorch Lightning

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Import the pruning module from torch.nn.utils.
  • Apply l1_unstructured pruning to model[3], the final layer, and prune 30% of the weights.

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 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)
แก้ไขและรันโค้ด