分类器模块
接下来,您的任务是创建一个分类器模块,用来替换原始的 VGG16 分类器。您决定使用包含两个全连接层、且中间带有 ReLU 激活函数的模块。
您在上一个练习中定义的 vgg_model 和 input_dim 已在工作区可用,且已导入 torch 和 torchvision.models。
本练习是课程的一部分
使用 PyTorch 进行图像深度学习
练习说明
- 创建一个变量
num_classes,表示类别数量,假设只需要检测猫和狗。 - 使用
nn.Sequential创建一个顺序模块。 - 创建一个线性层,并将
in_features设为input_dim。 - 在分类器的最后一层中设置输出特征数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a variable with the number of classes
____
# Create a sequential block
classifier = ____(
# Create a linear layer with input features
____(____, 512),
nn.ReLU(),
# Add the output dimension to the classifier
nn.Linear(512, ____),
)