I am trying to implement a custom loss function inspired by https://arxiv.org/pdf/2305.10464.pdf. That is:
$ L(\mathbf{x}) = (1-y) \left\lVert \mathbf{x_{true} - \mathbf{x_{pred}}} \right\rVert^2 + y \left\lVert \mathbf{1} - \mathbf{x_{true} - \mathbf{x_{pred}}} \right\rVert^2 $
The idea is that the model should reconstruct normal data as closely as possible while reconstructing anomalous data poorly. Thus, the model aims to put anomalous data outside of the domain description of the normal data. The labels y are purely to help guide the model and not to be predicted (y=1 --> anomaly , y = 0 --> normal ).
Although I find the loss function to be intuitive when comparing just two data points, I feel a bit confused when implementing this in python using keras. My understanding is that the loss function is evaluated per batch (is this really the case?). For the classical MSE loss, this would mean averaging the MSE-loss for all data points within the batch (https://keras.io/api/losses/regression_losses/). With the custom loss function, I am worried that the evaluation of the loss function won't capture the "anomaly-part" of the loss function as this would be lost in the aggregation? I'm unsure how to guarantee that these subtleties are effectively captured during model training. Any insights or suggestions on this matter would be highly valued.