How to display progress of scipy.optimize function?

后端 未结 7 661
醉酒成梦
醉酒成梦 2020-12-01 05:02

I use scipy.optimize to minimize a function of 12 arguments.

I started the optimization a while ago and still waiting for results.

Is there a wa

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 05:30

    Below is a solution that works for me :

    def f_(x):   # The rosenbrock function
        return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2
    
    def conjugate_gradient(x0, f):
        all_x_i = [x0[0]]
        all_y_i = [x0[1]]
        all_f_i = [f(x0)]
        def store(X):
            x, y = X
            all_x_i.append(x)
            all_y_i.append(y)
            all_f_i.append(f(X))
        optimize.minimize(f, x0, method="CG", callback=store, options={"gtol": 1e-12})
        return all_x_i, all_y_i, all_f_i
    
    

    and by example :

    conjugate_gradient([2, -1], f_)
    

    Source

提交回复
热议问题