What if the model was not build with
nn.Sequential? How can I extract a part of it? For instance, I have derived a class from
nn.Module and implemented its
forward() method as
def forward(self, x): x = F.conv2d(input=x, weight=self.conv1_w, bias=self.conv1_b, padding=self.conv1_pad) x = F.max_pool2d(F.relu(x), 2) x = F.conv2d(input=x, weight=self.conv2_w, bias=self.conv2_b, padding=self.conv2_pad) x = F.max_pool2d(F.relu(x), 2) x = x.view(x.size(0), -1) # Flatten but keep the same batch size x = F.linear(input=x, weight=self.fc1_w, bias=self.fc1_b) x = F.relu(x) x = F.linear(input=x, weight=self.fc2_w, bias=self.fc2_b) return x
Can I extract the model until the second convolutional layer, to see its output? That is, without doing copy-and-paste of the code.