Train part of the network
I am new to Deep Learning and Pytorch. I have the following problem:

My overall architecture consists of a network that I define (NN1), and another pretrained network (NN2), so that the output of NN1 is the input of NN2. I want to define the loss of NN1 using the difference (RMSE) between the output of NN2 and a know ground-truth. 
I need to back-propagate through NN2 and NN1 (to train NN1), without changing NN2. 

I can use requires_grad=False on NN2, but will it disable the back-propagation through NN2? How do I specify this requirement in Pytorch?

Thanks.   

You will use two different modules. The first will be your module/model/NN (the NN1) and the second the pretrained one. 

Then, you will use model_nn1.train() and model_nn2.eval(). Then, you can do:

optimizer = torch.optim.Adam(model_nn1.parameters())  # You can use your own preferred optimizer

model_nn1.train()
model_nn2.eval()

for epoch in range(epochs):
  for x, y in dataloader:  # the dataloader is your dataloader according to the torch.utils.data.DataLoader
    optimizer.zero_grad()
    h_nn1 = model_nn1(x) # x is the input
    y_hat = model_nn2(h_nn1) # y_hat is the output
    loss = torch.sqrt(torch.mean((yhat-y)**2))
    loss.backward()
    optimizer.step()


You can check the requirement about gradient doing: 

&gt;&gt;&gt; import torch
&gt;&gt;&gt; x = torch.nn.Linear(2, 3)
&gt;&gt;&gt; x2 = torch.nn.Linear(3, 2)
&gt;&gt;&gt; z = torch.rand(2, 2)
&gt;&gt;&gt; y = torch.rand(2, 2)
&gt;&gt;&gt; x.train()
Linear(in_features=2, out_features=3, bias=True)
&gt;&gt;&gt; x2.eval()
Linear(in_features=3, out_features=2, bias=True)
&gt;&gt;&gt; h = x(z)
&gt;&gt;&gt; h.requires_grad
True
&gt;&gt;&gt; y_hat = x2(h)
&gt;&gt;&gt; y_hat.requires_grad
True

