Playing with the PyUNLocBoX

https://github.com/epfl-lts2/pyunlocbox


In [ ]:
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
from pyunlocbox import functions, solvers

plt.rcParams['figure.figsize'] = (17, 5)

1 Solve an optimization problem

Following is a typical usage example who solves an optimization problem composed by the sum of two convex functions. The functions and solver objects are first instantiated with the desired parameters. The problem is then solved by a call to the solving function.


In [ ]:
f1 = functions.norm_l2(y=[4, 5, 6, 7])
f2 = functions.dummy()
solver = solvers.forward_backward()
ret = solvers.solve([f1, f2], [0., 0, 0, 0], solver, atol=1e-5)
ret['sol']

In [ ]:
plt.semilogy(np.array(ret['objective'])[:, 0], '.-');

2 Define your own objective function

In addtion to the functions available in pyunlocbox.functions, you can define your owns like below.


In [ ]:
class myfunc(functions.func):
    def __init__(self, myparam=1, **kwargs):
        self.myparam = myparam
        super(myfunc, self).__init__(**kwargs)
    def _eval(self, x):
        return 0  # Function evaluated at x.
    def _grad(self, x):
        return x  # Gradient evaluated at x, if available.
    def _prox(self, x, T):
        return x  # Proximal operator evaluated at x, if available.

In [ ]:
f = myfunc(myparam=2)
f.cap([0, 0])

Likewise, you can implement your owns solvers and acceleration schemes by sub-classing pyunlocbox.solvers.solver and pyunlocbox.acceleration.accel.

3 Tutorials

Try to follow one of our tutorials.


In [ ]:
# Your code here.

4 Playground

Or try anything that goes through your mind! Look at the reference guide to know more about the API.


In [ ]:
# Your code here.

If you miss a package, you can install it with:


In [ ]:
%pip install numpy