Computer Vision News - July 2018
Least-squares problem with box constraints: We shall start with a simple example, solving the least-squares problem with box constraints. In this problem, the equation b=A*X must be solved with X constrained between two limit values. This problem has applications in many fields such as nonnegative matrix factorization, image reconstruction, and more. We will find X by minimizing ||A*X – b||^2 Now let’s look at the code: Lines 1-2 import the needed libraries. Lines 3-7 define the data matrices. Now we define the problem to be solved by the solver, comprised of: Line 8 defines the objective function: sum of squares of A*X-b; Line 9 defines the box constraints. Line 10 instantiates a problem object with the defined function and constraints. Line 11 runs the solver to solve the problem instance. Line 12 prints the result. Lasso with Python built in multi-threading: The Lasso statistical solver was originally formulated to solve the least-squares problem. It uses the same sum of squares error, but adds a regularization term called the L1 penalty, commonly weighted by some factor. In order to implement CVXPY and Colaboratory 3 Tool Computer Vision News 1. import cvxpy as cp 2. import numpy as np 3. m = 30, n = 20 4. np.random.seed(1) 5. A = np.random.randn(m, n) 6. b = np.random.randn(m) 7. x = cp.Variable(n) 8. objective = cp.Minimize(cp.sum_squares(A*X - b)) 9. constraints = [0 <= x, x <= 1] 10. prob = cp.Problem(objective, constraints) 11. result = prob.solve() 12. print(x.value)
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=