Thank you for the quick answer, I  have modified my code so it follows the instructions you suggested, is it correct?
for param in net1.parametsrs():
     param.requires_grad = False

optimizer = torch.optim.SGD(net2.parameters(), lr=1e-3)

for i in torch.randperm(len(train_inputs)):
     x = train_inputs[i]
     y_hat = net2(net1(x))
     loss = criterion(y[i], y_hat)
     optimizer.zero_grad()
     loss.backward()
     optimizer.step()