22Test base
33"""
44
5+ import copy
56import shutil
67import numpy as np
78import pytest
2627yt = 0.2 * Xt [:, 0 ].ravel ()
2728
2829
30+ class DummyFeatureBased (BaseAdaptEstimator ):
31+
32+ def fit_transform (self , Xs , ** kwargs ):
33+ return Xs
34+
35+ def transform (self , Xs ):
36+ return Xs
37+
38+
39+ class DummyInstanceBased (BaseAdaptEstimator ):
40+
41+ def fit_weights (self , Xs , ** kwargs ):
42+ return np .ones (len (Xs ))
43+
44+ def predict_weights (self ):
45+ return np .ones (100 )
46+
47+
48+ class DummyParameterBased (BaseAdaptEstimator ):
49+
50+ def fit (self , Xs , ys ):
51+ return self .fit_estimator (Xs , ys )
52+
53+
2954def test_base_adapt_estimator ():
3055 base_adapt = BaseAdaptEstimator (Xt = Xt )
3156 for check in check_estimator (base_adapt , generate_only = True ):
@@ -37,7 +62,78 @@ def test_base_adapt_estimator():
3762 else :
3863 raise
3964
40-
65+
66+ def test_base_adapt_score ():
67+ model = DummyParameterBased (Xt = Xt , random_state = 0 )
68+ model .fit (Xs , ys )
69+ model .score (Xt , yt )
70+
71+ model = DummyFeatureBased (Xt = Xt , random_state = 0 )
72+ model .fit (Xs , ys )
73+ s1 = model .score (Xt , yt )
74+ s2 = model ._score_adapt (Xs , Xt )
75+ assert s1 == s2
76+
77+ model = DummyInstanceBased (Xt = Xt , random_state = 0 )
78+ model .fit (Xs , ys )
79+ model .score (Xt , yt )
80+ s1 = model .score (Xt , yt )
81+ s2 = model ._score_adapt (Xs , Xt )
82+ assert s1 == s2
83+
84+
85+ def test_base_adapt_val_sample_size ():
86+ model = DummyFeatureBased (Xt = Xt , random_state = 0 , val_sample_size = 10 )
87+ model .fit (Xs , ys )
88+ model .score (Xt , yt )
89+ assert len (model .Xs_ ) == 10
90+ assert len (model .Xt_ ) == 10
91+ assert np .all (model .Xs_ == Xs [model .src_index_ ])
92+
93+
94+ def test_base_adapt_keras_estimator ():
95+ est = Sequential ()
96+ est .add (Dense (1 , input_shape = Xs .shape [1 :]))
97+ est .compile (loss = "mse" , optimizer = Adam (0.01 ))
98+ model = BaseAdaptEstimator (est , Xt = Xt )
99+ model .fit (Xs , ys )
100+ assert model .estimator_ .loss == "mse"
101+ assert isinstance (model .estimator_ .optimizer , Adam )
102+ assert model .estimator_ .optimizer .learning_rate == 0.01
103+
104+ model = BaseAdaptEstimator (est , Xt = Xt , loss = "mae" ,
105+ optimizer = Adam (0.01 , beta_1 = 0.5 ),
106+ learning_rate = 0.1 )
107+ model .fit (Xs , ys )
108+ assert model .estimator_ .loss == "mae"
109+ assert isinstance (model .estimator_ .optimizer , Adam )
110+ assert model .estimator_ .optimizer .learning_rate == 0.1
111+ assert model .estimator_ .optimizer .beta_1 == 0.5
112+
113+ model = BaseAdaptEstimator (est , Xt = Xt , optimizer = "sgd" )
114+ model .fit (Xs , ys )
115+ assert not isinstance (model .estimator_ .optimizer , Adam )
116+
117+ est = Sequential ()
118+ est .add (Dense (1 , input_shape = Xs .shape [1 :]))
119+ model = BaseAdaptEstimator (est , Xt = Xt , loss = "mae" ,
120+ optimizer = Adam (0.01 , beta_1 = 0.5 ),
121+ learning_rate = 0.1 )
122+ model .fit (Xs , ys )
123+ assert model .estimator_ .loss == "mae"
124+ assert isinstance (model .estimator_ .optimizer , Adam )
125+ assert model .estimator_ .optimizer .learning_rate == 0.1
126+ assert model .estimator_ .optimizer .beta_1 == 0.5
127+
128+ s1 = model .score_estimator (Xt [:10 ], yt [:10 ])
129+ s2 = model .estimator_ .evaluate (Xt [:10 ], yt [:10 ])
130+ assert s1 == s2
131+
132+ copy_model = copy .deepcopy (model )
133+ assert s1 == copy_model .score_estimator (Xt [:10 ], yt [:10 ])
134+ assert hex (id (model )) != hex (id (copy_model ))
135+
136+
41137def test_base_adapt_deep ():
42138 model = BaseAdaptDeep (Xt = Xt , loss = "mse" , optimizer = Adam (), learning_rate = 0.1 )
43139 model .fit (Xs , ys )
@@ -52,8 +148,8 @@ def test_base_adapt_deep():
52148 assert hasattr (model , "encoder_" )
53149 assert hasattr (model , "task_" )
54150 assert hasattr (model , "discriminator_" )
55-
56-
151+
152+
57153def test_base_adapt_deep ():
58154 model = BaseAdaptDeep (Xt = Xt , loss = "mse" ,
59155 epochs = 2 ,
@@ -63,6 +159,7 @@ def test_base_adapt_deep():
63159 model .fit (Xs , ys )
64160 yp = model .predict (Xt )
65161 score = model .score (Xt , yt )
162+ score_est = model .score_estimator (Xt , yt )
66163 X_enc = model .transform (Xs )
67164 ypt = model .predict_task (Xt )
68165 ypd = model .predict_disc (Xt )
@@ -71,6 +168,7 @@ def test_base_adapt_deep():
71168 new_model .fit (Xs , ys )
72169 yp2 = new_model .predict (Xt )
73170 score2 = new_model .score (Xt , yt )
171+ score_est2 = new_model .score_estimator (Xt , yt )
74172 X_enc2 = new_model .transform (Xs )
75173 ypt2 = new_model .predict_task (Xt )
76174 ypd2 = new_model .predict_disc (Xt )
@@ -88,6 +186,7 @@ def test_base_adapt_deep():
88186
89187 assert np .all (yp == yp2 )
90188 assert score == score2
189+ assert score_est == score_est2
91190 assert np .all (ypt == ypt2 )
92191 assert np .all (ypd == ypd2 )
93192 assert np .all (X_enc == X_enc2 )
0 commit comments