Computer Vision News - July 2018
this weighted factor in CVXPY you need to use the parameter function, as you can see in the code below. The value of lambda isn’t known a priori as it is different for every problem. Therefore, a common approach is to evaluate several values for the lambda parameter. To do this efficiently, we will take advantage of the fact that CVXPY is a Python library to use Python’s multithreading capabilities to run parallel instances each with a different lambda value. Obviously, this capability is something that non-embedded DSLs could not provide. The get_x function below is a function that each thread will run -- it gets one lambda value and invokes a solver for finding the solution (x). Shortest path: Now we will solve the problem of finding the shortest path in a weighted graph. In this example, we will see how to use python classes to define the graph. The vertices and edges of the graph will each form a class. The Vertex class has two functions: constructor and prob . The constructor defines the variables to be optimized, and the prob function defines the specific constraint for the vertices, which is minimizing the net flow, i.e., minimizing the sum of weights passed through between source and sink. 4 Tool Computer Vision News CVXPY and Colaboratory x = Variable(n) lambda = Parameter(sign="positive") error = sum_squares(A*x - b) L1 = norm(x, 1) prob = Problem(Minimize(error + lambda*L1 )) def get_x(lambda_value): lambda.value = lambda_value result = prob.solve() return x.value gamma_vals = numpy.logspace(-4, 6) pool = multiprocessing.Pool(processes = N) x_values = pool.map(get_x, gamma_vals)
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=