@albanD Thansk for your quick reply.
Here is he full contents.

According to your answer, I tried. but have some problems.
I’m not sure how to use autograd.grad.
With this codes, allow_unused=True and retain_graph=True  is required.
And got (None, None)
    optimizer_g = optim.SGD(list(encoder.parameters()), lr=0.1, momentum=0.9, weight_decay=5e-4)
    optimizer_f = optim.SGD(list(classifier.parameters()), lr=0.1, momentum=0.9, weight_decay=5e-4)
    ...
    loss = criterion(outs, labels)
    optimizer_g.zero_grad()
    optimizer_f.zero_grad()
    loss.backward(retain_graph=True)
    grads = autograd.grad(loss, classifier.parameters(), create_graph=True, allow_unused=True)
    print(grads)
    optimizer_g.step()
    optimizer_f.step()