   ## 5  Proximal Toolbox

The previous toolbox we have presented is well adapted for solving a large number of small and medium-scale sparse decomposition problems with the square loss, which is typical from the classical dictionary learning framework. We now present a new software package that is adapted for solving a wide range of possibly large-scale learning problems, with several combinations of losses and regularization terms. The method implements the proximal methods of , and includes the proximal solvers for the tree-structured regularization of , and the solver of  for general structured sparse regularization. The solver for structured sparse regularization norms includes a C++ max-flow implementation of the push-relabel algorithm of , with heuristics proposed by .

This implementation also provides robust stopping criteria based on duality gaps, which are presented in Appendix A. It can handle intercepts (unregularized variables). The general formulation that our software can solve take the form

 min w ∈ ℝp
[g(w
 ▵ =
f(w) + λψ(w)],

where f is a smooth loss function and ψ is a regularization function. When one optimizes a matrix W in ℝp × r instead of a vector w in ℝp, we will write

 min W ∈ ℝp × r
[g(W
 ▵ =
f(W) + λψ(W)].

Note that the software can possibly handle nonnegativity constraints.

We start by presenting the type of regularization implemented in the software

### 5.1  Regularization Functions

Our software can handle the following regularization functions ψ for vectors w in ℝp:

• The Tikhonov regularization: ψ(w) = 1/2||w||22.
• The 1-norm: ψ(w) = ||w||1.
• The Elastic-Net: ψ(w) = ||w||1+γ||w||22.
• The Fused-Lasso: ψ(w) = ||w||1+γ||w||222i=1p−1|wi+1wi|.
• The group Lasso: ψ(w) =gG ηg ||wg||2, where G are groups of variables.
• The group Lasso with -norm: ψ(w) =gG ηg ||wg||, where G are groups of variables.
• The sparse group Lasso: same as above but with an additional ℓ1 term.
• The tree-structured sum of 2-norms: ψ(w) =gG ηg ||wg||2, where G is a tree-structured set of groups , and the ηg are positive weights.
• The tree-structured sum of -norms: ψ(w) =gG ηg ||wg||. See 
• General sum of -norms: ψ(w) =gG ηg ||wg||, where no assumption are made on the groups G.
• The path-coding penalties of .
• the 1-constraint.
• a few undocumented ones.

Our software also handles regularization functions ψ on matrices W in ℝp × r (note that W can be transposed in these formulations). In particular,

• The 1/ℓ2-norm: ψ(W) =i=1p ||Wi||2, where Wi denotes the i-th row of W.
• The 1/ℓ-norm: ψ(W) =i=1p ||Wi||,
• The 1/ℓ2+1-norm: ψ(W) =i=1p ||Wi||2 + λ2i,j|Wij|.
• The 1/ℓ+1-norm: ψ(W) =i=1p ||Wi||2i,j|Wij|,
• The 1/ℓ-norm on rows and columns: ψ(W) =i=1p ||Wi||2j=1r||Wj||, where Wj denotes the j-th column of W.
• The multi-task tree-structured sum of -norms:
ψ(W) ▵ =
 r ∑ i=1
 ∑ g ∈ G
ηg||wgi||+ γ  ∑ g ∈ G
ηg  max j ∈ g
||Wj||,     (25)
where the first double sums is in fact a sum of independent structured norms on the columns wi of W, and the right term is a tree-structured regularization norm applied to the ℓ-norm of the rows of W, thereby inducing the tree-structured regularization at the row level. G is here a tree-structured set of groups.
• The multi-task general sum of -norms is the same as Eq. (25) except that the groups G are general overlapping groups.
• The trace norm: ψ(W) = ||W||*.

Non-convex regularizations are also implemented with the ISTA algorithm (no duality gaps are of course provided in these cases):

• The 0-pseudo-norm: ψ(w) = ||w||0.
• The rank: ψ(W) = randk(W).
• The tree-structured 0-pseudo-norm: ψ(w) =gG δwg ≠ 0.

All of these regularization terms for vectors or matrices can be coupled with nonnegativity constraints. It is also possible to add an intercept, which one wishes not to regularize, and we will include this possibility in the next sections. There are also a few hidden undocumented options which are available in the source code.

We now present 3 functions for computing proximal operators associated to the previous regularization functions.

### 5.2  Function spams.proximalFlat

This function computes the proximal operators associated to many regularization functions, for input signals U=[u1,…,un] in ℝp × n, it finds a matrix V=[v1,…,vn] in ℝp × n such that:

•  If one chooses a regularization function on vectors, for every column u of U, it computes one column v of V solving

 min v ∈ ℝp

 1 2
||uv||22 + λ ||v||0,     (26)

or

 min v ∈ ℝp

 1 2
||uv||22 + λ ||v||1,     (27)

or

 min v ∈ ℝp

 1 2
||uv||22 +
 λ 2
||v||22,     (28)

or

 min v ∈ ℝp

 1 2
||uv||22 + λ ||v||1 + λ2||v||22,     (29)

or

 min v ∈ ℝp

 1 2
||uv||22 + λ
 p−1 ∑ j=1
|vj+1ivji|+λ2 ||v||1 + λ3||v||22,     (30)

or

 min v ∈ ℝp

 1 2
||uv||22 + λ
 ∑ g ∈ T
δg(v),     (31)

where T is a tree-structured set of groups (see ), and δg(v) = 0 if vg=0 and 1 otherwise. It can also solve

 min v ∈ ℝp

 1 2
||uv||22 + λ
 ∑ g ∈ T
ηg ||vg||2,     (32)

or

 min v ∈ ℝp

 1 2
||uv||22 + λ
 ∑ g ∈ T
ηg ||vg||,     (33)

or

 min v ∈ ℝp

 1 2
||uv||22 + λ
 ∑ g ∈ G
ηg ||vg||,     (34)

where G is any kind of set of groups.

This function can also solve the following proximal operators on matrices

 min V ∈ ℝp × n

 1 2
||UV||F2 + λ
 p ∑ i=1
||Vi||2,      (35)

where Vi is the i-th row of V, or

 min V ∈ ℝp × n

 1 2
||UV||F2 + λ
 p ∑ i=1
||Vi||,      (36)

or

 min V ∈ ℝp × n

 1 2
||UV||F2 + λ
 p ∑ i=1
||Vi||2 +λ2
 p ∑ i=1
 n ∑ j=1
|Vij|,      (37)

or

 min V ∈ ℝp × n

 1 2
||UV||F2 + λ
 p ∑ i=1
||Vi||2
 p ∑ i=1
 n ∑ j=1
|Vij|,      (38)

or

 min V ∈ ℝp × n

 1 2
||UV||F2 + λ
 p ∑ i=1
||Vi||2
 n ∑ j=1
||Vj||.     (39)

where Vj is the j-th column of V.

See details below:

 # # Name: proximalFlat # # Usage: spams.proximalFlat(U,return_val_loss = False,numThreads =-1,lambda1=1.0,lambda2=0., #                    lambda3=0.,intercept=False,resetflow=False,regul="",verbose=False, #                    pos=False,clever=True,size_group=1,groups = None,transpose=False) # # Description: #     proximalFlat computes proximal operators. Depending #         on the value of regul, it computes  #          #         Given an input matrix U=[u^1,\ldots,u^n], it computes a matrix  #         V=[v^1,\ldots,v^n] such that #         if one chooses a regularization functions on vectors, it computes #         for each column u of U, a column v of V solving #         if regul='l0' #             argmin 0.5||u-v||_2^2 + lambda1||v||_0 #         if regul='l1' #             argmin 0.5||u-v||_2^2 + lambda1||v||_1 #         if regul='l2' #             argmin 0.5||u-v||_2^2 + 0.5lambda1||v||_2^2 #         if regul='elastic-net' #             argmin 0.5||u-v||_2^2 + lambda1||v||_1 + lambda1_2||v||_2^2 #         if regul='fused-lasso' #             argmin 0.5||u-v||_2^2 + lambda1 FL(v) + ... #                               ...  lambda1_2||v||_1 + lambda1_3||v||_2^2 #         if regul='linf' #             argmin 0.5||u-v||_2^2 + lambda1||v||_inf #         if regul='l1-constraint' #             argmin 0.5||u-v||_2^2 s.t. ||v||_1 <= lambda1 #         if regul='l2-not-squared' #             argmin 0.5||u-v||_2^2 + lambda1||v||_2 #         if regul='group-lasso-l2'   #             argmin 0.5||u-v||_2^2 + lambda1 sum_g ||v_g||_2  #             where the groups are either defined by groups or by size_group, #         if regul='group-lasso-linf' #             argmin 0.5||u-v||_2^2 + lambda1 sum_g ||v_g||_inf #         if regul='sparse-group-lasso-l2'   #             argmin 0.5||u-v||_2^2 + lambda1 sum_g ||v_g||_2 + lambda1_2 ||v||_1 #             where the groups are either defined by groups or by size_group, #         if regul='sparse-group-lasso-linf' #             argmin 0.5||u-v||_2^2 + lambda1 sum_g ||v_g||_inf + lambda1_2 ||v||_1 #         if regul='trace-norm-vec'  #             argmin 0.5||u-v||_2^2 + lambda1 ||mat(v)||_*  #            where mat(v) has size_group rows #             #         if one chooses a regularization function on matrices #         if regul='l1l2',  V=  #             argmin 0.5||U-V||_F^2 + lambda1||V||_{1/2} #         if regul='l1linf',  V=  #             argmin 0.5||U-V||_F^2 + lambda1||V||_{1/inf} #         if regul='l1l2+l1',  V=  #             argmin 0.5||U-V||_F^2 + lambda1||V||_{1/2} + lambda1_2||V||_{1/1} #         if regul='l1linf+l1',  V=  #             argmin 0.5||U-V||_F^2 + lambda1||V||_{1/inf} + lambda1_2||V||_{1/1} #         if regul='l1linf+row-column',  V=  #             argmin 0.5||U-V||_F^2 + lambda1||V||_{1/inf} + lambda1_2||V'||_{1/inf} #         if regul='trace-norm',  V=  #             argmin 0.5||U-V||_F^2 + lambda1||V||_* #         if regul='rank',  V=  #             argmin 0.5||U-V||_F^2 + lambda1 rank(V) #         if regul='none',  V=  #             argmin 0.5||U-V||_F^2  #              #         for all these regularizations, it is possible to enforce non-negativity constraints #         with the option pos, and to prevent the last row of U to be regularized, with #         the option intercept # # Inputs: #       U:  double m x n matrix   (input signals) #               m is the signal size #       return_val_loss:      #               if true the function will return a tuple of matrices. #       lambda1:  (regularization parameter) #       regul: (choice of regularization, see above) #       lambda2:  (optional, regularization parameter) #       lambda3:  (optional, regularization parameter) #       verbose: (optional, verbosity level, false by default) #       intercept: (optional, last row of U is not regularized, #         false by default) #       transpose: (optional, transpose the matrix in the regularization function) #       size_group: (optional, for regularization functions assuming a group #         structure). It is a scalar. When groups is not specified, it assumes #         that the groups are the sets of consecutive elements of size size_group #       groups: (int32, optional, for regularization functions assuming a group #         structure. It is an int32 vector of size m containing the group indices of the #         variables (first group is 1). #       pos: (optional, adds positivity constraints on the #         coefficients, false by default) #       numThreads: (optional, number of threads for exploiting #         multi-core / multi-cpus. By default, it takes the value -1, #         which automatically selects all the available CPUs/cores). #       resetflow:    undocumented; modify at your own risks! #       clever:    undocumented; modify at your own risks! # # Output: #       V: double m x n matrix (output coefficients) #       val_regularizer: double 1 x n vector (value of the regularization #       term at the optimum). #       val_loss:        vector of size U.shape #         alpha = spams.proximalFlat(U,return_val_loss = False,...) #         (alpha,val_loss) = spams.proximalFlat(U,return_val_loss = True,...) # # Authors: # Julien MAIRAL, 2010 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (python interface) # # Note: #     Valid values for the regularization parameter (regul) are: #       "l0", "l1", "l2", "linf", "l2-not-squared", "elastic-net", "fused-lasso", #       "group-lasso-l2", "group-lasso-linf", "sparse-group-lasso-l2", #       "sparse-group-lasso-linf", "l1l2", "l1linf", "l1l2+l1", "l1linf+l1", #       "tree-l0", "tree-l2", "tree-linf", "graph", "graph-ridge", "graph-l2", #       "multi-task-tree", "multi-task-graph", "l1linf-row-column", "trace-norm", #       "trace-norm-vec", "rank", "rank-vec", "none" #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 import spams import numpy as np param = {'numThreads' : -1,'verbose' : True,          'lambda1' : 0.1 } m = 100;n = 1000 U = np.asfortranarray(np.random.normal(size = (m,n)),dtype=myfloat) # test L0 print "\nprox l0" param['regul'] = 'l0' param['pos'] = False       # false by default param['intercept'] = False # false by default alpha = spams.proximalFlat(U,False,**param) # test L1 print "\nprox l1, intercept, positivity constraint" param['regul'] = 'l1' param['pos'] = True       # can be used with all the other regularizations param['intercept'] = True # can be used with all the other regularizations alpha = spams.proximalFlat(U,False,**param) # test L2 print "\nprox squared-l2" param['regul'] = 'l2' param['pos'] = False param['intercept'] = False alpha = spams.proximalFlat(U,False,**param) # test elastic-net print "\nprox elastic-net" param['regul'] = 'elastic-net' param['lambda2'] = 0.1 alpha = spams.proximalFlat(U,**param) # test fused-lasso print "\nprox fused lasso" param['regul'] = 'fused-lasso' param['lambda2'] = 0.1 param['lambda3'] = 0.1 alpha = spams.proximalFlat(U,**param) # test l1l2 print "\nprox mixed norm l1/l2" param['regul'] = 'l1l2' alpha = spams.proximalFlat(U,**param) # test l1linf print "\nprox mixed norm l1/linf" param['regul'] = 'l1linf' alpha = spams.proximalFlat(U,**param) # test l1l2+l1 print "\nprox mixed norm l1/l2 + l1" param['regul'] = 'l1l2+l1' param['lambda2'] = 0.1 alpha = spams.proximalFlat(U,**param) # test l1linf+l1 print "\nprox mixed norm l1/linf + l1" param['regul'] = 'l1linf+l1' param['lambda2'] = 0.1 alpha = spams.proximalFlat(U,**param) # test l1linf-row-column print "\nprox mixed norm l1/linf on rows and columns" param['regul'] = 'l1linf-row-column' param['lambda2'] = 0.1 alpha = spams.proximalFlat(U,**param) # test none print "\nprox no regularization" param['regul'] = 'none' alpha = spams.proximalFlat(U,**param)

### 5.3  Function spams.proximalTree

This function computes the proximal operators associated to tree-structured regularization functions, for input signals U=[u1,…,un] in ℝp × n, and a tree-structured set of groups , it computes a matrix V=[v1,…,vn] in ℝp × n. When one uses a regularization function on vectors, it computes a column v of V for every column u of U:

 min v ∈ ℝp

 1 2
||uv||22 + λ
 ∑ g ∈ T
ηg ||vg||2,     (40)

or

 min v ∈ ℝp

 1 2
||uv||22 + λ
 ∑ g ∈ T
ηg ||vg||,     (41)

or

 min v ∈ ℝp

 1 2
||uv||22 + λ
 ∑ g ∈ T
δg(v),     (42)

where δg(v)=0 if vg=0 and 1 otherwise (see appendix of ).

When the multi-task tree-structured regularization function is used, it solves

 min V ∈ ℝp× n

 1 2
||UV||F2 + λ
 n ∑ i=1
 ∑ g ∈ T
ηg ||vgi||+ λ2
 ∑ g ∈ T

 max j ∈ g
||vgj||,     (43)

which is a formulation presented in .

This function can also be used for computing the proximal operators addressed by spams.proximalFlat (it will just not take into account the tree structure). The way the tree is incoded is presented below, (and examples are given in the file test_ProximalTree.m, with more usage details:

 # # Name: proximalTree # # Usage: spams.proximalTree(U,tree,return_val_loss = False,numThreads =-1,lambda1=1.0,lambda2=0., #                    lambda3=0.,intercept=False,resetflow=False,regul="",verbose=False, #                    pos=False,clever=True,size_group=1,transpose=False) # # Description: #     proximalTree computes a proximal operator. Depending #         on the value of regul, it computes  #          #         Given an input matrix U=[u^1,\ldots,u^n], and a tree-structured set of groups T, #         it returns a matrix V=[v^1,\ldots,v^n]: #          #         when the regularization function is for vectors, #         for every column u of U, it compute a column v of V solving #         if regul='tree-l0' #             argmin 0.5||u-v||_2^2 + lambda1 \sum_{g \in T} \delta^g(v) #         if regul='tree-l2' #           for all i, v^i =  #             argmin 0.5||u-v||_2^2 + lambda1\sum_{g \in T} \eta_g||v_g||_2 #         if regul='tree-linf' #           for all i, v^i =  #             argmin 0.5||u-v||_2^2 + lambda1\sum_{g \in T} \eta_g||v_g||_inf #              #         when the regularization function is for matrices: #         if regul='multi-task-tree' #            V=argmin 0.5||U-V||_F^2 + lambda1 \sum_{i=1}^n\sum_{g \in T} \eta_g||v^i_g||_inf + ... #                                                lambda1_2 \sum_{g \in T} \eta_g max_{j in g}||V_j||_{inf} #                                                 #         it can also be used with any non-tree-structured regularization addressed by proximalFlat #          #         for all these regularizations, it is possible to enforce non-negativity constraints #         with the option pos, and to prevent the last row of U to be regularized, with #         the option intercept # # Inputs: #       U:  double m x n matrix   (input signals) #               m is the signal size #       tree: named list  #             with four fields, eta_g, groups, own_variables and N_own_variables. #              #             The tree structure requires a particular organization of groups and variables #                * Let us denote by N = |T|, the number of groups. #                  the groups should be ordered T={g1,g2,\ldots,gN} such that if gi is included #                  in gj, then j <= i. g1 should be the group at the root of the tree  #                  and contains every variable. #                * Every group is a set of  contiguous indices for instance  #                  gi={3,4,5} or gi={4,5,6,7} or gi={4}, but not {3,5}; #                * We define root(gi) as the indices of the variables that are in gi, #                  but not in its descendants. For instance for #                  T={ g1={1,2,3,4},g2={2,3},g3={4} }, then, root(g1)={1},  #                  root(g2)={2,3}, root(g3)={4}, #                  We assume that for all i, root(gi) is a set of contigous variables #                * We assume that the smallest of root(gi) is also the smallest index of gi. #                 #                For instance,  #                  T={ g1={1,2,3,4},g2={2,3},g3={4} }, is a valid set of groups. #                  but we can not have #                  T={ g1={1,2,3,4},g2={1,2},g3={3} }, since root(g1)={4} and 4 is not the #                  smallest element in g1. #                   #             We do not lose generality with these assumptions since they can be fullfilled for any #             tree-structured set of groups after a permutation of variables and a correct ordering of the #             groups. #             see more examples in test_ProximalTree.m of valid tree-structured sets of groups. #              #             The first fields sets the weights for every group #                tree['eta_g']            double N vector  #                 #             The next field sets inclusion relations between groups  #             (but not between groups and variables): #                tree['groups']           sparse (double or boolean) N x N matrix   #                the (i,j) entry is non-zero if and only if i is different than j and  #                gi is included in gj. #                the first column corresponds to the group at the root of the tree. #                 #             The next field define the smallest index of each group gi,  #             which is also the smallest index of root(gi) #             tree['own_variables']    int32 N vector #              #             The next field define for each group gi, the size of root(gi) #             tree['N_own_variables']  int32 N vector  #              #             examples are given in test_ProximalTree.m #              #       return_val_loss:      #               if true the function will return a tuple of matrices. #       lambda1:  (regularization parameter) #       regul: (choice of regularization, see above) #       lambda2:  (optional, regularization parameter) #       lambda3:  (optional, regularization parameter) #       verbose: (optional, verbosity level, false by default) #       intercept: (optional, last row of U is not regularized, #         false by default) #       pos: (optional, adds positivity constraints on the #         coefficients, false by default) #       transpose: (optional, transpose the matrix in the regularization function) #       size_group: (optional, for regularization functions assuming a group #         structure). It is a scalar. When groups is not specified, it assumes #         that the groups are the sets of consecutive elements of size size_group #       numThreads: (optional, number of threads for exploiting #         multi-core / multi-cpus. By default, it takes the value -1, #         which automatically selects all the available CPUs/cores). #       resetflow:    undocumented; modify at your own risks! #       clever:    undocumented; modify at your own risks! # # Output: #       V: double m x n matrix (output coefficients) #       val_regularizer: double 1 x n vector (value of the regularization #       term at the optimum). #       val_loss:        vector of size U.shape #         alpha = spams.proximalTree(U,tree,return_val_loss = False,...) #         (alpha,val_loss) = spams.proximalTree(U,tree,return_val_loss = True,...) # # Authors: # Julien MAIRAL, 2010 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (python interface) # # Note: #     Valid values for the regularization parameter (regul) are: #       "l0", "l1", "l2", "linf", "l2-not-squared", "elastic-net", "fused-lasso", #       "group-lasso-l2", "group-lasso-linf", "sparse-group-lasso-l2", #       "sparse-group-lasso-linf", "l1l2", "l1linf", "l1l2+l1", "l1linf+l1", #       "tree-l0", "tree-l2", "tree-linf", "graph", "graph-ridge", "graph-l2", #       "multi-task-tree", "multi-task-graph", "l1linf-row-column", "trace-norm", #       "trace-norm-vec", "rank", "rank-vec", "none" #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 import spams import numpy as np param = {'numThreads' : -1,'verbose' : True,          'pos' : False, 'intercept' : False, 'lambda1' : 0.1 } m = 10;n = 1000 U = np.asfortranarray(np.random.normal(size = (m,n)),dtype=myfloat) print 'First tree example' # Example 1 of tree structure # tree structured groups: # g1= {0 1 2 3 4 5 6 7 8 9} # g2= {2 3 4} # g3= {5 6 7 8 9} own_variables =  np.array([0,2,5],dtype=np.int32) # pointer to the first variable of each group N_own_variables =  np.array([2,3,5],dtype=np.int32) # number of "root" variables in each group # (variables that are in a group, but not in its descendants). # for instance root(g1)={0,1}, root(g2)={2 3 4}, root(g3)={5 6 7 8 9} eta_g = np.array([1,1,1],dtype=myfloat) # weights for each group, they should be non-zero to use fenchel duality groups = np.asfortranarray([[0,0,0],                             [1,0,0],                             [1,0,0]],dtype = np.bool) # first group should always be the root of the tree # non-zero entriees mean inclusion relation ship, here g2 is a children of g1, # g3 is a children of g1 groups = ssp.csc_matrix(groups,dtype=np.bool) tree = {'eta_g': eta_g,'groups' : groups,'own_variables' : own_variables,         'N_own_variables' : N_own_variables} print '\ntest prox tree-l0' param['regul'] = 'tree-l2' alpha = spams.proximalTree(U,tree,False,**param) print '\ntest prox tree-linf' param['regul'] = 'tree-linf' alpha = spams.proximalTree(U,tree,False,**param) print 'Second tree example' # Example 2 of tree structure # tree structured groups: # g1= {0 1 2 3 4 5 6 7 8 9}    root(g1) = { }; # g2= {0 1 2 3 4 5}            root(g2) = {0 1 2}; # g3= {3 4}                    root(g3) = {3 4}; # g4= {5}                      root(g4) = {5}; # g5= {6 7 8 9}                root(g5) = { }; # g6= {6 7}                    root(g6) = {6 7}; # g7= {8 9}                    root(g7) = {8}; # g8 = {9}                     root(g8) = {9}; own_variables =  np.array([0, 0, 3, 5, 6, 6, 8, 9],dtype=np.int32) N_own_variables =  np.array([0,3,2,1,0,2,1,1],dtype=np.int32) eta_g = np.array([1,1,1,2,2,2,2.5,2.5],dtype=myfloat) groups = np.asfortranarray([[0,0,0,0,0,0,0,0],                 [1,0,0,0,0,0,0,0],                 [0,1,0,0,0,0,0,0],                 [0,1,0,0,0,0,0,0],                 [1,0,0,0,0,0,0,0],                 [0,0,0,0,1,0,0,0],                 [0,0,0,0,1,0,0,0],                 [0,0,0,0,0,0,1,0]],dtype = np.bool) groups = ssp.csc_matrix(groups,dtype=np.bool) tree = {'eta_g': eta_g,'groups' : groups, 'own_variables' : own_variables,         'N_own_variables' : N_own_variables} print '\ntest prox tree-l0' param['regul'] = 'tree-l0' alpha = spams.proximalTree(U,tree,False,**param) print '\ntest prox tree-l2' param['regul'] = 'tree-l2' alpha = spams.proximalTree(U,tree,False,**param) print '\ntest prox tree-linf' param['regul'] = 'tree-linf' alpha = spams.proximalTree(U,tree,False,**param) # mexProximalTree also works with non-tree-structured regularization functions print '\nprox l1, intercept, positivity constraint' param['regul'] = 'l1' param['pos'] = True       # can be used with all the other regularizations param['intercept'] = True # can be used with all the other regularizations      alpha = spams.proximalTree(U,tree,False,**param) print '\nprox multi-task tree' param['pos'] = False param['intercept'] = False param['lambda2'] = param['lambda1'] param['regul'] = 'multi-task-tree' alpha = spams.proximalTree(U,tree,False,**param)

### 5.4  Function spams.proximalGraph

This function computes the proximal operators associated to structured sparse regularization, for input signals U=[u1,…,un] in ℝp × n, and a set of groups , it returns a matrix V=[v1,…,vn] in ℝp × n. When one uses a regularization function on vectors, it computes a column v of V for every column u of U:

 min v ∈ ℝp

 1 2
||uv||22 + λ
 ∑ g ∈ G
ηg ||vg||,     (44)

or with a regularization function on matrices, it computes V solving

 min V ∈ ℝp× n

 1 2
||UV||F2 + λ
 n ∑ i=1
 ∑ g ∈ G
ηg ||vgi||+ λ2
 ∑ g ∈ G

 max j ∈ g
||vgj||,     (45)

This function can also be used for computing the proximal operators addressed by spams.proximalFlat. The way the graph is incoded is presented below (and also in the example file test_ProximalGraph.m, with more usage details:

 # # Name: proximalGraph # # Usage: spams.proximalGraph(U,graph,return_val_loss = False,numThreads =-1,lambda1=1.0,lambda2=0., #                     lambda3=0.,intercept=False,resetflow=False,regul="",verbose=False, #                     pos=False,clever=True,eval= None,size_group=1,transpose=False) # # Description: #     proximalGraph computes a proximal operator. Depending #         on the value of regul, it computes  #          #         Given an input matrix U=[u^1,\ldots,u^n], and a set of groups G, #         it computes a matrix V=[v^1,\ldots,v^n] such that #          #         if regul='graph' #         for every column u of U, it computes a column v of V solving #             argmin 0.5||u-v||_2^2 + lambda1\sum_{g \in G} \eta_g||v_g||_inf #              #         if regul='graph+ridge' #         for every column u of U, it computes a column v of V solving #             argmin 0.5||u-v||_2^2 + lambda1\sum_{g \in G} \eta_g||v_g||_inf + lambda1_2||v||_2^2 #              #              #         if regul='multi-task-graph' #            V=argmin 0.5||U-V||_F^2 + lambda1 \sum_{i=1}^n\sum_{g \in G} \eta_g||v^i_g||_inf + ... #                                                lambda1_2 \sum_{g \in G} \eta_g max_{j in g}||V_j||_{inf} #                                                 #         it can also be used with any regularization addressed by proximalFlat #          #         for all these regularizations, it is possible to enforce non-negativity constraints #         with the option pos, and to prevent the last row of U to be regularized, with #         the option intercept # # Inputs: #       U:  double p x n matrix   (input signals) #               m is the signal size #       graph: struct #             with three fields, eta_g, groups, and groups_var #              #             The first fields sets the weights for every group #                graph.eta_g            double N vector  #                 #             The next field sets inclusion relations between groups  #             (but not between groups and variables): #                graph.groups           sparse (double or boolean) N x N matrix   #                the (i,j) entry is non-zero if and only if i is different than j and  #                gi is included in gj. #                 #             The next field sets inclusion relations between groups and variables #                graph.groups_var       sparse (double or boolean) p x N matrix #                the (i,j) entry is non-zero if and only if the variable i is included  #                in gj, but not in any children of gj. #                 #             examples are given in test_ProximalGraph.m #              #       return_val_loss:      #               if true the function will return a tuple of matrices. #       lambda1:  (regularization parameter) #       regul: (choice of regularization, see above) #       lambda2:  (optional, regularization parameter) #       lambda3:  (optional, regularization parameter) #       verbose: (optional, verbosity level, false by default) #       intercept: (optional, last row of U is not regularized, #         false by default) #       pos: (optional, adds positivity constraints on the #         coefficients, false by default) #       numThreads: (optional, number of threads for exploiting #         multi-core / multi-cpus. By default, it takes the value -1, #         which automatically selects all the available CPUs/cores). #       resetflow:    undocumented; modify at your own risks! #       clever:    undocumented; modify at your own risks! #       size_group:    undocumented; modify at your own risks! #       transpose:    undocumented; modify at your own risks! # # Output: #       V: double p x n matrix (output coefficients) #       val_regularizer: double 1 x n vector (value of the regularization #       term at the optimum). #       val_loss:        vector of size U.shape #         alpha = spams.proximalGraph(U,graph,return_val_loss = False,...) #         (alpha,val_loss) = spams.proximalGraph(U,graph,return_val_loss = True,...) # # Authors: # Julien MAIRAL, 2010 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (python interface) # # Note: #     Valid values for the regularization parameter (regul) are: #       "l0", "l1", "l2", "linf", "l2-not-squared", "elastic-net", "fused-lasso", #       "group-lasso-l2", "group-lasso-linf", "sparse-group-lasso-l2", #       "sparse-group-lasso-linf", "l1l2", "l1linf", "l1l2+l1", "l1linf+l1", #       "tree-l0", "tree-l2", "tree-linf", "graph", "graph-ridge", "graph-l2", #       "multi-task-tree", "multi-task-graph", "l1linf-row-column", "trace-norm", #       "trace-norm-vec", "rank", "rank-vec", "none" #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

### 5.5  Function spams.proximalPathCoding

This function computes the proximal operators associated to the path coding penalties of .

 # # The python function is not yet implemented. #

The following piece of code illustrates how to use this function. This function is associated to a function to evaluate the penalties:

### 5.6  Function spams.evalPathCoding

 # # The python function is not yet implemented. #

The following piece of code illustrates how to use this function.

After having presented the regularization terms which our software can handle, we present the various formulations that we address

### 5.7  Problems Addressed

We present here regression or classification formulations and their multi-task variants.

#### 5.7.1  Regression Problems with the Square Loss

Given a training set {xi,yi}i=1n, with xi ∈ ℝp and yi ∈ ℝ for all i in [ 1;n ], we address

 min w ∈ ℝp, b ∈ ℝ

 n ∑ i=1

 1 2
(yiwxib)2 + λψ(w),

where b is an optional variable acting as an “intercept”, which is not regularized, and ψ can be any of the regularization functions presented above. Let us consider the vector y in ℝn that carries the entries yi. The problem without the intercept takes the following form, which we have already encountered in the previous toolbox, but with different notations:

 min w ∈ ℝp

 1 2
||yXw||22 + λψ(w),

where the X=[xi,…,xn]T (the xi’s are here the rows of X).

#### 5.7.2  Classification Problems with the Logistic Loss

The next formulation that our software can solve is the regularized logistic regression formulation. We are again given a training set {xi,yi}i=1n, with xi ∈ ℝp, but the variables yi are now in {−1,+1} for all i in [ 1;n ]. The optimization problem we address is

 min w ∈ ℝp, b ∈ ℝ

 1 n
 n ∑ i=1
log(1+eyi(wxi+b) + λψ(w),

with again ψ taken to be one of the regularization function presented above, and b is an optional intercept.

#### 5.7.3  Multi-class Classification Problems with the Softmax Loss

We have also implemented a multi-class logistic classifier (or softmax). For a classification problem with r classes, we are given a training set {xi,yi}i=1n, where the variables xi are still vectors in ℝp, but the yi’s have integer values in {1,2,…,r}. The formulation we address is the following multi-class learning problem

 min W ∈ ℝp × r, b ∈ ℝr

 1 n
 n ∑ i=1
log

 r ∑ j=1
e (wjwyi)xi + bjbyi

+ λ
 r ∑ j=1
ψ(wj),     (46)

where W = [w1,…,wr] and the optional vector b in ℝr carries intercepts for each class.

#### 5.7.4  Multi-task Regression Problems with the Square Loss

We are now considering a problem with r tasks, and a training set {xi,yi}i=1n, where the variables xi are still vectors in ℝp, and yi is a vector in ℝr. We are looking for r regression vectors wj, for j∈ [ 1;r ], or equivalently for a matrix W=[w1,…,wr] in ℝp × r. The formulation we address is the following multi-task regression problem

 min W ∈ ℝp × r, b ∈ ℝr

 r ∑ j=1
 n ∑ i=1

 1 2
(yjiwxibj)2 + λψ(W),

where ψ is any of the regularization function on matrices we have presented in the previous section. Note that by introducing the appropriate variables Y, the problem without intercept could be equivalently rewritten

 min W ∈ ℝp × r

 1 2
||YXW||F2 + λψ(W).

#### 5.7.5  Multi-task Classification Problems with the Logistic Loss

The multi-task version of the logistic regression follows the same principle. We consider r tasks, and a training set {xi,yi}i=1n, with the xi’s in ℝp, and the yi’s are vectors in {−1,+1}r. We look for a matrix W=[w1,…,wr] in ℝp × r. The formulation is the following multi-task regression problem

 min W ∈ ℝp × r, b ∈ ℝr

 r ∑ j=1
 1 n
 n ∑ i=1
log

1+eyji(wxi+bj)

+ λψ(W).

#### 5.7.6  Multi-task and Multi-class Classification Problems with the Softmax Loss

The multi-task/multi-class version directly follows from the formulation of Eq. (46), but associates with each class a task, and as a consequence, regularizes the matrix W in a particular way:

 min W ∈ ℝp × r, b ∈ ℝr

 1 n
 n ∑ i=1
log

 r ∑ j=1
e (wjwyi)xi + bjbyi

+ λψ(W).

How duality gaps are computed for any of these formulations is presented in Appendix A. We now present the main functions for solving these problems

### 5.8  Function spams.fistaFlat

Given a matrix X=[x1,…,xp]T in ℝm × p, and a matrix Y=[y1,…,yn], it solves the optimization problems presented in the previous section, with the same regularization functions as spams.proximalFlat. see usage details below:

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 import spams import numpy as np param = {'numThreads' : -1,'verbose' : True,          'lambda1' : 0.05, 'it0' : 10, 'max_it' : 200,          'L0' : 0.1, 'tol' : 1e-3, 'intercept' : False,          'pos' : False} np.random.seed(0) m = 100;n = 200 X = np.asfortranarray(np.random.normal(size = (m,n))) X = np.asfortranarray(X - np.tile(np.mean(X,0),(X.shape,1)),dtype=myfloat) X = spams.normalize(X) Y = np.asfortranarray(np.random.normal(size = (m,1))) Y = np.asfortranarray(Y - np.tile(np.mean(Y,0),(Y.shape,1)),dtype=myfloat) Y = spams.normalize(Y) W0 = np.zeros((X.shape,Y.shape),dtype=myfloat,order="FORTRAN") # Regression experiments  # 100 regression problems with the same design matrix X. print '\nVarious regression experiments' param['compute_gram'] = True print '\nFISTA + Regression l1' param['loss'] = 'square' param['regul'] = 'l1' # param.regul='group-lasso-l2'; # param.size_group=10; (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) ##    print "XX %s" %str(optim_info.shape);return None print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:],0),np.mean(optim_info[2,:],0),np.mean(optim_info[3,:],0)) ### print '\nISTA + Regression l1' param['ista'] = True (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f\n' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) ## print '\nSubgradient Descent + Regression l1' param['ista'] = False param['subgrad'] = True param['a'] = 0.1 param['b'] = 1000 # arbitrary parameters max_it = param['max_it'] it0 = param['it0'] param['max_it'] = 500 param['it0'] = 50 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f\n' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) param['subgrad'] = False param['max_it'] = max_it param['it0'] = it0 ### print '\nFISTA + Regression l2' param['regul'] = 'l2' (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f\n' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) ### print '\nFISTA + Regression l2 + sparse feature matrix' param['regul'] = 'l2'; (W, optim_info) = spams.fistaFlat(Y,ssp.csc_matrix(X),W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) ########### print '\nFISTA + Regression Elastic-Net' param['regul'] = 'elastic-net' param['lambda2'] = 0.1 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[3,:])) print '\nFISTA + Group Lasso L2' param['regul'] = 'group-lasso-l2' param['size_group'] = 2 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:],0),np.mean(optim_info[2,:],0),np.mean(optim_info[3,:],0)) print '\nFISTA + Group Lasso L2 with variable size of groups' param['regul'] = 'group-lasso-l2' param2=param.copy() param2['groups'] = np.array(np.random.random_integers(1,5,X.shape),dtype = np.int32) param2['lambda1'] *= 10 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:],0),np.mean(optim_info[2,:],0),np.mean(optim_info[3,:],0)) print '\nFISTA + Trace Norm' param['regul'] = 'trace-norm-vec' param['size_group'] = 5 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:],0),np.mean(optim_info[3,:])) ####     print '\nFISTA + Regression Fused-Lasso' param['regul'] = 'fused-lasso' param['lambda2'] = 0.1 param['lambda3'] = 0.1; # (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[3,:])) print '\nFISTA + Regression no regularization' param['regul'] = 'none' (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[3,:])) print '\nFISTA + Regression l1 with intercept ' param['intercept'] = True param['regul'] = 'l1' x1 = np.asfortranarray(np.concatenate((X,np.ones((X.shape,1))),1),dtype=myfloat) W01 = np.asfortranarray(np.concatenate((W0,np.zeros((1,W0.shape))),0),dtype=myfloat) (W, optim_info) = spams.fistaFlat(Y,x1,W01,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) print '\nFISTA + Regression l1 with intercept+ non-negative ' param['pos'] = True param['regul'] = 'l1' x1 = np.asfortranarray(np.concatenate((X,np.ones((X.shape,1))),1),dtype=myfloat) W01 = np.asfortranarray(np.concatenate((W0,np.zeros((1,W0.shape))),0),dtype=myfloat) (W, optim_info) = spams.fistaFlat(Y,x1,W01,True,**param) print 'mean loss: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[3,:])) param['pos'] = False param['intercept'] = False print '\nISTA + Regression l0' param['regul'] = 'l0' (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[3,:])) # Classification print '\nOne classification experiment' Y = np.asfortranarray(2 * np.asarray(np.random.normal(size = (100,1)) > 0,dtype=myfloat) - 1) print '\nFISTA + Logistic l1' param['regul'] = 'l1' param['loss'] = 'logistic' param['lambda1'] = 0.01 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) # can be used of course with other regularization functions, intercept,... param['regul'] = 'l1' param['loss'] = 'weighted-logistic' param['lambda1'] = 0.01 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) # can be used of course with other regularization functions, intercept,... print '\nFISTA + Logistic l1 + sparse matrix' param['loss'] = 'logistic' (W, optim_info) = spams.fistaFlat(Y,ssp.csc_matrix(X),W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) # can be used of course with other regularization functions, intercept,... # Multi-Class classification Y = np.asfortranarray(np.ceil(5 * np.random.random(size = (100,1000))) - 1,dtype=myfloat) param['loss'] = 'multi-logistic' print '\nFISTA + Multi-Class Logistic l1' nclasses = np.max(Y[:])+1 W0 = np.zeros((X.shape,nclasses * Y.shape),dtype=myfloat,order="FORTRAN") (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) # can be used of course with other regularization functions, intercept,... # Multi-Task regression Y = np.asfortranarray(np.random.normal(size = (100,100)),dtype=myfloat) Y = np.asfortranarray(Y - np.tile(np.mean(Y,0),(Y.shape,1)),dtype=myfloat) Y = spams.normalize(Y) param['compute_gram'] = False W0 = np.zeros((X.shape,Y.shape),dtype=myfloat,order="FORTRAN") param['loss'] = 'square' print '\nFISTA + Regression l1l2 ' param['regul'] = 'l1l2' (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) print '\nFISTA + Regression l1linf ' param['regul'] = 'l1linf' (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) print '\nFISTA + Regression l1l2 + l1 ' param['regul'] = 'l1l2+l1' param['lambda2'] = 0.1 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[3,:])) print '\nFISTA + Regression l1linf + l1 ' param['regul'] = 'l1linf+l1' param['lambda2'] = 0.1 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[3,:])) print '\nFISTA + Regression l1linf + row + columns ' param['regul'] = 'l1linf-row-column' param['lambda2'] = 0.1 (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) # Multi-Task Classification print '\nFISTA + Logistic + l1l2 ' param['regul'] = 'l1l2' param['loss'] = 'logistic' Y = np.asfortranarray(2 * np.asarray(np.random.normal(size = (100,100)) > 1,dtype=myfloat) - 1) (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) # Multi-Class + Multi-Task Regularization print '\nFISTA + Multi-Class Logistic l1l2 ' Y = np.asfortranarray(np.ceil(5 * np.random.random(size = (100,1000))) - 1,dtype=myfloat) Y = spams.normalize(Y) param['loss'] = 'multi-logistic' param['regul'] = 'l1l2' nclasses = np.max(Y[:])+1 W0 = np.zeros((X.shape,nclasses * Y.shape),dtype=myfloat,order="FORTRAN") (W, optim_info) = spams.fistaFlat(Y,X,W0,True,**param) print 'mean loss: %f, mean relative duality_gap: %f, number of iterations: %f' %(np.mean(optim_info[0,:]),np.mean(optim_info[2,:]),np.mean(optim_info[3,:])) # can be used of course with other regularization functions, intercept,... #############

### 5.9  Function spams.fistaTree

Given a matrix X=[x1,…,xp]T in ℝm × p, and a matrix Y=[y1,…,yn], it solves the optimization problems presented in the previous section, with the same regularization functions as spams.proximalTree. see usage details below:

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

### 5.10  Function spams.fistaGraph

Given a matrix X=[x1,…,xp]T in ℝm × p, and a matrix Y=[y1,…,yn], it solves the optimization problems presented in the previous section, with the same regularization functions as spams.proximalGraph. see usage details below:

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

### 5.11  Function spams.fistaPathCoding

Similarly, the toolbox handles the penalties of  with the following function

 # # The python function is not yet implemented. #

The following piece of code illustrates how to use this function.

### 5.12  Function solverPoisson

The following problem is addressed here

 min w ∈ ℝ+p

 p ∑ j=1
xiw + δ − yi log(xiw +δ) + λ ψ(w).
 # # The python function is not yet implemented. #

The following piece of code illustrates how to use this function.

### 5.13  Function mexIncrementalProx

This implements the incremental solver MISO .

 # # The python function is not yet implemented. #

The following piece of code illustrates how to use this function.

### 5.14  Function mexStochasticProx

This implements the stochastic proximal gradient solver .

 # # The python function is not yet implemented. #

The following piece of code illustrates how to use this function.   