question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gauss Newton Minimization Error

See original GitHub issue

I was working through the SimPEG examples trying to understand how the guts of the code work and I find the easiest way to do that is to keep it as simple as possible. So I wanted to try and solve the toy linear problem using GaussNewton (basically something where the matrix is actually being formed and inverted) so I can look at things step by step. Unfortunately, when we I set opt = Optimization.GaussNewton(maxIter=60) I get the following error:

'_CustomLinearOperator' object has no attribute 'tocsc'

The SolverWrapper assumes that the matrix being passed to it is sparse. The error occurs when the code in the wrapper tries to set the sparse matrix to a csc_sparse format.

There are a few things going on.

  1. The wrapper for direct solvers SolverWrapD assumes the system matrix is sparse.
  2. SolverWrapD is currently used for Solver which is wraps scipy.sparse.linalg.spsolve and SolverLU which wraps scipy.sparse.linalg.splu. The splu code is more restrictive and requires a csc or csr sparse matrix. spsolve accepts an ndarray or sparse matrix.
  3. SolverLU is used in many places within the code for forward modelling. Solver is only used in Optimization.GaussNewton
  4. The argument H that is passed to Solver in generated by evalfunction. Unfortunately (for this case) the function creates a linear operator. It is a relatively easy task to generate a sparse representation of H from the Linear operator.

My solution is to change the existing code

    @Utils.timeIt
    def findSearchDirection(self):
        return Solver(self.H) * (-self.g)

to

    @Utils.timeIt
    def findSearchDirection(self):
         if isinstance(self.H, sp.linalg.LinearOperator):
            self.H = sp.csr_matrix(self.H.dot(np.identity(self.g.size)))
        elif isinstance(self.H, np.ndarray):
            self.H = sp.csr_matrix(self.H)

        return Solver(self.H) * (-self.g)

but maybe the checking isn’t necessary and a simpler fix would be

    @Utils.timeIt
    def findSearchDirection(self):
        self.H = sp.csr_matrix(self.H.dot(np.identity(self.g.size)))
        return Solver(self.H) * (-self.g)

I have made a notebook that shows the error and I wrote code to replicate inv.run and was able to fix the problem.

It is here https://colab.research.google.com/drive/1iGJ7XTTLdxrb0JjJgirnsCTFMoI6hIlp

I have also managed to make this change to my version of the code and if all goes well I am going to try and make a pull request later.

Sean

This is my first attempt at an issue and PR so let me know if I’m doing it right or wrong.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
fourndocommented, Mar 28, 2019

Awesome @Rockpointgeo . @sgkang and I wrote a simpler implementation with CG solves. Check out Optimization.ProjectedGNCG, I think this should be a lot clearer, closer to what we are used too at GIF

1reaction
Rockpointgeocommented, Mar 28, 2019

Thanks. @fourndo and @lheagy . I agree with worries about dense matrices, I guess I am two decades out of date so I get a bit freaked out by inexact solvers because I never really worked on large problems that required them. As @fourndo observed I am trying to replicate my GIF experience. Perhaps I should move on but don’t like to do so without understanding what is happening.

I added the PR. In all honesty I imagine no one ever came across this because no one uses that method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gauss–Newton algorithm
The Gauss–Newton algorithm is used to solve non-linear least squares problems, which is equivalent to minimizing a sum of squared function values.
Read more >
Error in gauss-newton implementation for pose optimization
Gauss-Newton Minimization. Here is what I have done. First, I've (hopefully) “corrected” some errors: (a) dt and dR can be passed by ...
Read more >
16. Gauss–Newton method
a nonconvex optimization problem with “composite structure”: minimize. ( ()). : R → R is convex, : R → R is differentiable. Gauss–Newton...
Read more >
1 Gauss-Newton
A related approach is two-dimensional subspace minimization, which involves a constrained miminization over the two-dimensional subspace spanned ...
Read more >
Least-squares optimization and the Gauss-Newton method
The Gauss-Newton method for minimizing least-squares problems · Make an initial guess for the minimizer, x0. · Evaluate the vector F ≡ F(x)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found