Select layers in a CNN

how to select layers of a CNN by its name in pytorch

Hi, you could print your model to view all the layers present in it. And them access a particular layer using . like so -

import torch
import torch.nn as nn

class LinearModel(nn.Module):
  def __init__(self):
      super(LinearModel, self).__init__()
      self.fc1 = nn.Linear(10, 20)
      self.fc2 = nn.Linear(20, 20)
      self.fc3 = nn.Linear(20, 20)


  def forward(self, x):
      pass
      

net = LinearModel()
print(net) # printing model to see the layers
print(net.fc1) # accessing particular layers

gives -

LinearModel(
  (fc1): Linear(in_features=10, out_features=20, bias=True)
  (fc2): Linear(in_features=20, out_features=20, bias=True)
  (fc3): Linear(in_features=20, out_features=20, bias=True)
)
Linear(in_features=10, out_features=20, bias=True)