-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDANN.py
211 lines (171 loc) · 8.64 KB
/
DANN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import time
import numpy as np
from math import sqrt
class DANN(object):
def __init__(self, learning_rate=0.05, hidden_layer_size=25, lambda_adapt=1., maxiter=200,
epsilon_init=None, adversarial_representation=True, seed=12342, verbose=False):
"""
Domain Adversarial Neural Network for classification
option "learning_rate" is the learning rate of the neural network.
option "hidden_layer_size" is the hidden layer size.
option "lambda_adapt" weights the domain adaptation regularization term.
if 0 or None or False, then no domain adaptation regularization is performed
option "maxiter" number of training iterations.
option "epsilon_init" is a term used for initialization.
if None the weight matrices are weighted by 6/(sqrt(r+c))
(where r and c are the dimensions of the weight matrix)
option "adversarial_representation": if False, the adversarial classifier is trained
but has no impact on the hidden layer representation. The label predictor is
then the same as a standard neural-network one (see experiments_moon.py figures).
option "seed" is the seed of the random number generator.
"""
self.hidden_layer_size = hidden_layer_size
self.maxiter = maxiter
self.lambda_adapt = lambda_adapt if lambda_adapt not in (None, False) else 0.
self.epsilon_init = epsilon_init
self.learning_rate = learning_rate
self.adversarial_representation = adversarial_representation
self.seed = seed
self.verbose = verbose
def sigmoid(self, z):
"""
Sigmoid function.
"""
return 1. / (1. + np.exp(-z))
def softmax(self, z):
"""
Softmax function.
"""
v = np.exp(z)
return v / np.sum(v,axis=0)
def random_init(self, l_in, l_out):
"""
This method is used to initialize the weight matrices of the DA neural network
"""
if self.epsilon_init is not None:
epsilon = self.epsilon_init
else:
epsilon = sqrt(6.0 / (l_in + l_out))
return epsilon * (2 * np.random.rand(l_out, l_in) - 1.0)
def fit(self, X, Y, X_adapt, X_valid=None, Y_valid=None, do_random_init=True):
"""
Trains the domain adversarial neural network until it reaches a total number of
iterations of "self.maxiter" since it was initialize.
inputs:
X : Source data matrix
Y : Source labels
X_adapt : Target data matrix
(X_valid, Y_valid) : validation set used for early stopping.
do_random_init : A boolean indicating whether to use random initialization or not.
"""
nb_examples, nb_features = np.shape(X)
nb_labels = len(set(Y))
nb_examples_adapt, _ = np.shape(X_adapt)
if self.verbose:
print('[DANN parameters]', self.__dict__)
np.random.seed(self.seed)
if do_random_init:
W = self.random_init(nb_features, self.hidden_layer_size)
V = self.random_init(self.hidden_layer_size, nb_labels)
b = np.zeros(self.hidden_layer_size)
c = np.zeros(nb_labels)
U = np.zeros(self.hidden_layer_size)
d = 0.
else:
W, V, b, c, U, d = self.W, self.V, self.b, self.c, self.U, self.d
best_valid_risk = 2.0
continue_until = 30
for t in range(self.maxiter):
for i in range(nb_examples):
x_t, y_t = X[i,:], Y[i]
hidden_layer = self.sigmoid(np.dot(W, x_t) + b)
output_layer = self.softmax(np.dot(V, hidden_layer) + c)
y_hot = np.zeros(nb_labels)
y_hot[y_t] = 1.0
delta_c = output_layer - y_hot
delta_V = np.dot(delta_c.reshape(-1,1), hidden_layer.reshape(1,-1))
delta_b = np.dot(V.T, delta_c) * hidden_layer * (1.-hidden_layer)
delta_W = np.dot(delta_b.reshape(-1,1), x_t.reshape(1,-1))
if self.lambda_adapt == 0.:
delta_U, delta_d = 0., 0.
else:
# add domain adaptation regularizer from current domain
gho_x_t = self.sigmoid(np.dot(U.T, hidden_layer) + d)
delta_d = self.lambda_adapt * (1. - gho_x_t)
delta_U = delta_d * hidden_layer
if self.adversarial_representation:
tmp = delta_d * U * hidden_layer * (1. - hidden_layer)
delta_b += tmp
delta_W += tmp.reshape(-1,1) * x_t.reshape(1,-1)
# add domain adaptation regularizer from other domain
i_2 = np.random.randint(nb_examples_adapt)
x_t_2 = X_adapt[i_2, :]
hidden_layer_2 = self.sigmoid( np.dot(W, x_t_2) + b)
gho_x_t_2 = self.sigmoid(np.dot(U.T, hidden_layer_2) + d)
delta_d -= self.lambda_adapt * gho_x_t_2
delta_U -= self.lambda_adapt * gho_x_t_2 * hidden_layer_2
if self.adversarial_representation:
tmp = -self.lambda_adapt * gho_x_t_2 * U * hidden_layer_2 * (1. - hidden_layer_2)
delta_b += tmp
delta_W += tmp.reshape(-1,1) * x_t_2.reshape(1,-1)
W -= delta_W * self.learning_rate
b -= delta_b * self.learning_rate
V -= delta_V * self.learning_rate
c -= delta_c * self.learning_rate
U += delta_U * self.learning_rate
d += delta_d * self.learning_rate
# END for i in range(nb_examples)
self.W, self.V, self.b, self.c, self.U, self.d = W, V, b, c, U, d
# early stopping
if X_valid is not None:
valid_pred = self.predict(X_valid)
valid_risk = np.mean( valid_pred != Y_valid )
if valid_risk <= best_valid_risk:
if self.verbose:
print('[DANN best valid risk so far] %f (iter %d)' % (valid_risk, t))
best_valid_risk = valid_risk
best_weights = (W.copy(), V.copy(), b.copy(), c.copy())
best_t = t
continue_until = max(continue_until, int(1.5*t))
elif t > continue_until:
if self.verbose:
print('[DANN early stop] iter %d' % t)
break
# END for t in range(self.maxiter)
if X_valid is not None:
self.W, self.V, self.b, self.c = best_weights
self.nb_iter = best_t
self.valid_risk = best_valid_risk
else:
self.nb_iter = self.maxiter
self.valid_risk = 2.
def forward(self, X):
"""
Compute and return the network outputs for X, i.e., a 2D array of size len(X) by len(set(Y)).
the ith row of the array contains output probabilities for each class for the ith example.
"""
hidden_layer = self.sigmoid(np.dot(self.W, X.T) + self.b[:,np.newaxis])
output_layer = self.softmax(np.dot(self.V, hidden_layer) + self.c[:,np.newaxis])
return output_layer
def hidden_representation(self, X):
"""
Compute and return the network hidden layer values for X.
"""
hidden_layer = self.sigmoid(np.dot(self.W, X.T) + self.b[:,np.newaxis])
return hidden_layer.T
def predict(self, X):
"""
Compute and return the label predictions for X, i.e., a 1D array of size len(X).
the ith row of the array contains the predicted class for the ith example .
"""
output_layer = self.forward(X)
return np.argmax(output_layer, 0)
def predict_domain(self, X):
"""
Compute and return the domain predictions for X, i.e., a 1D array of size len(X).
the ith row of the array contains the predicted domain (0 or 1) for the ith example.
"""
hidden_layer = self.sigmoid(np.dot(self.W, X.T) + self.b[:, np.newaxis])
output_layer = self.sigmoid(np.dot(self.U, hidden_layer) + self.d)
return np.array(output_layer < .5, dtype=int)