Hi, your objective function is differentiable, but you need to code that computation like this:
from torch import exp, log

def loss(d, z, sa, gb, s, g):
    S = torch.empty(1)
    for i in range(n):
        for j in range(n):
            S += exp(s[i]+g[j])
    
    return - log(((1-d)*exp(z) + d*exp(sa + gb))/(exp(z) + S))