My process looks similar to this: https://fluxml.ai/assets/2019-03-05-dp-vs-rl/trebuchet-flow.png 3
I have some features and some labels. From these labels, i can invoke a custom function to check whether some target criteria match. I don’t care about the labels per se, only that the target criteria are met. Now, similar to the Trebuchet example, my ANN should infer the labels from the features.
My idea is to write a custom loss function to use the predicted features and then check the target criteria. The sum of the target criteria differences is the loss for the neural network.
In principle, the code works, but the model is not learning (loss is exactly the same in every epoch). Likely i’m breaking the graph by converting the labels to numpy, which i have to do in order to calculate the targets.
Does anyone know how to handle this “numpy interruption” (without writing a custom backward)?
Loss:
def solve_targets(x, y, ...)
    return some_numpy_calculations(x, y, ...)

class FLoss(nn.Module):
    def __init__(self, t, g, xsc, dvc):
        super(FLoss, self).__init__()
        self.targets = t
        self.device = dvc
        ...

    def forward(self, x, y):
        loss_np = solve_targets(x.cpu().data.numpy(), y.cpu().data.numpy())
        res_values = torch.tensor(loss_np, requires_grad=True, device=self.device)
        loss = torch.clamp_min_(res_values - self.targets, 0.).sum()
        print("loss", loss.item())
        return loss