Skip to content

Commit 9df60d4

Browse files
author
Leo gautheron
committed
Remove unnecessary parameter
1 parent e2cf853 commit 9df60d4

File tree

2 files changed

+21
-57
lines changed

2 files changed

+21
-57
lines changed

ot/da.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .optim import gcg
1212

1313

14-
def sinkhorn_lpl1_mm(a,labels_a, b, M, reg, eta=0.1,numItermax = 10,numInnerItermax = 200,stopInnerThr=1e-9,unlabelledValue=-99,verbose=False,log=False):
14+
def sinkhorn_lpl1_mm(a,labels_a, b, M, reg, eta=0.1,numItermax = 10,numInnerItermax = 200,stopInnerThr=1e-9,verbose=False,log=False):
1515
"""
1616
Solve the entropic regularization optimal transport problem with nonconvex group lasso regularization
1717
@@ -55,8 +55,6 @@ def sinkhorn_lpl1_mm(a,labels_a, b, M, reg, eta=0.1,numItermax = 10,numInnerIter
5555
Max number of iterations (inner sinkhorn solver)
5656
stopInnerThr : float, optional
5757
Stop threshold on error (inner sinkhorn solver) (>0)
58-
unlabelledValue : int, optional
59-
this value in array labels_a means this is an unlabelled example
6058
verbose : bool, optional
6159
Print information along iterations
6260
log : bool, optional
@@ -84,41 +82,28 @@ def sinkhorn_lpl1_mm(a,labels_a, b, M, reg, eta=0.1,numItermax = 10,numInnerIter
8482
ot.optim.cg : General regularized OT
8583
8684
"""
87-
p=0.5
85+
p = 0.5
8886
epsilon = 1e-3
8987

90-
# init data
91-
Nini = len(a)
92-
Nfin = len(b)
93-
9488
indices_labels = []
9589
classes = np.unique(labels_a)
9690
for c in classes:
9791
idxc, = np.where(labels_a == c)
9892
indices_labels.append(idxc)
9993

100-
W=np.zeros(M.shape)
94+
W = np.zeros(M.shape)
10195

10296
for cpt in range(numItermax):
10397
Mreg = M + eta*W
104-
transp=sinkhorn(a,b,Mreg,reg,numItermax=numInnerItermax, stopThr=stopInnerThr)
105-
# the transport has been computed. Check if classes are really separated
106-
W = np.ones((Nini,Nfin))
107-
all_majs = []
108-
idx_unlabelled = -1
98+
transp = sinkhorn(a, b, Mreg, reg, numItermax=numInnerItermax,
99+
stopThr=stopInnerThr)
100+
# the transport has been computed. Check if classes are really
101+
# separated
102+
W = np.ones(M.shape)
109103
for (i, c) in enumerate(classes):
110-
if c != unlabelledValue:
111-
majs = np.sum(transp[indices_labels[i]], axis=0)
112-
majs = p*((majs+epsilon)**(p-1))
113-
W[indices_labels[i]] = majs
114-
all_majs.append(majs)
115-
else:
116-
idx_unlabelled = i
117-
118-
# now we majorize the unlabelled (if there are any) by the min of
119-
# the majorizations. do it only for unlabbled data
120-
if idx_unlabelled != -1:
121-
W[indices_labels[idx_unlabelled]] = np.min(all_majs, axis=0)
104+
majs = np.sum(transp[indices_labels[i]], axis=0)
105+
majs = p*((majs+epsilon)**(p-1))
106+
W[indices_labels[i]] = majs
122107

123108
return transp
124109

ot/gpu/da.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ def pairwiseEuclideanGPU(a, b, returnAsGPU=False, squared=False):
6969

7070
def sinkhorn_lpl1_mm(a, labels_a, b, M_GPU, reg, eta=0.1, numItermax=10,
7171
numInnerItermax=200, stopInnerThr=1e-9,
72-
unlabelledValue=-99, verbose=False, log=False):
72+
verbose=False, log=False):
7373
p = 0.5
7474
epsilon = 1e-3
75-
76-
# init data
7775
Nfin = len(b)
7876

7977
indices_labels = []
@@ -94,37 +92,18 @@ def sinkhorn_lpl1_mm(a, labels_a, b, M_GPU, reg, eta=0.1, numItermax=10,
9492
# separated
9593
W_GPU.assign(1)
9694
W_GPU = W_GPU.transpose()
97-
all_majs_GPU = []
98-
idx_unlabelled = -1
9995
for (i, c) in enumerate(classes):
100-
if c != unlabelledValue:
101-
(_, nbRow) = indices_labels[i].shape
102-
tmpC_GPU = cudamat.empty((Nfin, nbRow)).assign(0)
103-
transp_GPU.transpose().select_columns(indices_labels[i],
104-
tmpC_GPU)
105-
majs_GPU = tmpC_GPU.sum(axis=1).add(epsilon)
106-
cudamat.pow(majs_GPU, (p-1))
107-
majs_GPU.mult(p)
108-
all_majs_GPU.append(majs_GPU)
109-
110-
tmpC_GPU.assign(0)
111-
tmpC_GPU.add_col_vec(majs_GPU)
112-
W_GPU.set_selected_columns(indices_labels[i], tmpC_GPU)
113-
else:
114-
idx_unlabelled = i
115-
116-
# now we majorize the unlabelled (if there are any) by the min of
117-
# the majorizations. do it only for unlabbled data
118-
if idx_unlabelled != -1:
119-
all_majs = np.array([m_GPU.asarray() for m_GPU in all_majs_GPU])
120-
minMaj_GPU = (cudamat.CUDAMatrix(all_majs).min(axis=0)
121-
.transpose())
122-
(_, nbRow) = indices_labels[idx_unlabelled].shape
96+
(_, nbRow) = indices_labels[i].shape
12397
tmpC_GPU = cudamat.empty((Nfin, nbRow)).assign(0)
98+
transp_GPU.transpose().select_columns(indices_labels[i], tmpC_GPU)
99+
majs_GPU = tmpC_GPU.sum(axis=1).add(epsilon)
100+
cudamat.pow(majs_GPU, (p-1))
101+
majs_GPU.mult(p)
102+
103+
tmpC_GPU.assign(0)
104+
tmpC_GPU.add_col_vec(majs_GPU)
105+
W_GPU.set_selected_columns(indices_labels[i], tmpC_GPU)
124106

125-
tmpC_GPU.add_col_vec(minMaj_GPU)
126-
W_GPU.set_selected_columns(indices_labels[idx_unlabelled],
127-
tmpC_GPU)
128107
W_GPU = W_GPU.transpose()
129108

130109
return transp_GPU.asarray()

0 commit comments

Comments
 (0)