存取模型參數
使用 nn.Sequential() 建立的 PyTorch 模型是一個包含網路不同層的模組。回想一下,你可以直接以索引方式從模型中存取各層的參數。在本練習中,你將練習如何存取神經網路中不同線性層的參數。
本練習屬於課程
使用 PyTorch 的深度學習入門
練習說明
- 存取「第一個線性」層的
weight參數。 - 存取「第二個線性」層的
bias參數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
model = nn.Sequential(nn.Linear(16, 8),
nn.Linear(8, 2)
)
# Access the weight of the first linear layer
weight_0 = ____
print("Weight of the first layer:", weight_0)
# Access the bias of the second linear layer
bias_1 = ____
print("Bias of the second layer:", bias_1)