开始使用免费开始使用

访问模型参数

使用 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)
编辑并运行代码