Hi! I am trying to integrate the fenics loss function into a NN system in which I would want to calculate the derivative of a loss function w.r.t. y_hat where L is defined as

L = sqrt(||y_hat - y||) / sqrt(||y||)

How can I get dL/d_hat, to be used for “assemble(dL/d_hat)” with functional norms? I tried defining my own functional, how never I couldn’t define a root infront of the norm. Any pointers is very much appreciated!!

Thanks.

“”"

from dolfin import *

from fenics import *

import numpy as np

#mesh and function space

mesh = UnitSquareMesh(1, 1)

#define measure

dx = Measure(‘dx’, domain=mesh)

#define a functional

d_space = VectorFunctionSpace(mesh, “CG”, 1)

#define my own functional

u = Function (d_space)

u_hat = Function (d_space)

#set values of u and u_hat

u.vector()[:] = np.ones(d_space.dim())

u_hat.vector()[:] = np.ones(d_space.dim()) * 5

print("u: ", u.vector().get_local())

#define a functional (l2-norm of the difference) → sqrt(integral((u-u_hat)^2))

L = (u-u_hat)**2 * dx

#we want a root of the functional

#L_sqrt = ? I NEED A FUNCTIONAL WITH SQRT HERE

#compute J

L_value = assemble(L)

print ("L2: ", L_value)

##print ("J: ", J)

dLdu_hat = derivative(L, u_hat)

##compute

dLdu_hat = assemble(dLdu_hat)

print("dJdu_hat: ", dLdu_hat.get_local())

“”"

Ariana