I want to get the gradients of “mixup_state” which is the input of the “self.encoder”, then those concatenated with “mixup_action” are fed to the “self.discriminator”.
So, I need the gradients of disc → discriminator → encoder → state gradient
But it gives the error “RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior”. But if setting allow_unused=True, the gradient of the “mixup_state” become None.
I think it is because “mixup_action” is not the input of the encoder and when gradients are backpropagated from disciriminator to encoder. How can I get the gradient of “mixup_state”?
mixup_data = torch.cat([self.state_encoder(mixup_state), mixup_action], dim=1) mixup_state.requires_grad = True # mixup_action.requires_grad = True # either, mixup_action.detach(), all same error disc = self.discriminator(mixup_data) ones = torch.ones(disc.size()).to(disc.device) grad_state = autograd.grad( outputs=disc, inputs=mixup_state, grad_outputs=ones, create_graph=True, retain_graph=True, only_inputs=True)