开始使用免费开始使用

分类器模块

接下来,您的任务是创建一个分类器模块,用来替换原始的 VGG16 分类器。您决定使用包含两个全连接层、且中间带有 ReLU 激活函数的模块。

您在上一个练习中定义的 vgg_modelinput_dim 已在工作区可用,且已导入 torchtorchvision.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, ____),
)
编辑并运行代码