write loss function, trace tools and retrieve function