1
1
import unittest
2
- import numpy as np
3
2
import torch
4
- from botorch_community .models .np_regression import NeuralProcessModel
5
3
from botorch .posteriors import GPyTorchPosterior
4
+ from botorch_community .models .np_regression import NeuralProcessModel
6
5
7
6
device = torch .device ("cuda" if torch .cuda .is_available () else "cpu" )
8
7
8
+
9
9
class TestNeuralProcessModel (unittest .TestCase ):
10
10
def initialize (self ):
11
11
self .r_hidden_dims = [16 , 16 ]
@@ -16,39 +16,39 @@ def initialize(self):
16
16
self .r_dim = 8
17
17
self .z_dim = 8
18
18
self .model = NeuralProcessModel (
19
- self .r_hidden_dims ,
19
+ torch .rand (100 , self .x_dim ),
20
+ torch .rand (100 , self .y_dim ),
21
+ self .r_hidden_dims ,
20
22
self .z_hidden_dims ,
21
23
self .decoder_hidden_dims ,
22
24
self .x_dim ,
23
25
self .y_dim ,
24
26
self .r_dim ,
25
27
self .z_dim ,
26
28
)
27
- self .x_data = np .random .rand (100 , self .x_dim )
28
- self .y_data = np .random .rand (100 , self .y_dim )
29
29
30
30
def test_r_encoder (self ):
31
31
self .initialize ()
32
- input = torch .rand (10 , self .x_dim + self .y_dim )
32
+ input = torch .rand (100 , self .x_dim + self .y_dim )
33
33
output = self .model .r_encoder (input )
34
- self .assertEqual (output .shape , (10 , self .r_dim ))
34
+ self .assertEqual (output .shape , (100 , self .r_dim ))
35
35
self .assertTrue (torch .is_tensor (output ))
36
36
37
37
def test_z_encoder (self ):
38
38
self .initialize ()
39
- input = torch .rand (10 , self .r_dim )
39
+ input = torch .rand (100 , self .r_dim )
40
40
mean , logvar = self .model .z_encoder (input )
41
- self .assertEqual (mean .shape , (10 , self .z_dim ))
42
- self .assertEqual (logvar .shape , (10 , self .z_dim ))
41
+ self .assertEqual (mean .shape , (100 , self .z_dim ))
42
+ self .assertEqual (logvar .shape , (100 , self .z_dim ))
43
43
self .assertTrue (torch .is_tensor (mean ))
44
44
self .assertTrue (torch .is_tensor (logvar ))
45
45
46
46
def test_decoder (self ):
47
47
self .initialize ()
48
- x_pred = torch .rand (10 , self .x_dim )
48
+ x_pred = torch .rand (100 , self .x_dim )
49
49
z = torch .rand (self .z_dim )
50
50
output = self .model .decoder (x_pred , z )
51
- self .assertEqual (output .shape , (10 , self .y_dim ))
51
+ self .assertEqual (output .shape , (100 , self .y_dim ))
52
52
self .assertTrue (torch .is_tensor (output ))
53
53
54
54
def test_sample_z (self ):
@@ -71,50 +71,43 @@ def test_KLD_gaussian(self):
71
71
72
72
def test_data_to_z_params (self ):
73
73
self .initialize ()
74
- x = torch .rand (10 , self .x_dim )
75
- y = torch .rand (10 , self .y_dim )
76
- mu , logvar = self .model .data_to_z_params (x , y )
74
+ mu , logvar = self .model .data_to_z_params (
75
+ self .model .train_X ,
76
+ self .model .train_Y
77
+ )
77
78
self .assertEqual (mu .shape , (self .z_dim ,))
78
79
self .assertEqual (logvar .shape , (self .z_dim ,))
79
80
self .assertTrue (torch .is_tensor (mu ))
80
81
self .assertTrue (torch .is_tensor (logvar ))
81
82
82
83
def test_forward (self ):
83
84
self .initialize ()
84
- x_t = torch .rand (5 , self .x_dim )
85
- x_c = torch .rand (10 , self .x_dim )
86
- y_c = torch .rand (10 , self .y_dim )
87
- y_t = torch .rand (5 , self .y_dim )
88
- output = self .model (x_t , x_c , y_c , y_t )
89
- self .assertEqual (output .shape , (5 , self .y_dim ))
85
+ output = self .model (self .model .train_X , self .model .train_Y )
86
+ self .assertEqual (output .loc .shape , (80 , self .y_dim ))
90
87
91
88
def test_random_split_context_target (self ):
92
89
self .initialize ()
93
90
x_c , y_c , x_t , y_t = self .model .random_split_context_target (
94
- self .x_data [:, 0 ], self .y_data , 20 , 0
91
+ self .model . train_X [:, 0 ], self .model . train_Y
95
92
)
96
93
self .assertEqual (x_c .shape [0 ], 20 )
97
94
self .assertEqual (y_c .shape [0 ], 20 )
98
95
self .assertEqual (x_t .shape [0 ], 80 )
99
96
self .assertEqual (y_t .shape [0 ], 80 )
100
-
97
+
101
98
def test_posterior (self ):
102
99
self .initialize ()
103
- x_t = torch .rand (5 , self .x_dim )
104
- x_c = torch .rand (10 , self .x_dim )
105
- y_c = torch .rand (10 , self .y_dim )
106
- y_t = torch .rand (5 , self .y_dim )
107
- output = self .model (x_t , x_c , y_c , y_t )
108
- posterior = self .model .posterior (x_t , 0.1 , 0.01 , observation_noise = True )
100
+ self .model (self .model .train_X , self .model .train_Y )
101
+ posterior = self .model .posterior (self .model .train_X , observation_noise = True )
109
102
self .assertIsInstance (posterior , GPyTorchPosterior )
110
103
mvn = posterior .mvn
111
- self .assertEqual (mvn .covariance_matrix .size (), (5 , 5 , 5 ))
112
-
104
+ self .assertEqual (mvn .covariance_matrix .size (), (100 , 100 , 100 ))
105
+
113
106
def test_transform_inputs (self ):
114
107
self .initialize ()
115
108
X = torch .rand (5 , 3 )
116
109
self .assertTrue (torch .equal (self .model .transform_inputs (X ), X .to (device )))
117
-
110
+
118
111
119
112
if __name__ == "__main__" :
120
113
unittest .main ()
0 commit comments