summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ot/da.py37
-rw-r--r--ot/gpu/da.py41
2 files changed, 21 insertions, 57 deletions
diff --git a/ot/da.py b/ot/da.py
index c944c0d..557e2aa 100644
--- a/ot/da.py
+++ b/ot/da.py
@@ -11,7 +11,7 @@ from .optim import cg
from .optim import gcg
-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):
+def sinkhorn_lpl1_mm(a,labels_a, b, M, reg, eta=0.1,numItermax = 10,numInnerItermax = 200,stopInnerThr=1e-9,verbose=False,log=False):
"""
Solve the entropic regularization optimal transport problem with nonconvex group lasso regularization
@@ -55,8 +55,6 @@ def sinkhorn_lpl1_mm(a,labels_a, b, M, reg, eta=0.1,numItermax = 10,numInnerIter
Max number of iterations (inner sinkhorn solver)
stopInnerThr : float, optional
Stop threshold on error (inner sinkhorn solver) (>0)
- unlabelledValue : int, optional
- this value in array labels_a means this is an unlabelled example
verbose : bool, optional
Print information along iterations
log : bool, optional
@@ -84,41 +82,28 @@ def sinkhorn_lpl1_mm(a,labels_a, b, M, reg, eta=0.1,numItermax = 10,numInnerIter
ot.optim.cg : General regularized OT
"""
- p=0.5
+ p = 0.5
epsilon = 1e-3
- # init data
- Nini = len(a)
- Nfin = len(b)
-
indices_labels = []
classes = np.unique(labels_a)
for c in classes:
idxc, = np.where(labels_a == c)
indices_labels.append(idxc)
- W=np.zeros(M.shape)
+ W = np.zeros(M.shape)
for cpt in range(numItermax):
Mreg = M + eta*W
- transp=sinkhorn(a,b,Mreg,reg,numItermax=numInnerItermax, stopThr=stopInnerThr)
- # the transport has been computed. Check if classes are really separated
- W = np.ones((Nini,Nfin))
- all_majs = []
- idx_unlabelled = -1
+ transp = sinkhorn(a, b, Mreg, reg, numItermax=numInnerItermax,
+ stopThr=stopInnerThr)
+ # the transport has been computed. Check if classes are really
+ # separated
+ W = np.ones(M.shape)
for (i, c) in enumerate(classes):
- if c != unlabelledValue:
- majs = np.sum(transp[indices_labels[i]], axis=0)
- majs = p*((majs+epsilon)**(p-1))
- W[indices_labels[i]] = majs
- all_majs.append(majs)
- else:
- idx_unlabelled = i
-
- # now we majorize the unlabelled (if there are any) by the min of
- # the majorizations. do it only for unlabbled data
- if idx_unlabelled != -1:
- W[indices_labels[idx_unlabelled]] = np.min(all_majs, axis=0)
+ majs = np.sum(transp[indices_labels[i]], axis=0)
+ majs = p*((majs+epsilon)**(p-1))
+ W[indices_labels[i]] = majs
return transp
diff --git a/ot/gpu/da.py b/ot/gpu/da.py
index 700a5e4..399e769 100644
--- a/ot/gpu/da.py
+++ b/ot/gpu/da.py
@@ -69,11 +69,9 @@ def pairwiseEuclideanGPU(a, b, returnAsGPU=False, squared=False):
def sinkhorn_lpl1_mm(a, labels_a, b, M_GPU, reg, eta=0.1, numItermax=10,
numInnerItermax=200, stopInnerThr=1e-9,
- unlabelledValue=-99, verbose=False, log=False):
+ verbose=False, log=False):
p = 0.5
epsilon = 1e-3
-
- # init data
Nfin = len(b)
indices_labels = []
@@ -94,37 +92,18 @@ def sinkhorn_lpl1_mm(a, labels_a, b, M_GPU, reg, eta=0.1, numItermax=10,
# separated
W_GPU.assign(1)
W_GPU = W_GPU.transpose()
- all_majs_GPU = []
- idx_unlabelled = -1
for (i, c) in enumerate(classes):
- if c != unlabelledValue:
- (_, nbRow) = indices_labels[i].shape
- tmpC_GPU = cudamat.empty((Nfin, nbRow)).assign(0)
- transp_GPU.transpose().select_columns(indices_labels[i],
- tmpC_GPU)
- majs_GPU = tmpC_GPU.sum(axis=1).add(epsilon)
- cudamat.pow(majs_GPU, (p-1))
- majs_GPU.mult(p)
- all_majs_GPU.append(majs_GPU)
-
- tmpC_GPU.assign(0)
- tmpC_GPU.add_col_vec(majs_GPU)
- W_GPU.set_selected_columns(indices_labels[i], tmpC_GPU)
- else:
- idx_unlabelled = i
-
- # now we majorize the unlabelled (if there are any) by the min of
- # the majorizations. do it only for unlabbled data
- if idx_unlabelled != -1:
- all_majs = np.array([m_GPU.asarray() for m_GPU in all_majs_GPU])
- minMaj_GPU = (cudamat.CUDAMatrix(all_majs).min(axis=0)
- .transpose())
- (_, nbRow) = indices_labels[idx_unlabelled].shape
+ (_, nbRow) = indices_labels[i].shape
tmpC_GPU = cudamat.empty((Nfin, nbRow)).assign(0)
+ transp_GPU.transpose().select_columns(indices_labels[i], tmpC_GPU)
+ majs_GPU = tmpC_GPU.sum(axis=1).add(epsilon)
+ cudamat.pow(majs_GPU, (p-1))
+ majs_GPU.mult(p)
+
+ tmpC_GPU.assign(0)
+ tmpC_GPU.add_col_vec(majs_GPU)
+ W_GPU.set_selected_columns(indices_labels[i], tmpC_GPU)
- tmpC_GPU.add_col_vec(minMaj_GPU)
- W_GPU.set_selected_columns(indices_labels[idx_unlabelled],
- tmpC_GPU)
W_GPU = W_GPU.transpose()
return transp_GPU.asarray()