user19416164
user19416164

Reputation: 11

Julia, optim.jl fails to converge, residual function

I'm currently attempting to minimize a function in Julia using the Optim.jl library. However, I'm facing some difficulties as I'm unable to obtain the desired results for some unknown reason Optim.jl fails to converge to the correct solution. Here's how the function operates: S represents an array of floats, which are the multiplicative factors I am trying to optimize. 'image' (like 'source') refers to a matrix, specifically the original scientific image I am working on, 'error' corresponds to the error matrix associated with the image. 'contaminants' is a data cube containing generic contaminants. All the images (matrices) have the same dimensions, including those in the data cube.

here the function:


function residuals(s,image,source,contaminants, error)
    c = copy(contaminants)
    sorgente = copy(source)
    
#ass is astropy
    mean,median,std = ass.stats.sigma_clipped_stats(image)
    sorgente = sorgente .*s[1] 

       #contaminants as  data-cube 
    x,y,z = size(contaminants)
        #loop 
   
   
    #multiply each contaminants for the scale-factor
    for dim in 1:z
        c[:,:,dim] = contaminants[:,:,dim] .* s[dim+1]
    end
        
    cont_sommati = dropdims(nansum(c, dims=3), dims=3)
   
    mod = cont_sommati .+ sorgente
    
    #astro is the library AstroImages.jl
    #display(astro.implot(mod.-(image .-mean)))
    
    res= nansum(((mod.-(image .-mean))./error).^2)
        return res
    
end

residui(s) = residuals(s,irac.data, radio_mod,contaminats,error_irac.data)
#initial guess

s_in = [1 for i in 1:n_contaminants+1]

#optimization
result = optimize(residui, s_in)

To better understand the situation, let's imagine we have an image and we want to remove certain aberrations or sources from it. We know the positions of these contaminants but not their intensities. So, we multiply these contaminants by a factor (a different factor for each one) and calculate the residuals. The residuals represent the differences between the original image and the modified image with the contaminants removed. By optimizing the factors applied to each contaminant, we aim to minimize these residuals and achieve a better fit between the modified image and the desired result.

After thorough testing, I can assure you that the function itself is working correctly. Therefore, the issue I am facing is likely related to the usage of the Optim.jl library.

Given that the Optim.jl library optimizes the function one parameter at a time, you have created the 'residui' function where the only free parameter is 's', the one you want to optimize. Although the optimization process using the library does not throw any errors, it fails to converge to the correct result. In such cases, there are a few possible reasons for this behavior.

I tried different methods but it won't work... BTW: all residuals are positive (for what I tested)

PS: This function is a porting of a Python function where the minimization was done via Scipy and it worked perfectly

Upvotes: 1

Views: 139

Answers (0)

Related Questions