# Transfer Gradient so backward() of 'target' takes the same path as 'source', & only differs
    #   cuz the values of the Tensors are different
    def TransferGradient(source, target):
        assert source.shape==target.shape
        with T.no_grad():
            target.grad_fn = source.grad_fn
            target._grad_fn = source._grad_fn
            target.requires_grad = True
            return target

    # MSE Fast
    def MSE(y, labels):
        with T.no_grad():
            loss = (y - labels)**2
            loss = TransferGradient(y, loss)
        return T.mean(loss)

Hi, I would really appreciate your help.
So I would like to transfer the gradient information from one Tensor to another so that the backward() pass would be identical, and the backward() pass would only differ since the values in the first tensor are different from the values in the second. I was hoping to do this to speed up the time and save memory for area’s that can be excluded in the backward pass. Take a cost function above as an example, where I skip the gradient calculation within the cost function, but re-apply it to the loss variable at the end. So that “loss.backward()” would calculate the gradients after the MSE() function is executed.
Thank you ,
Andrew