分類器區塊
你接下來的任務是建立一個分類器區塊,取代原本的 VGG16 分類器。你決定使用一個包含兩個全連接層(fully connected),中間有 ReLU 活化函式的區塊。
你在上一個練習中定義的 vgg_model 和 input_dim 已可在工作區中使用,且已匯入 torch 與 torchvision.models。
本練習屬於課程
使用 PyTorch 進行影像深度學習
練習說明
- 建立變數
num_classes,其值為類別數;假設你只要偵測貓與狗。 - 使用
nn.Sequential建立一個 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, ____),
)