forked from simpeg/simpeg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_Objectivefct.py
328 lines (229 loc) · 9.2 KB
/
test_Objectivefct.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
import numpy as np
import scipy.sparse as sp
import unittest
from SimPEG import ObjectiveFunction, Utils, Maps
np.random.seed(130)
EPS = 1e-9
class Empty_ObjFct(ObjectiveFunction.BaseObjectiveFunction):
def __init__(self):
super(Empty_ObjFct, self).__init__()
class Error_if_Hit_ObjFct(ObjectiveFunction.BaseObjectiveFunction):
def __init__(self):
super(Error_if_Hit_ObjFct, self).__init__()
def __call__(self, m):
raise Exception('entered __call__')
def deriv(self, m):
raise Exception('entered deriv')
def deriv2(self, m, v=None):
raise Exception('entered deriv2')
class TestBaseObjFct(unittest.TestCase):
def test_derivs(self):
objfct = ObjectiveFunction.L2ObjectiveFunction()
self.assertTrue(objfct.test(eps=1e-9))
def test_deriv2(self):
nP=100
mapping=Maps.ExpMap(nP=nP)
m = np.random.rand(nP)
v = np.random.rand(nP)
objfct = ObjectiveFunction.L2ObjectiveFunction(nP=nP, mapping=mapping)
self.assertTrue(
np.allclose(objfct.deriv2(m=m, v=v), objfct.deriv2(m=m)*v)
)
def test_scalarmul(self):
scalar = 10.
nP = 100
objfct_a = ObjectiveFunction.L2ObjectiveFunction(
W=Utils.sdiag(np.random.randn(nP))
)
objfct_b = scalar * objfct_a
m = np.random.rand(nP)
objfct_c = objfct_a + objfct_b
self.assertTrue(scalar * objfct_a(m) == objfct_b(m))
self.assertTrue(objfct_b.test())
self.assertTrue(objfct_c(m) == objfct_a(m) + objfct_b(m))
self.assertTrue(len(objfct_c.objfcts) == 2)
self.assertTrue(len(objfct_c.multipliers) == 2)
self.assertTrue(len(objfct_c) == 2)
def test_sum(self):
scalar = 10.
nP = 100.
objfct = (
ObjectiveFunction.L2ObjectiveFunction(W=sp.eye(nP)) +
scalar * ObjectiveFunction.L2ObjectiveFunction(W=sp.eye(nP))
)
self.assertTrue(objfct.test(eps=1e-9))
self.assertTrue(np.all(objfct.multipliers == np.r_[1., scalar]))
def test_2sum(self):
nP = 80
alpha1 = 100
alpha2 = 200
phi1 = (
ObjectiveFunction.L2ObjectiveFunction(W=Utils.sdiag(np.random.rand(nP))) +
alpha1 * ObjectiveFunction.L2ObjectiveFunction()
)
phi2 = ObjectiveFunction.L2ObjectiveFunction() + alpha2 * phi1
self.assertTrue(phi2.test(eps=EPS))
self.assertTrue(len(phi1.multipliers) == 2)
self.assertTrue(len(phi2.multipliers) == 2)
self.assertTrue(len(phi1.objfcts) == 2)
self.assertTrue(len(phi2.objfcts) == 2)
self.assertTrue(len(phi2) == 2)
self.assertTrue(len(phi1) == 2)
self.assertTrue(len(phi2) == 2)
self.assertTrue(np.all(phi1.multipliers == np.r_[1., alpha1]))
self.assertTrue(np.all(phi2.multipliers == np.r_[1., alpha2]))
def test_3sum(self):
nP = 90
alpha1 = 0.3
alpha2 = 0.6
alpha3inv = 9
phi1 = ObjectiveFunction.L2ObjectiveFunction(W=sp.eye(nP))
phi2 = ObjectiveFunction.L2ObjectiveFunction(W=sp.eye(nP))
phi3 = ObjectiveFunction.L2ObjectiveFunction(W=sp.eye(nP))
phi = alpha1 * phi1 + alpha2 * phi2 + phi3 / alpha3inv
m = np.random.rand(nP)
self.assertTrue(
np.all(phi.multipliers == np.r_[alpha1, alpha2, 1./alpha3inv])
)
self.assertTrue(
np.allclose((alpha1*phi1(m) + alpha2*phi2(m) + phi3(m)/alpha3inv), phi(m))
)
self.assertTrue(len(phi.objfcts) == 3)
self.assertTrue(phi.test())
def test_sum_fail(self):
nP1 = 10
nP2 = 30
phi1 = ObjectiveFunction.L2ObjectiveFunction(
W=Utils.sdiag(np.random.rand(nP1))
)
phi2 = ObjectiveFunction.L2ObjectiveFunction(
W=Utils.sdiag(np.random.rand(nP2))
)
with self.assertRaises(Exception):
phi = phi1 + phi2
with self.assertRaises(Exception):
phi = phi1 + 100 * phi2
def test_emptyObjFct(self):
phi = Empty_ObjFct()
x = np.random.rand(20)
with self.assertRaises(NotImplementedError):
phi(x)
phi.deriv(x)
phi.deriv2(x)
def test_ZeroObjFct(self):
# This is not a combo objective function, it will just give back an
# L2 objective function. That might be ok? or should this be a combo
# objective function?
nP = 20
alpha = 2.
phi = alpha*(
ObjectiveFunction.L2ObjectiveFunction(W = sp.eye(nP)) +
Utils.Zero()*ObjectiveFunction.L2ObjectiveFunction()
)
self.assertTrue(len(phi.objfcts) == 1)
self.assertTrue(phi.test())
def test_updateMultipliers(self):
nP = 10
m = np.random.rand(nP)
W1 = Utils.sdiag(np.random.rand(nP))
W2 = Utils.sdiag(np.random.rand(nP))
phi1 = ObjectiveFunction.L2ObjectiveFunction(W=W1)
phi2 = ObjectiveFunction.L2ObjectiveFunction(W=W2)
phi = phi1 + phi2
self.assertTrue(phi(m) == phi1(m) + phi2(m))
phi.multipliers[0] = Utils.Zero()
self.assertTrue(phi(m) == phi2(m))
phi.multipliers[0] = 1.
phi.multipliers[1] = Utils.Zero()
self.assertTrue(len(phi.objfcts) == 2)
self.assertTrue(len(phi.multipliers) == 2)
self.assertTrue(len(phi) == 2)
self.assertTrue(phi(m) == phi1(m))
def test_early_exits(self):
nP = 10
m = np.random.rand(nP)
v = np.random.rand(nP)
W1 = Utils.sdiag(np.random.rand(nP))
phi1 = ObjectiveFunction.L2ObjectiveFunction(W=W1)
phi2 = Error_if_Hit_ObjFct()
objfct = phi1 + 0*phi2
self.assertTrue(len(objfct) == 2)
self.assertTrue(np.all(objfct.multipliers == np.r_[1, 0]))
self.assertTrue(objfct(m) == phi1(m))
self.assertTrue(np.all(objfct.deriv(m) == phi1.deriv(m)))
self.assertTrue(np.all(objfct.deriv2(m, v) == phi1.deriv2(m, v)))
objfct.multipliers[1] = Utils.Zero()
self.assertTrue(len(objfct) == 2)
self.assertTrue(np.all(objfct.multipliers == np.r_[1, 0]))
self.assertTrue(objfct(m) == phi1(m))
self.assertTrue(np.all(objfct.deriv(m) == phi1.deriv(m)))
self.assertTrue(np.all(objfct.deriv2(m, v) == phi1.deriv2(m, v)))
def test_Maps(self):
nP = 10
m = np.random.rand(2*nP)
wires = Maps.Wires(('sigma', nP), ('mu', nP))
objfct1 = ObjectiveFunction.L2ObjectiveFunction(mapping=wires.sigma)
objfct2 = ObjectiveFunction.L2ObjectiveFunction(mapping=wires.mu)
objfct3 = objfct1 + objfct2
objfct4 = ObjectiveFunction.L2ObjectiveFunction(nP=nP)
self.assertTrue(objfct1.nP == 2*nP)
self.assertTrue(objfct2.nP == 2*nP)
self.assertTrue(objfct3.nP == 2*nP)
# print(objfct1.nP, objfct4.nP, objfct4.W.shape, objfct1.W.shape, m[:nP].shape)
self.assertTrue(objfct1(m) == objfct4(m[:nP]))
self.assertTrue(objfct2(m) == objfct4(m[nP:]))
self.assertTrue(objfct3(m) == objfct1(m) + objfct2(m))
objfct1.test()
objfct2.test()
objfct3.test()
def test_ComboW(self):
nP = 15
m = np.random.rand(nP)
phi1 = ObjectiveFunction.L2ObjectiveFunction(nP=nP)
phi2 = ObjectiveFunction.L2ObjectiveFunction(nP=nP)
alpha1 = 2.
alpha2 = 0.5
phi = alpha1*phi1 + alpha2*phi2
r = phi.W * m
r1 = phi1.W * m
r2 = phi2.W * m
print(phi(m), 0.5*np.inner(r, r))
self.assertTrue(np.allclose(phi(m), 0.5*np.inner(r, r)))
self.assertTrue(np.allclose(phi(m), 0.5*(
alpha1*np.inner(r1, r1) + alpha2*np.inner(r2, r2))
))
def test_ComboConstruction(self):
nP = 10
m = np.random.rand(nP)
v = np.random.rand(nP)
phi1 = ObjectiveFunction.L2ObjectiveFunction(nP=nP)
phi2 = ObjectiveFunction.L2ObjectiveFunction(nP=nP)
phi3 = 2*phi1 + 3*phi2
phi4 = ObjectiveFunction.ComboObjectiveFunction(
[phi1, phi2], [2, 3]
)
self.assertTrue(phi3(m) == phi4(m))
self.assertTrue(np.all(phi3.deriv(m) == phi4.deriv(m)))
self.assertTrue(np.all(phi3.deriv2(m, v) == phi4.deriv2(m, v)))
def test_updating_multipliers(self):
nP = 20
phi1 = ObjectiveFunction.L2ObjectiveFunction(nP=nP)
phi2 = ObjectiveFunction.L2ObjectiveFunction(nP=nP)
phi3 = 2*phi1 + 4*phi2
self.assertTrue(all(phi3.multipliers == np.r_[2, 4]))
phi3.multipliers[1] = 3
self.assertTrue(all(phi3.multipliers == np.r_[2, 3]))
phi3.multipliers = np.r_[1., 5.]
self.assertTrue(all(phi3.multipliers == np.r_[1., 5.]))
with self.assertRaises(Exception):
phi3.multipliers[0] = 'a'
with self.assertRaises(Exception):
phi3.multipliers = np.r_[0., 3., 4.]
with self.assertRaises(Exception):
phi3.multipliers = ['a', 'b']
if __name__ == '__main__':
unittest.main()