There are two things I think you could do:


subclass torch.nn.Module and write a constructor for it that takes a target tensor. You can then use target in the forward and backwards functions. Something like https://github.com/jcjohnson/pytorch-examples#pytorch-custom-nn-modules 52 but add an __init__ constructor to take the target tensor.


Write your custom layer like a loss function. You could subclass _Loss like in here https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/loss.py 37 or write a forward function that takes target as a parameter.