How to obtain the structure relation of each layer of the model

How to get input and output data from the model

You’ll want something like this:

data = Variable(data).cuda()
label = Variable(label).cuda()

out = net(data)

out will also be a Variable object on the GPU in this case.


Hello, now, I only have a model file, how to know through the code, can give the complete code, sorry, I just learned

if you have a saved .pth you’ll first want to torch.load(...some file path...)

Though, many models that are distributed (incl. the torchvision ones, I believe) use a state_dict so you may need to instantiate an instance of the model class and then use model.load_state_dict(...some file path...)

If you upload your code, it will be easier to help.


The output of the first level in Pytorch is not the input of the next layer, or the output of the first two layers is the input of the next layer, etc., how do we connect the layer to layer? If so, how can we get the relation between them? Similar to the top and bottom. of caffe.

model = nn.Sequential(
    nn.Linear(100, 50),
    nn.Linear(50, 20),
    nn.Linear(20, 10)

This is the easiest way. The nn.Sequential object will collect all the modules that you pass to it and when it call its .forward() method it will feed each layer into the next. If you need to define a more complicated forward pass you can override the forward of an inherited nn.Module:

class CoolModel(nn.Module):
  def __init__(self, *args, **kwargs):
    super(CoolModel, self).__init__()
    self.layer1 = nn.Linear(100, 50)
    self.layer2 = nn.Linear(50, 20)

  def forward(self, x):
    out = self.layer1(x)
    out = self.layer2(out)
    return out

Hope that helps!


Is it possible to get topological relationships between layers in PyTorch?

I understand that we can do forward and backprop by calling forward(). What if I really need the model relationships? For example, I want to do network pruning. When I delete one Conv layer, I need to update all following layers connected to it accordingly.

1 Like