開始使用免費開始

分類器區塊

你接下來的任務是建立一個分類器區塊,取代原本的 VGG16 分類器。你決定使用一個包含兩個全連接層(fully connected),中間有 ReLU 活化函式的區塊。

你在上一個練習中定義的 vgg_modelinput_dim 已可在工作區中使用,且已匯入 torchtorchvision.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, ____),
)
編輯並執行程式碼