-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsdgp.py
More file actions
66 lines (51 loc) · 2.44 KB
/
dsdgp.py
File metadata and controls
66 lines (51 loc) · 2.44 KB
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
from layer import Layer_rbf
import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
class DSDGP():
# def __init__(self, X=None, Y=None, layers=None, Z=None, num_samples=1, name=""):
# self.layers = layers
# self.X, self.Y, self.num_samples = X, Y, num_samples
# self.num_data = X.shape[0]
# self.name=name
# if Z is not None:
# self.Z = Z
# else:
# self.Z = np.random.permutation(self.X.copy())[:layers[0].num_inducings]
def __init__(self, num_data, input_dim, output_dim, layers=None, num_samples=1, name=""):
self.num_data, self.num_samples = num_data, num_samples
self.input_dim, self.output_dim = input_dim, output_dim
self.name = name
self.layers = layers
self.Z = np.zeros((self.layers[0].num_inducings, self.input_dim))
def initialize_param(self):
X = np.random.random((self.num_data, self.input_dim))
X_running, Z_running = self.layers[0].initialize_forward(X, self.Z)
if len(self.layers) > 1:
for layer in self.layers[1:]:
X_running, Z_running = layer.initialize_forward(X_running, Z_running)
def propagate(self, X, full_cov=False, S=1, zs=None):
sX = tf.tile(tf.expand_dims(X, 0), [S, 1, 1])
Fs, Fmeans, Fvars = [], [], []
F = sX
zs = zs or [None, ] * len(self.layers)
for layer, z in zip(self.layers, zs):
F, Fmean, Fvar = layer.sample_from_conditional(F, z=z, full_cov=full_cov)
Fs.append(F)
Fmeans.append(Fmean)
Fvars.append(Fvar)
return Fs, Fmeans, Fvars
def _build_predict(self, X, full_cov=False, S=1):
Fs, Fmeans, Fvars = self.propagate(X, full_cov=full_cov, S=S)
return Fs[-1], Fmeans[-1], Fvars[-1]
def predict_f(self, Xnew, num_samples):
return self._build_predict(Xnew, full_cov=False, S=num_samples)
def predict_f_full_cov(self, Xnew, num_samples):
return self._build_predict(Xnew, full_cov=True, S=num_samples)
def predict_all_layers(self, Xnew, num_samples):
return self.propagate(Xnew, full_cov=False, S=num_samples)
def predict_all_layers_full_cov(self, Xnew, num_samples):
return self.propagate(Xnew, full_cov=True, S=num_samples)
def predict_y(self, Xnew, num_samples):
F, Fmean, Fvar = self._build_predict(Xnew, full_cov=False, S=num_samples)
return tf.identity(F)