forked from GPflow/GPflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_param.py
1222 lines (1036 loc) · 44.5 KB
/
test_param.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2016 the GPflow authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=E1123
import gpflow
import numpy as np
import pandas as pd
import pytest
import tensorflow as tf
from gpflow import GPflowError, settings
from gpflow.test_util import GPflowTestCase, session_tf
from numpy.testing import assert_allclose
class Foo(gpflow.models.Model):
def _build_likelihood(self):
return tf.zeros([1], dtype=gpflow.settings.float_type)
class TestNaming(GPflowTestCase):
def test_index(self):
index = gpflow.core.parentable.Parentable._read_index() + 1
with self.test_context():
def increment_assert(i):
p = gpflow.Param(1)
assert p.index.split("-")[-1] == i
for i in range(index, index + 5):
increment_assert(str(i))
def test_standard_name(self):
with self.test_context():
p = gpflow.Param(1)
assert p.name.startswith('Parameter')
assert p.name == p.pathname
m = gpflow.params.Parameterized()
assert m.name.startswith('Parameterized')
assert m.name == m.pathname
def test_pathname(self):
with self.test_context():
a = gpflow.Param(1)
b = gpflow.Param(1, name='test_name')
a_pathname = a.pathname
b_pathname = b.pathname
assert a.name != b.name
assert a_pathname != b_pathname
assert a_pathname == a.full_name
assert b_pathname == b.full_name
m = gpflow.params.Parameterized()
m.a = a
m.b = b
assert m.a.name != m.b.name
assert m.a.pathname != a_pathname
assert m.b.pathname != b_pathname
assert m.a.full_name != a_pathname
assert m.b.full_name != b_pathname
assert m.a.pathname.split("/")[0] == m.name
assert m.b.pathname.split("/")[0] == m.name
class TestType(GPflowTestCase):
def setUp(self):
int_type = np.int16
float_type = np.float16
test_data = [(1, int_type),
(1.0, float_type),
([1], float_type),
([1.0], float_type),
(np.array([1, 1], dtype=np.float32), np.float32),
(np.array([1, 1], dtype=np.int32), np.int32)]
self.int_type = int_type
self.float_type = float_type
self.test_data = test_data
def test_specific_dtype(self):
test_data = self.test_data + [
(1, np.float32),
(1.0, np.float64),
([1.0], np.float32),
(np.array([1, 2, 3], dtype=np.float64), np.float16)
]
with self.test_context():
for v, vtype in test_data:
p = gpflow.Param(v, dtype=vtype, autobuild=False)
self.assertEqual(p.dtype, vtype)
p.compile()
self.assertEqual(p.dtype, vtype)
def test_default_type(self):
s = gpflow.settings.get_settings()
s.dtypes.int_type = self.int_type
s.dtypes.float_type = self.float_type
with gpflow.settings.temp_settings(s), self.test_context():
for v, vtype in self.test_data:
p = gpflow.Param(v)
self.assertEqual(p.dtype, vtype)
def test_assign_fail_types(self):
with self.test_context():
param = gpflow.Param(np.array([1]), dtype=np.int32, autobuild=False)
def fail_assigns(p):
with self.assertRaises(ValueError):
p.assign([2], dtype=np.float32)
with self.assertRaises(ValueError):
p.assign(np.array([2], dtype=np.float32))
with self.assertRaises(ValueError):
p.assign(np.array([2]), dtype=np.float32)
with self.assertRaises(ValueError):
p.assign([2], dtype=np.int64)
fail_assigns(param)
param.compile()
fail_assigns(param)
class TestParameter(GPflowTestCase):
def setUp(self):
with self.test_context():
self.p = gpflow.Param(1.0)
self.m = gpflow.params.Parameterized()
self.m.p = gpflow.Param(1.0)
self.m.b = gpflow.Param(1.0)
def test_parameter_different_options(self):
with self.test_context() as session:
val = 10.
a = gpflow.Param(val)
assert_allclose(a.read_value(), val)
self.assertEqual(a.size, 1)
size = 2
val = [10.] * size
b = gpflow.Param([10.] * size, fix_shape=False)
assert_allclose(b.read_value(), val)
self.assertEqual(b.dtype, np.float64)
self.assertEqual(b.size, size)
size = 3
val = [10] * size
c = gpflow.Param(val, dtype=np.float16)
assert_allclose(c.read_value(), val)
self.assertEqual(c.dtype, np.float16)
self.assertEqual(c.size, size)
size = 4
val = [10.] * size
d = gpflow.Param(val, trainable=False)
assert_allclose(d.read_value(), val)
self.assertEqual(d.trainable, False)
self.assertEqual(d.size, size)
size = 5
val = [10.] * size
transform = gpflow.transforms.Log1pe()
e = gpflow.Param(val, transform=transform)
assert_allclose(e.read_value(), val)
self.assertEqual(e.size, size)
unconstrained = transform.backward(np.array(val))
assert_allclose(session.run(e.unconstrained_tensor), unconstrained)
size = 6
val = [10.] * size
f = gpflow.Param(val, prior=gpflow.priors.Gaussian(1, 2))
assert_allclose(f.read_value(), val)
assert_allclose(f.read_value(session), val)
self.assertEqual(f.size, size)
self.assertTrue(isinstance(f.prior, gpflow.priors.Gaussian))
def test_initialized(self):
with self.test_context() as session1:
p = gpflow.Param(1.0)
self.assertTrue(p.is_initialized(session1))
with self.test_context() as session2:
self.assertFalse(p.is_initialized(session2))
with self.test_context() as session3:
p = gpflow.Param(1.0, autobuild=False)
self.assertFalse(p.is_initialized(session1))
self.assertFalse(p.is_initialized(session2))
self.assertFalse(p.is_initialized(session3))
p.compile()
self.assertFalse(p.is_initialized(session1))
self.assertFalse(p.is_initialized(session2))
self.assertTrue(p.is_initialized(session3))
def assert_exception(args, fun, exception):
for arg in args:
with self.assertRaises(exception, msg="Raise at '{}'".format(arg)):
fun(arg)
with self.test_context():
assert_exception(['', 'non-tempty', 1.0, None, object()],
p.is_initialized, ValueError)
def test_fail_scenarios(self):
with self.test_context() as session:
p = gpflow.Param(1.0)
values = ['', 'test', 1., object(), None]
for v in values:
def value_error(value):
return self.assertRaises(ValueError, msg='Raised at "{}"'.format(value))
with value_error(v):
p.set_trainable(v)
with value_error(v):
p.trainable = v
with value_error(v):
p.is_built(v)
tensor = tf.get_variable('test', shape=())
tensor_non_trainable = tf.get_variable(
'test_non_trainable', shape=(), trainable=False)
p = gpflow.Param(tensor)
p_non_trainable = gpflow.Param(1.0, trainable=False)
with self.assertRaises(GPflowError):
p_non_trainable._check_tensor_trainable(tensor)
with self.assertRaises(GPflowError):
p._check_tensor_trainable(tensor_non_trainable)
with self.assertRaises(GPflowError):
p.read_value(session=None)
for v in ['', 'non-empty', 1.0, object()]:
with self.assertRaises(ValueError):
p.read_value(session=v)
with self.assertRaises(GPflowError):
p.set_trainable(False)
with self.assertRaises(GPflowError):
p.trainable = False
with self.assertRaises(GPflowError):
p.set_trainable(True)
with self.assertRaises(GPflowError):
p.trainable = True
values = ['', 'test', 1., object()]
for v in values:
with self.assertRaises(ValueError, msg='Raised at "{}"'.format(v)):
p.anchor(v)
with self.assertRaises(tf.errors.FailedPreconditionError):
p.anchor(session)
with self.assertRaises(ValueError):
tensor = tf.get_variable('test1', shape=(), trainable=False)
gpflow.Param(tensor)
with self.assertRaises(ValueError):
tensor = tf.get_variable('test2', shape=())
gpflow.Param(tensor, trainable=False)
def test_str(self):
with self.test_context():
def check_str(obj, expect_str):
expect = [e for e in expect_str.format(name=p.name).split(' ') if e != '']
got = [e for e in str(obj).split(' ') if e != '']
print(expect)
print(got)
self.assertEqual(expect, got)
p_str = (' class prior transform trainable shape '
'fixed_shape value\n{name} Parameter None (none)'
' True () True 1.0')
p = gpflow.Param(1., name="short")
check_str(p, p_str)
d_str = (' class shape fixed_shape value'
'\n{name} DataHolder () False 1.0')
d = gpflow.DataHolder(1., name="short")
check_str(d, d_str)
params_str = (' class prior transform trainable shape'
' fixed_shape value\n{name}/p Parameter None'
' (none) True () True 1.0')
params = gpflow.Parameterized(name="short")
params.p = p
params.d = d
check_str(params, params_str)
def test_generators(self):
with self.test_context():
self.assertEqual(len(list(self.m.parameters)), 2)
self.assertEqual(len(list(self.m.data_holders)), 0)
self.assertEqual(len(list(self.m.params)), 2)
def test_assign(self):
with self.test_context(tf.Graph()) as session:
with self.assertRaises(GPflowError):
self.p.read_value(session)
with self.test_context() as session:
self.p.assign(2.0)
self.assertEqual(self.p.read_value(), 2.0)
self.assertEqual(self.p.value, 2.0)
self.m.p = 2.0
self.assertEqual(self.m.p.read_value(), 2.0)
self.assertEqual(self.m.p.value, 2.0)
self.p.assign(100.0, session=session)
self.assertEqual(self.p.read_value(session), 100.0)
self.assertEqual(self.p.value, 100.0)
def test_assign_tensor(self):
with self.test_context():
tensor = tf.get_variable('a', shape=())
param = gpflow.Param(tensor)
with self.assertRaises(GPflowError):
param.assign(10)
def test_floating_assign(self):
with self.test_context():
val = 10.
p = gpflow.Param(val, fix_shape=False)
assert_allclose(p.read_value(), val)
val = [10, 10]
p.assign(val)
assert_allclose(p.read_value(), val)
val = [10, 10, 10]
p.assign(val)
assert_allclose(p.read_value(), val)
val = [[10, 10, 10], [10, 10, 10]]
p.assign(val)
assert_allclose(p.read_value(), val)
with self.test_context():
val = 10.
p = gpflow.Param(val)
val = [10., 10.]
with self.assertRaises(ValueError):
p.assign(val)
val = [[10.]]
with self.assertRaises(ValueError):
p.assign(val)
def test_create_and_replace(self):
with self.test_context():
tensor = tf.get_variable('a', shape=()) + 1.0
param = gpflow.Param(1e3)
with self.assertRaises(ValueError):
external_param = gpflow.Param(tensor)
external_param = gpflow.Param(tensor, trainable=False)
new_param = gpflow.Param(1.0, name='new_param')
self.m.b = external_param
self.assertEqual(self.m.b, external_param)
p = self.m.p
self.m.p = param
assert self.m.p is param
assert p.name.startswith('Parameter')
assert p.root is p
self.m.d = new_param
assert self.m.d is new_param
assert self.m.d.pathname == '{name}/d'.format(name=self.m.name)
def test_assign_with_compile(self):
with self.test_context():
self.p.compile()
self.m.compile()
self.p.assign(2.0)
self.m.p = 2.0
self.assertEqual(self.p.read_value(), 2.0)
self.assertEqual(self.m.p.read_value(), 2.0)
def test_root(self):
self.assertTrue(self.m.p.root is self.m)
def test_existing_tensor(self):
with self.test_context():
_ = tf.get_variable('param/unconstrained', shape=())
with self.assertRaises(GPflowError):
p = gpflow.Param(1.0, name='param')
def test_trainable(self):
self.assertTrue(self.p.trainable)
self.p.trainable = False
self.assertFalse(self.p.trainable)
self.assertTrue(self.m.trainable)
self.m.p.trainable = False
self.assertFalse(self.m.p.trainable)
self.assertTrue(self.m.trainable)
def test_trainable_with_compile(self):
with self.test_context():
self.p.compile()
self.m.compile()
self.assertTrue(self.p.trainable)
self.p.trainable = False
self.assertFalse(self.p.trainable)
self.assertTrue(self.m.trainable)
self.m.p.trainable = False
self.assertTrue(self.m.trainable)
self.assertFalse(self.m.p.trainable)
_check_trainable_flag(self.m, self.assertTrue, self.assertFalse)
def test_fixed_shape(self):
with self.test_context():
p = gpflow.Param(1., fix_shape=False)
self.assertFalse(p.fixed_shape)
self.assertAllEqual(p.shape, ())
self.assertEqual(p.size, 1)
p.assign([10., 10.])
self.assertFalse(p.fixed_shape)
self.assertAllEqual(p.shape, (2,))
self.assertEqual(p.size, 2)
p.fix_shape()
self.assertTrue(p.fixed_shape)
self.assertAllEqual(p.shape, (2,))
self.assertEqual(p.size, 2)
p.assign(np.zeros(p.shape))
with self.assertRaises(ValueError):
p.assign([1.], force=True)
with self.assertRaises(ValueError):
p.assign(1., force=True)
with self.assertRaises(ValueError):
p.assign(np.zeros((3,3)), force=True)
class TestParameterized(GPflowTestCase):
@staticmethod
def create_layout():
p = gpflow.Parameterized(name='p')
p.a = gpflow.Param(10.)
p.b = gpflow.Param(11.)
p.c = gpflow.Parameterized()
p.c.d = gpflow.Param(12., fix_shape=False)
p.c.e = gpflow.DataHolder(13.)
return p
def test_is_built(self):
with self.test_context():
p = gpflow.Parameterized()
self.assertTrue(p.is_built_coherence())
# TODO(@awav): Should it be NO?
self.assertEqual(p.is_built_coherence(tf.Graph()), gpflow.Build.YES)
values = [None, "", 1.0, object()]
for v in values:
with self.assertRaises(ValueError, msg='Passed value {}'.format(v)):
p.is_built(v)
p.a = gpflow.Param(1.0)
self.assertEqual(p.is_built_coherence(), gpflow.Build.NO)
p.compile()
not_compatible = gpflow.Build.NOT_COMPATIBLE_GRAPH
self.assertTrue(p.is_built_coherence())
self.assertEqual(p.is_built(tf.Graph()), not_compatible)
with self.assertRaises(GPflowError):
p.is_built_coherence(tf.Graph())
for v in values:
with self.assertRaises(ValueError, msg='Passed value "{}"'.format(v)):
p.is_built(v)
def test_anchor(self):
with self.test_context() as session:
p = gpflow.Parameterized()
p.a = gpflow.Param(1.0)
p.compile()
with self.assertRaises(ValueError):
p.anchor(None)
new_value = 2.0
p.a.parameter_tensor.load(new_value)
p.anchor(session)
assert_allclose(p.a.read_value(), new_value)
def test_read_values(self):
def check_values(values, expected_dict, unexpected_dicts):
self.assertTrue(values == expected_dict)
for unexpected_dict in unexpected_dicts:
self.assertFalse(values == unexpected_dict)
expected_dict = {'p/a': 10., 'p/b': 11., 'p/c/d': 12.}
unexpected_dicts = [
{'p': 10., 'p/b': 11., 'p/c/d': 12.},
{'p/a': 11., 'p/b': 11., 'p/c/d': 12.},
{'p/a': 11.}
]
with self.test_context() as session:
session_new = tf.Session(graph=session.graph)
self.assertNotEqual(session_new, session)
with session_new.as_default():
with gpflow.defer_build():
p = self.create_layout()
values = p.read_values()
check_values(values, expected_dict, unexpected_dicts)
p.compile()
values = p.read_values()
check_values(values, expected_dict, unexpected_dicts)
with self.assertRaises(tf.errors.FailedPreconditionError):
p.read_values(session=session)
with self.test_context() as session_fail:
self.assertFalse(session == session_fail)
with self.assertRaises(tf.errors.FailedPreconditionError):
p.read_values(session=session_fail)
with self.test_context() as session_intialize:
p.initialize(session=session_intialize)
values = p.read_values(session=session_intialize)
check_values(values, expected_dict, unexpected_dicts)
values = p.read_values(session=session_new)
check_values(values, expected_dict, unexpected_dicts)
session_new.close()
def test_parameterized_assign(self):
with self.test_context():
## Create parameterized object inside context
p = self.create_layout()
values = p.read_values()
values['p/b'] = 100.
values['p/c/d'] = 100.
p.assign(values)
assert_allclose(p.a.read_value(), 10)
assert_allclose(p.b.read_value(), 100)
assert_allclose(p.c.d.read_value(), 100)
values = list(map(float, p.read_values().values()))
self.assertTrue(set(values) == set([10, 100, 100]))
with self.test_context() as session:
assign_values = {'p/a': 1e3, 'p/c/d': 1e4}
p.assign(assign_values, session=session)
assert_allclose(p.a.read_value(), 1e3)
assert_allclose(p.b.read_value(), 100)
assert_allclose(p.c.d.read_value(), 1e4)
values = list(map(float, p.read_values().values()))
self.assertTrue(set(values) == set([1e3, 100, 1e4]))
def test_parameterized_assign_panda(self):
with self.test_context():
p = self.create_layout()
vals1 = [1e2, 1e3, 1e4]
vals2 = [2e2, 2e3, 2e4]
self.assertEqual(len(vals1), len(vals2))
df1 = pd.DataFrame({'p/a': vals1, 'p/c/d': vals1})
df2 = pd.DataFrame({'p/a': vals2, 'p/c/d': vals2})
for i in range(len(vals1)):
df_slice1 = df1.iloc[i]
p.assign(df_slice1, force=False)
values = p.read_values()
for name in df_slice1.index:
assert_allclose(df_slice1[name], values[name])
df_slice2 = df2.iloc[i]
p.assign(df_slice2, force=True)
values = p.read_values()
for name in df_slice2.index:
assert_allclose(df_slice2[name], values[name])
def test_fail_assign(self):
with self.test_context():
p = self.create_layout()
values = [1.0, {'a': 1.0}, None, "", "artem", object()]
for v in values:
with self.assertRaises(ValueError):
p.assign(v)
different_shape = {
'p/a': np.zeros((10, 1)),
'p/b': -1,
'p/c/d': -1
}
a = p.a.read_value()
b = p.b.read_value()
c_d = p.c.d.read_value()
with self.assertRaises(ValueError):
p.assign(different_shape)
assert_allclose(p.a.read_value(), a)
assert_allclose(p.b.read_value(), b)
assert_allclose(p.c.d.read_value(), c_d)
def test_fix_shapes(self):
with self.test_context():
def children(p):
yield from p.parameters
yield from p.data_holders
p = self.create_layout()
self.assertFalse(all([c.fixed_shape for c in children(p)]))
p.fix_shape()
self.assertTrue(all([c.fixed_shape for c in children(p)]))
p = self.create_layout()
p.fix_shape(parameters=False, data_holders=True)
self.assertTrue(all([c.fixed_shape for c in p.data_holders]))
p.fix_shape(parameters=True)
self.assertTrue(all([c.fixed_shape for c in p.parameters]))
self.assertTrue(all([c.fixed_shape for c in children(p)]))
def test_trainables(self):
with self.test_context():
p = self.create_layout()
self.assertTrue(all([c.trainable for c in p.parameters]))
self.assertTrue(p.trainable)
p.set_trainable(False)
self.assertFalse(all([c.trainable for c in p.parameters]))
self.assertFalse(p.trainable)
p.set_trainable(True)
self.assertTrue(all([c.trainable for c in p.parameters]))
self.assertTrue(p.trainable)
values = [None, "test", "", 1]
for v in values:
with self.assertRaises(ValueError, msg='Caught exception for "{}"'.format(v)):
p.set_trainable(v)
class TestParameterizedNoParameters(GPflowTestCase):
def setUp(self):
with self.test_context(), gpflow.defer_build():
self.m = gpflow.params.Parameterized(name='m')
self.m.p = gpflow.params.Parameterized()
self.m.b = gpflow.params.Parameterized()
def test_feeds_empty(self):
with self.test_context():
p = gpflow.Parameterized()
self.assertEqual(p.initializables, [])
self.assertEqual(p.initializable_feeds, {})
self.assertEqual(p.feeds, {})
def test_is_built(self):
with self.test_context():
self.assertEqual(self.m.is_built_coherence(), gpflow.Build.YES)
def test_compile(self):
with self.test_context():
self.m.compile()
self.assertEqual(self.m.is_built_coherence(), gpflow.Build.YES)
def test_generators(self):
with self.test_context():
self.assertEqual(list(self.m.parameters), [])
self.assertEqual(list(self.m.data_holders), [])
self.assertEqual(len(list(self.m.params)), 2)
def test_add_parameter_to_empty_parameterized(self):
with self.test_context():
self.m.compile()
self.m.a = gpflow.Param(10)
self.assertEqual(self.m.is_built_coherence(), gpflow.Build.NO)
self.m.compile()
self.assertEqual(self.m.is_built_coherence(), gpflow.Build.YES)
with self.assertRaises(GPflowError):
self.m.b = gpflow.Param(20)
class TestParameterizedCompile(GPflowTestCase):
def setUp(self):
self.test_graph = tf.Graph()
with self.test_context() as session:
self.graph = session.graph
tensor = tf.get_variable('a', shape=())
self.m = gpflow.params.Parameterized(name='m')
self.m.p = gpflow.params.Parameterized()
self.m.a = gpflow.Param(tensor)
self.m.b = gpflow.Param(1.0, trainable=False)
self.m.c = gpflow.Param(np.array([1.0, 2.0]))
self.m.p.d = gpflow.Param(1.0)
def test_compile(self):
with self.test_context():
tensor = self.m.a.parameter_tensor
self.m.compile()
self.assertEqual(len(list(self.m.parameters)), 4)
self.assertEqual(len(list(self.m.trainable_tensors)), 3)
self.assertEqual(self.m.a.parameter_tensor, tensor)
for param in self.m.parameters:
self.assertTrue(gpflow.misc.is_tensor(param.parameter_tensor))
self.assertTrue(gpflow.misc.is_tensor(param.constrained_tensor))
self.assertTrue(gpflow.misc.is_tensor(param.prior_tensor))
def test_modify_compiled(self):
with self.test_context():
self.assertEqual(len(list(self.m.parameters)), 4)
self.assertEqual(len(list(self.m.trainable_tensors)), 3)
for param in self.m.parameters:
self.assertTrue(gpflow.misc.is_tensor(param.parameter_tensor))
self.assertTrue(gpflow.misc.is_tensor(param.constrained_tensor))
self.assertTrue(gpflow.misc.is_tensor(param.prior_tensor))
def test_fails_after_compile(self):
with self.test_context(self.graph):
self.m.compile()
with self.assertRaises(GPflowError):
self.m.d = gpflow.Param(1.0)
with self.assertRaises(AttributeError):
_param = self.m.d
def test_compile(self):
with self.test_context():
self.m.compile()
with self.test_context() as session:
self.m.compile(session=session)
class TestAutobuild(GPflowTestCase):
def test_autobuild_option(self):
with self.test_context():
foo = Foo(autobuild=False)
equal = self.assertEqual
equal(foo.is_built(tf.get_default_graph()), gpflow.Build.NO)
equal(foo.is_built_coherence(), gpflow.Build.NO)
p = gpflow.Param(10)
equal(p.is_built(tf.get_default_graph()), gpflow.Build.YES)
equal(p.is_built_coherence(), gpflow.Build.YES)
b = gpflow.Param(10, autobuild=False)
equal(b.is_built(tf.get_default_graph()), gpflow.Build.NO)
equal(b.is_built_coherence(), gpflow.Build.NO)
foo.p = p
equal(foo.p, p)
equal(hasattr(foo, 'p'), True)
equal(foo.is_built(tf.get_default_graph()), gpflow.Build.NO)
equal(foo.is_built_coherence(), gpflow.Build.NO)
foo.b = b
equal(foo.b, b)
equal(hasattr(foo, 'b'), True)
equal(foo.is_built(tf.get_default_graph()), gpflow.Build.NO)
equal(foo.is_built_coherence(), gpflow.Build.NO)
foo.compile()
equal(foo.is_built(tf.get_default_graph()), gpflow.Build.YES)
equal(foo.is_built_coherence(), gpflow.Build.YES)
equal(p.is_built(tf.get_default_graph()), gpflow.Build.YES)
equal(p.is_built_coherence(), gpflow.Build.YES)
equal(b.is_built(tf.get_default_graph()), gpflow.Build.YES)
equal(b.is_built_coherence(), gpflow.Build.YES)
class TestParameterizedDeep(GPflowTestCase):
def setUp(self):
with self.test_context():
self.m = gpflow.params.Parameterized(name='m')
self.m.a = gpflow.Param(1.0, trainable=False)
self.m.foo = gpflow.params.Parameterized()
self.m.foo.bar = gpflow.params.Parameterized()
self.m.foo.bar.baz = gpflow.Param(1.0)
def test_generators(self):
with self.test_context():
self.assertEqual(len(list(self.m.parameters)), 2)
self.assertEqual(len(list(self.m.data_holders)), 0)
self.assertEqual(len(list(self.m.params)), 2)
def test_root(self):
self.assertTrue(self.m.foo.root is self.m)
self.assertTrue(self.m.foo.bar.root is self.m)
self.assertTrue(self.m.foo.bar.baz.root is self.m)
def test_deep_name(self):
assert self.m.foo.pathname == 'm/foo'
assert self.m.foo.bar.pathname == 'm/foo/bar'
assert self.m.foo.bar.baz.pathname == 'm/foo/bar/baz'
def test_deep_trainable(self):
with self.test_context():
self.m.compile()
self.m.trainable = False
self.assertEqual(len(list(self.m.trainable_tensors)), 0)
_check_trainable_flag(self.m, self.assertTrue, self.assertFalse)
self.m.trainable = True
self.assertEqual(
len(list(self.m.parameters)),
len(list(self.m.trainable_tensors)))
_check_trainable_flag(self.m, self.assertTrue, self.assertFalse)
class TestParamLikeInvariant(GPflowTestCase):
def test_self_reference(self):
m = gpflow.params.Parameterized()
with self.assertRaises(ValueError):
m.foo = m
m.foo = gpflow.params.Parameterized()
with self.assertRaises(ValueError):
m.foo.bar = m
def test_reassign(self):
m = gpflow.params.Parameterized()
p = gpflow.params.Parameterized()
m.foo = p # assign
m.foo = p # reassign
# TODO(@awav):
# m = gpflow.params.Parameterized()
# m.foo = gpflow.params.Parameterized()
# m.foo.bar = gpflow.params.Parameterized()
# with self.assertRaises(ValueError):
# m.baz = m.foo.bar
# TODO(@awav):
#m = gpflow.params.Parameterized()
#m.foo = gpflow.params.Parameterized()
#m.foo.bar = gpflow.params.Parameterized()
#m.boo = gpflow.params.Parameterized()
#with self.assertRaises(ValueError):
# m.boo.far = m.foo.bar
# TODO(@awav):
# def testAddingToAnother(self):
# """
# Adding the same Paramterized object to another tree is fine.
# """
# m1 = gpflow.params.Parameterized()
# m1.foo = gpflow.params.Parameterized()
# m2 = gpflow.params.Parameterized()
# with self.assertRaises(GPflowError):
# m2.foo = m1.foo
class TestParamList(GPflowTestCase):
def test_construction(self):
with self.test_context():
gpflow.ParamList([])
gpflow.ParamList([gpflow.Param(1)])
gpflow.ParamList([1.0, np.array([1, 2]), gpflow.Param(1.0)])
with self.assertRaises(ValueError):
gpflow.ParamList([gpflow.Param(1), 'stringsnotallowed'])
with self.assertRaises(ValueError):
# tuples not valid in constuctor:
gpflow.ParamList((gpflow.Param(1),))
with self.assertRaises(ValueError):
# param objects not valid in constructor (must be in list)
gpflow.ParamList(gpflow.Param(1))
with gpflow.defer_build():
p = gpflow.ParamList([0.0])
p[0] = gpflow.Param(1.0)
with self.assertRaises(ValueError):
p[0] = 1.0
with self.assertRaises(ValueError):
p[0] = "test"
p = gpflow.ParamList([])
p.append(gpflow.Param(1.0))
p.append(gpflow.Param(2.0))
p.append(2.0)
self.assertEqual(len(p), 3)
with self.assertRaises(ValueError):
p.append("test")
def test_naming(self):
with self.test_context():
p1 = gpflow.Param(1.2)
p2 = gpflow.Param(np.array([3.4, 5.6], settings.float_type))
l = gpflow.ParamList([p1, p2])
assert p1.pathname == l.name + '/0'
assert p2.pathname == l.name + '/1'
def test_setitem(self):
with self.test_context():
p1 = gpflow.Param(1.2)
p2 = gpflow.Param(np.array([3.4, 5.6], settings.float_type))
param_list = gpflow.ParamList([p1, p2], name='param_list', autobuild=False)
self.assertEqual(p1.read_value(), param_list[0].read_value())
self.assertTrue(np.all(param_list[1].read_value() == p2.read_value()))
param_list[0] = gpflow.Param(2.0)
self.assertEqual(p1.read_value(), 1.2)
self.assertEqual(p1.root, p1)
self.assertEqual(param_list[0].read_value(), 2.0)
arr = np.array([1.1, 2.2], settings.float_type)
param_list[1] = gpflow.Param(arr)
self.assertEqual(p2.root, p2)
self.assertTrue(np.all(param_list[1].read_value() == arr))
param_list.compile()
with self.assertRaises(GPflowError):
param_list[0] = gpflow.Param(12)
def test_append(self):
with self.test_context():
p1 = gpflow.Param(1.2)
p4 = gpflow.Param(np.array([3.4, 5.6], settings.float_type))
with gpflow.defer_build():
p2 = gpflow.Param(1.2)
param_list = gpflow.ParamList([p1])
param_list.append(p2)
p3 = gpflow.Param(1.2)
param_list.append(p3)
param_list.compile()
with self.assertRaises(gpflow.GPflowError):
param_list.append(p4)
self.assertTrue(p1 in param_list.params)
self.assertTrue(p2 in param_list.params)
self.assertTrue(p3 in param_list.params)
self.assertFalse(p4 in param_list.params)
with self.assertRaises(ValueError):
param_list.append('foo')
def test_len(self):
with self.test_context():
p1 = gpflow.Param(1.2)
p2 = gpflow.Param(np.array([3.4, 5.6], settings.float_type))
l = gpflow.ParamList([p1, p2])
self.assertTrue(len(l) == 2)
def test_with_parameterized(self):
with self.test_context():
pzd = gpflow.params.Parameterized()
p = gpflow.Param(1.2)
pzd.p = p
param_list = gpflow.ParamList([pzd])
param_list[0].p = 5.
self.assertEqual(param_list[0].p.read_value(), 5)
def test_in_model(self):
class Foo(gpflow.models.Model):
def __init__(self):
gpflow.models.Model.__init__(self)
self.param_list = gpflow.ParamList([gpflow.Param(1.), gpflow.Param(12.)])
@gpflow.params_as_tensors
def _build_likelihood(self):
return -tf.add_n([tf.square(x) for x in self.param_list])
with self.test_context():
m = Foo()
m.compile()
optimizer = gpflow.train.ScipyOptimizer()
optimizer.minimize(m, maxiter=10)
atol = 1e-6 if settings.float_type is np.float32 else 1e-8
params = [param.read_value() for param in m.parameters]
self.assertTrue(np.allclose(params, 0., atol=atol))
class TestFixWithPrior(GPflowTestCase):
"""
This tests that models with a fixed parameter which has a prior continue to work
"""
def test_non_trainable_with_prior(self):
with self.test_context():
m = Foo(autobuild=False)
m.p = gpflow.Param(1.0, gpflow.transforms.positive, autobuild=False)
m.pp = gpflow.Param(1.0, gpflow.transforms.positive, autobuild=False)
m.p.prior = gpflow.priors.Gamma(1, 1)
m.pp.prior = gpflow.priors.Gamma(1, 1)
m.p.trainable = False
m.compile()
optimizer = gpflow.train.ScipyOptimizer()
optimizer.minimize(m, maxiter=10)
#class TestRandomizeDefault(GPflowTestCase):
# """
# This tests that distributions can sample random values without priors
# """
#
# def test(self):
# with self.test_context():
# np.random.seed(1)
# m = gpflow.models.Model()
# m.p = gpflow.Param(1.0)
# m.pp = gpflow.Param(1.0, gpflow.transforms.Log1pe())
# m.pf = gpflow.Param(1.0)