Skip to content

Commit 35ba9a7

Browse files
Update examples
1 parent 4240e74 commit 35ba9a7

File tree

25 files changed

+316
-291
lines changed

25 files changed

+316
-291
lines changed

adapt/feature_based/_adda.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,15 @@ class ADDA(BaseAdaptDeep):
113113
114114
Examples
115115
--------
116-
>>> import numpy as np
116+
>>> from adapt.utils import make_classification_da
117117
>>> from adapt.feature_based import ADDA
118-
>>> np.random.seed(0)
119-
>>> Xs = np.concatenate((np.random.random((100, 1)),
120-
... np.zeros((100, 1))), 1)
121-
>>> Xt = np.concatenate((np.random.random((100, 1)),
122-
... np.ones((100, 1))), 1)
123-
>>> ys = 0.2 * Xs[:, 0]
124-
>>> yt = 0.2 * Xt[:, 0]
125-
>>> model = ADDA(random_state=0)
126-
>>> model.fit(Xs, ys, Xt, epochs=100, verbose=0)
127-
>>> np.abs(model.predict_task(Xt, domain="src").ravel() - yt).mean()
128-
0.1531...
129-
>>> np.abs(model.predict_task(Xt, domain="tgt").ravel() - yt).mean()
130-
0.0227...
118+
>>> Xs, ys, Xt, yt = make_classification_da()
119+
>>> model = ADDA(Xt=Xt, metrics=["acc"], random_state=0)
120+
>>> model.fit(Xs, ys, epochs=100, verbose=0)
121+
>>> model.score(Xt, yt)
122+
1/1 [==============================] - 0s 153ms/step - loss: 0.0960 - acc: 0.9300
123+
0.09596743434667587
124+
131125
132126
See also
133127
--------

adapt/feature_based/_ccsa.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def pairwise_y(X, Y):
1515
Y = tf.reshape(Y, (batch_size_y, dim))
1616
X = tf.tile(tf.expand_dims(X, -1), [1, 1, batch_size_y])
1717
Y = tf.tile(tf.expand_dims(Y, -1), [1, 1, batch_size_x])
18-
return tf.reduce_sum(tf.abs(X-tf.transpose(Y)), 1)/2
18+
return tf.reduce_sum(tf.abs(X-tf.transpose(Y)), 1)/2.
1919

2020

2121
def pairwise_X(X, Y):
@@ -88,6 +88,22 @@ class CCSA(BaseAdaptDeep):
8888
If ``yt`` is given in ``fit`` method, target metrics
8989
and losses are recorded too.
9090
91+
See also
92+
--------
93+
CDAN
94+
95+
Examples
96+
--------
97+
>>> import numpy as np
98+
>>> from adapt.utils import make_classification_da
99+
>>> from adapt.feature_based import CCSA
100+
>>> Xs, ys, Xt, yt = make_classification_da()
101+
>>> model = CCSA(margin=1., gamma=0.5, Xt=Xt, metrics=["acc"], random_state=0)
102+
>>> model.fit(Xs, ys, epochs=100, verbose=0)
103+
>>> model.score(Xt, yt)
104+
1/1 [==============================] - 0s 180ms/step - loss: 0.1550 - acc: 0.8900
105+
0.15503168106079102
106+
91107
References
92108
----------
93109
.. [1] `[1] <https://arxiv.org/abs/1709.10190>`_ S. Motiian, M. Piccirilli, \
@@ -116,18 +132,36 @@ def __init__(self,
116132
def train_step(self, data):
117133
# Unpack the data.
118134
Xs, Xt, ys, yt = self._unpack_data(data)
119-
135+
136+
# Check that yt is not None
137+
if yt is None:
138+
raise ValueError("The target labels `yt` is `None`, CCSA is a supervised"
139+
" domain adaptation method and need `yt` to be specified.")
140+
141+
# Check shape of ys
142+
if len(ys.get_shape()) <= 1 or ys.get_shape()[1] == 1:
143+
self._ys_is_1d = True
144+
else:
145+
self._ys_is_1d = False
146+
120147
# loss
121-
with tf.GradientTape() as task_tape, tf.GradientTape() as enc_tape:
148+
with tf.GradientTape() as task_tape, tf.GradientTape() as enc_tape:
122149
# Forward pass
123150
Xs_enc = self.encoder_(Xs, training=True)
124151
ys_pred = self.task_(Xs_enc, training=True)
125152

153+
# Change type
154+
ys = tf.cast(ys, ys_pred.dtype)
155+
yt = tf.cast(yt, ys_pred.dtype)
156+
126157
Xt_enc = self.encoder_(Xt, training=True)
127158

128-
dist_y = pairwise_y(ys, yt)
159+
dist_y = pairwise_y(ys, yt)
129160
dist_X = pairwise_X(Xs_enc, Xt_enc)
130161

162+
if self._ys_is_1d:
163+
dist_y *= 2.
164+
131165
contrastive_loss = tf.reduce_sum(dist_y * tf.maximum(0., self.margin - dist_X), 1) / (tf.reduce_sum(dist_y, 1) + EPS)
132166
contrastive_loss += tf.reduce_sum((1-dist_y) * dist_X, 1) / (tf.reduce_sum(1-dist_y, 1) + EPS)
133167
contrastive_loss = tf.reduce_mean(contrastive_loss)

adapt/feature_based/_cdan.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ class CDAN(BaseAdaptDeep):
140140
ADDA
141141
WDGRL
142142
143+
Examples
144+
--------
145+
>>> import numpy as np
146+
>>> from adapt.utils import make_classification_da
147+
>>> from adapt.feature_based import CDAN
148+
>>> Xs, ys, Xt, yt = make_classification_da()
149+
>>> ys = np.stack([ys, np.abs(1-ys)], 1)
150+
>>> yt = np.stack([yt, np.abs(1-yt)], 1)
151+
>>> model = CDAN(lambda_=0.1, Xt=Xt, metrics=["acc"], random_state=0)
152+
>>> model.fit(Xs, ys, epochs=100, verbose=0)
153+
>>> model.score(Xt, yt)
154+
1/1 [==============================] - 0s 106ms/step - loss: 0.1081 - acc: 0.8400
155+
0.10809497535228729
156+
143157
References
144158
----------
145159
.. [1] `[1] <https://arxiv.org/pdf/1705.10667.pdf>`_ Long, M., Cao, \

adapt/feature_based/_coral.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,18 @@ class CORAL(BaseAdaptEstimator):
7070
7171
Examples
7272
--------
73-
>>> import numpy as np
73+
>>> from sklearn.linear_model import RidgeClassifier
74+
>>> from adapt.utils import make_classification_da
7475
>>> from adapt.feature_based import CORAL
75-
>>> Xs = np.random.multivariate_normal(
76-
... np.array([0, 0]), np.array([[0.001, 0], [0, 1]]), 100)
77-
>>> Xt = np.random.multivariate_normal(
78-
... np.array([0, 0]), np.array([[0.1, 0.2], [0.2, 0.5]]), 100)
79-
>>> ys = np.zeros(100)
80-
>>> yt = np.zeros(100)
81-
>>> ys[Xs[:, 1]>0] = 1
82-
>>> yt[(Xt[:, 1]-0.5*Xt[:, 0])>0] = 1
83-
>>> model = CORAL(lambda_=1000.)
84-
>>> model.fit(Xs, ys, Xt);
85-
Covariance Matrix alignement...
86-
Previous covariance difference: 0.258273
87-
New covariance difference: 0.258072
88-
Fit estimator...
89-
>>> model.estimator_.score(Xt, yt)
90-
0.5750...
91-
>>> model = CORAL(lambda_=0.)
92-
>>> model.fit(Xs, ys, Xt);
93-
Covariance Matrix alignement...
94-
Previous covariance difference: 0.258273
95-
New covariance difference: 0.000000
96-
Fit estimator...
97-
>>> model.estimator_.score(Xt, yt)
98-
0.5717...
76+
>>> Xs, ys, Xt, yt = make_classification_da()
77+
>>> model = CORAL(RidgeClassifier(), Xt=Xt, random_state=0)
78+
>>> model.fit(Xs, ys)
79+
Fit transform...
80+
Previous covariance difference: 0.013181
81+
New covariance difference: 0.000004
82+
Fit Estimator...
83+
>>> model.score(Xt, yt)
84+
0.86
9985
10086
See also
10187
--------

adapt/feature_based/_dann.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,14 @@ class DANN(BaseAdaptDeep):
7777
7878
Examples
7979
--------
80-
>>> import numpy as np
80+
>>> from adapt.utils import make_classification_da
8181
>>> from adapt.feature_based import DANN
82-
>>> np.random.seed(0)
83-
>>> Xs = np.concatenate((np.random.random((100, 1)),
84-
... np.zeros((100, 1))), 1)
85-
>>> Xt = np.concatenate((np.random.random((100, 1)),
86-
... np.ones((100, 1))), 1)
87-
>>> ys = 0.2 * Xs[:, 0]
88-
>>> yt = 0.2 * Xt[:, 0]
89-
>>> model = DANN(lambda_=0., random_state=0)
90-
>>> model.fit(Xs, ys, Xt, epochs=100, verbose=0)
91-
>>> model.score_estimator(Xt, yt)
92-
0.0231...
93-
>>> model = DANN(lambda_=0.1, random_state=0)
94-
>>> model.fit(Xs, ys, Xt, epochs=100, verbose=0)
95-
>>> model.score_estimator(Xt, yt)
96-
0.0010...
82+
>>> Xs, ys, Xt, yt = make_classification_da()
83+
>>> model = DANN(lambda_=0.1, Xt=Xt, metrics=["acc"], random_state=0)
84+
>>> model.fit(Xs, ys, epochs=100, verbose=0)
85+
>>> model.score(Xt, yt)
86+
1/1 [==============================] - 0s 108ms/step - loss: 0.1732 - acc: 0.8100
87+
0.17324252426624298
9788
9889
See also
9990
--------

adapt/feature_based/_deepcoral.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,14 @@ class DeepCORAL(BaseAdaptDeep):
8181
8282
Examples
8383
--------
84-
>>> import numpy as np
84+
>>> from adapt.utils import make_classification_da
8585
>>> from adapt.feature_based import DeepCORAL
86-
>>> np.random.seed(0)
87-
>>> Xs = np.random.multivariate_normal(
88-
... np.array([0, 0]), np.array([[0.001, 0], [0, 1]]), 100)
89-
>>> Xt = np.random.multivariate_normal(
90-
... np.array([0, 0]), np.array([[0.1, 0.2], [0.2, 0.5]]), 100)
91-
>>> ys = np.zeros(100)
92-
>>> yt = np.zeros(100)
93-
>>> ys[Xs[:, 1]>0] = 1
94-
>>> yt[(Xt[:, 1]-0.5*Xt[:, 0])>0] = 1
95-
>>> model = DeepCORAL(lambda_=0., random_state=0)
96-
>>> model.fit(Xs, ys, Xt, epochs=500, batch_size=100, verbose=0)
97-
>>> model.score_estimator(Xt, yt)
98-
0.0574...
99-
>>> model = DeepCORAL(lambda_=1., random_state=0)
100-
>>> model.fit(Xs, ys, Xt, epochs=500, batch_size=100, verbose=0)
101-
>>> model.score_estimator(Xt, yt)
102-
0.0649...
86+
>>> Xs, ys, Xt, yt = make_classification_da()
87+
>>> model = DeepCORAL(lambda_=1., Xt=Xt, metrics=["acc"], random_state=0)
88+
>>> model.fit(Xs, ys, epochs=100, verbose=0)
89+
>>> model.score(Xt, yt)
90+
1/1 [==============================] - 0s 99ms/step - loss: 0.2029 - acc: 0.6800
91+
0.2029329240322113
10392
10493
See also
10594
--------

adapt/feature_based/_fa.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,18 @@ class FA(BaseAdaptEstimator):
7272
7373
Examples
7474
--------
75-
>>> import numpy as np
75+
>>> from sklearn.linear_model import RidgeClassifier
76+
>>> from adapt.utils import make_classification_da
7677
>>> from adapt.feature_based import FA
77-
>>> np.random.seed(0)
78-
>>> Xs = 0.1 * np.random.randn(100, 1) + 1.
79-
>>> Xt = 0.1 * np.random.randn(100, 1) + 1.
80-
>>> ys = 0.1 * np.random.randn(100, 1) + 0.
81-
>>> yt = 0.1 * np.random.randn(100, 1) + 1.
82-
>>> model = FA()
83-
>>> model.fit(Xs, ys, Xt[:10], yt[:10]);
84-
Augmenting feature space...
85-
Previous shape: (100, 1)
86-
New shape: (100, 3)
87-
Fit estimator...
88-
>>> np.abs(model.predict(Xt, domain="src") - yt).mean()
89-
0.9846...
90-
>>> np.abs(model.predict(Xt, domain="tgt") - yt).mean()
91-
0.1010...
78+
>>> Xs, ys, Xt, yt = make_classification_da()
79+
>>> model = FA(RidgeClassifier(), Xt=Xt[:10], yt=yt[:10], random_state=0)
80+
>>> model.fit(Xs, ys)
81+
Fit transform...
82+
Previous shape: (100, 2)
83+
New shape: (110, 6)
84+
Fit Estimator...
85+
>>> model.score(Xt, yt)
86+
0.92
9287
9388
References
9489
----------
@@ -145,6 +140,10 @@ def fit_transform(self, Xs, Xt, ys, yt, domains=None, **kwargs):
145140
-------
146141
X_emb, y : embedded input and output data
147142
"""
143+
if yt is None:
144+
raise ValueError("The target labels `yt` is `None`, FA is a supervised"
145+
" domain adaptation method and need `yt` to be specified.")
146+
148147
Xs, ys = check_arrays(Xs, ys)
149148
Xt, yt = check_arrays(Xt, yt)
150149

adapt/feature_based/_fmmd.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ class fMMD(BaseAdaptEstimator):
146146
CORAL
147147
FE
148148
149+
Examples
150+
--------
151+
>>> from sklearn.linear_model import RidgeClassifier
152+
>>> from adapt.utils import make_classification_da
153+
>>> from adapt.feature_based import fMMD
154+
>>> Xs, ys, Xt, yt = make_classification_da()
155+
>>> model = fMMD(RidgeClassifier(), Xt=Xt, kernel="linear", random_state=0, verbose=0)
156+
>>> model.fit(Xs, ys)
157+
>>> model.score(Xt, yt)
158+
0.45
159+
149160
References
150161
----------
151162
.. [1] `[1] <https://www.cs.cmu.edu/afs/cs/Web/People/jgc/publication\
@@ -225,6 +236,9 @@ def F(x=None, z=None):
225236
G = matrix(np.concatenate((linear_const_G, squared_constraint_G)))
226237
h = matrix(np.concatenate((linear_const_h, squared_constraint_h)))
227238
dims = {'l': p, 'q': [p+1], 's': []}
239+
240+
solvers.options["show_progress"] = bool(self.verbose)
241+
228242
sol = solvers.cp(F, G, h, dims)
229243

230244
W = np.array(sol["x"]).ravel()

adapt/feature_based/_mcd.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,14 @@ class MCD(BaseAdaptDeep):
5353
5454
Examples
5555
--------
56-
>>> Xs = np.concatenate((np.random.random((100, 1)),
57-
... np.zeros((100, 1))), 1)
58-
>>> Xt = np.concatenate((np.random.random((100, 1)),
59-
... np.ones((100, 1))), 1)
60-
>>> ys = 0.2 * Xs[:, 0]
61-
>>> yt = 0.2 * Xt[:, 0]
62-
>>> model = MCD(random_state=0)
63-
>>> model.fit(Xs, ys, Xt, yt, epochs=100, verbose=0)
64-
>>> model.history_src_["task_t"][-1]
65-
0.0234...
66-
>>> model.history_["task_t"][-1]
67-
0.0009...
56+
>>> from adapt.utils import make_classification_da
57+
>>> from adapt.feature_based import MCD
58+
>>> Xs, ys, Xt, yt = make_classification_da()
59+
>>> model = MCD(pretrain=True, n_steps=1, Xt=Xt, metrics=["acc"], random_state=0)
60+
>>> model.fit(Xs, ys, epochs=100, verbose=0)
61+
>>> model.score(Xt, yt)
62+
1/1 [==============================] - 0s 147ms/step - loss: 0.2527 - acc: 0.6200
63+
0.25267112255096436
6864
6965
References
7066
----------

adapt/feature_based/_mdd.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,14 @@ class MDD(BaseAdaptDeep):
4949
5050
Examples
5151
--------
52-
>>> Xs = np.concatenate((np.random.random((100, 1)),
53-
... np.zeros((100, 1))), 1)
54-
>>> Xt = np.concatenate((np.random.random((100, 1)),
55-
... np.ones((100, 1))), 1)
56-
>>> ys = 0.2 * Xs[:, 0]
57-
>>> yt = 0.2 * Xt[:, 0]
58-
>>> model = MDD(random_state=0)
59-
>>> model.fit(Xs, ys, Xt, yt, epochs=100, verbose=0)
60-
>>> model.history_["task_t"][-1]
61-
0.0009...
52+
>>> from adapt.utils import make_classification_da
53+
>>> from adapt.feature_based import MDD
54+
>>> Xs, ys, Xt, yt = make_classification_da()
55+
>>> model = MDD(lambda_=0.1, gamma=4., Xt=Xt, metrics=["acc"], random_state=0)
56+
>>> model.fit(Xs, ys, epochs=100, verbose=0)
57+
>>> model.score(Xt, yt)
58+
1/1 [==============================] - 0s 102ms/step - loss: 0.1971 - acc: 0.7800
59+
0.19707153737545013
6260
6361
References
6462
----------

0 commit comments

Comments
 (0)