The MeanFlow loss is a squared L2 error between the network output and its target, with . The paper also studies the powered loss and implements it as a squared L2 loss with a weight that adapts to the size of the error:
is the stop-gradient: the weight is treated as a constant when the loss is differentiated.
Write adaptive_weighted_loss(u, u_tgt, p=1.0, c=1e-3):
u, u_tgt: arrays of shape (B, ...). Each sample has its own squared norm (the sum of squared errors over all of its dimensions) and its own weight."sq_norm": array (B,), the values ,"weight": array (B,), the values ,"loss": a float, the mean over the batch of ,"grad_u": array with the shape of u, the gradient of "loss" with respect to u with the weights held constant.