Thank you for your reply. If I understand it correctly, as soon as a loss function can be expressed as a chain of linear algebra operations, the automatic differentiation can be applied (by decomposition of the loss function into a graph of individual operations). This assumption I drew from examples provided by @ ehsanmok. The latter, possibly, motivates your question regarding the placement of weight term. In short, my particular function naturally follows from the problem in hand.