jump-dev / Convex.jl

A Julia package for disciplined convex programming
https://jump.dev/Convex.jl/stable/
Other
557 stars 119 forks source link

Scalar indexing in `logisticloss` #682

Closed ericphanson closed 1 month ago

ericphanson commented 1 month ago

https://github.com/jump-dev/Convex.jl/blob/2978c32c0fcc3abc1cf10704980367cc23f8e1df/src/atoms/LogSumExpAtom.jl#L48

I guess I missed this one in #614

ericphanson commented 1 month ago

A quick fix is

function mylogisticloss(expr)
    n = length(expr)
    e = hcat(expr, zeros(n))
    # log(sum(exp(x))) <= t  <=>  sum(exp(x)) <= exp(t) <=> sum(exp(x - t)) <= 1
    t = Variable(n)
    z = sum(exp(e - t .* ones(size(e))))
    add_constraint!(t, 1 >= z)
    return sum(t)
end

but that subverts DCP since the extended formulation is only valid in a convex context, but the resulting sum(t) is affine. The proper fix therefore is probably to have a vectorized version of logsumexp as a new atom and use it here.