Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import gpflowopt
import gpflow
import numpy as np
from .utility import create_parabola_model, GPflowOptTestCase
float_type = gpflow.settings.dtypes.float_type
class MethodOverride(gpflowopt.models.ModelWrapper):
def __init__(self, m):
super(MethodOverride, self).__init__(m)
self.A = gpflow.param.DataHolder(np.array([1.0]))
@gpflow.param.AutoFlow((float_type, [None, None]))
def predict_f(self, Xnew):
"""
Compute the mean and variance of held-out data at the points Xnew
"""
m, v = self.build_predict(Xnew)
return self.A * m, v
def prepare(self, dimF, dimY, num=10):
feed = {}
def make_tensor(data, dtype=default_float()):
tensor = tf.placeholder(dtype)
feed[tensor] = data.astype(dtype)
return tensor
dF = np.vstack((self.rng.randn(num - 3, dimF), np.array([[-3., 0.], [3, 0.], [0., 0.]]))) if dimF == 2 else \
self.rng.randn(num, dimF)
dY = np.vstack((self.rng.randn(num - 3, dimY), np.ones((3, dimY)))) > 0
F = make_tensor(dF)
Y = make_tensor(dY, settings.int_type) # 0 or 1
return F, Y, feed
def testContextManager(self):
orig = gpflow.settings.verbosity.tf_compile_verb
gpflow.settings.verbosity.tf_compile_verb = True
config = gpflow.settings.get_settings()
config.verbosity.tf_compile_verb = False
self.assertEqual(gpflow.settings.verbosity.tf_compile_verb, True)
with gpflow.settings.temp_settings(config):
self.assertEqual(gpflow.settings.verbosity.tf_compile_verb, False)
self.assertEqual(gpflow.settings.verbosity.tf_compile_verb, True)
gpflow.settings.verbosity.tf_compile_verb = orig
def test_autoflow(self):
m = self.prepare()
s = gpflow.settings.get_settings()
s.profiling.dump_timeline = True
s.profiling.output_directory = tf.test.get_temp_dir()
s.profiling.output_file_name = 'test_trace_autoflow'
with gpflow.settings.temp_settings(s):
with gpflow.session_manager.get_session().as_default():
m.compile()
m.kern.compute_K_symm(m.X.read_value())
directory = s.profiling.output_directory
filename = s.profiling.output_file_name + '.json'
expected_file = os.path.join(directory, filename)
self.assertTrue(os.path.exists(expected_file))
os.remove(expected_file)
m.clear()
s.profiling.output_directory = tf.test.get_temp_dir()
m.compile()
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 cast(x):
return tf.cast(np.asarray(x), dtype=gpflow.settings.float_type)
def convgp_fit(train_data, test_data, iterations, float_type, jitter_level):
custom_settings = gpflow.settings.get_settings()
custom_settings.dtypes.float_type = getattr(np, float_type)
custom_settings.numerics.jitter_level = jitter_level
gpflow.settings.push(custom_settings)
session = gpflow.get_default_session()
step = mon.create_global_step(session)
model = convgp_setup_model(train_data)
model.compile()
optimizer = convgp_setup_optimizer(model, step)
optimizer.minimize(model, maxiter=0)
monitor_tasks = convgp_monitor_tasks(train_data, model, optimizer)
monitor = mon.Monitor(monitor_tasks, session, step, print_summary=True)
restore_session(session)
print(session.run(optimizer.optimizer.variables()[:3]))
with monitor:
@gpflow.autoflow((gpflow.settings.float_type, [None, None]), (gpflow.settings.int_type, ()))
def predict_f_multisample(self, X, S):
X_tiled = tf.tile(X[None, :, :], [S, 1, 1])
_, means, covs, _, _ = self.propagate(X_tiled)
return means[-1], covs[-1]
def __init__(self, Z):
"""
:param Z: the initial positions of the inducing points, size M x D
"""
super().__init__()
self.Z = Parameter(Z, dtype=settings.float_type)
If the z is a sample from N(0, 1), the output is a sample from N(mean, var)
If full_cov=True then var must be of shape S,N,N,D and the full covariance is used. Otherwise
var must be S,N,D and the operation is elementwise
:param mean: mean of shape S,N,D
:param var: covariance of shape S,N,D or S,N,N,D
:param z: samples form unit Gaussian of shape S,N,D
:param full_cov: bool to indicate whether var is of shape S,N,N,D or S,N,D
:return sample from N(mean, var) of shape S,N,D
"""
if var is None:
return mean
if full_cov is False:
return mean + z * (var + settings.jitter) ** 0.5
else:
S, N, D = tf.shape(mean)[0], tf.shape(mean)[1], tf.shape(mean)[2] # var is SNND
mean = tf.transpose(mean, (0, 2, 1)) # SND -> SDN
var = tf.transpose(var, (0, 3, 1, 2)) # SNND -> SDNN
I = settings.jitter * tf.eye(N, dtype=settings.float_type)[None, None, :, :] # 11NN
chol = tf.cholesky(var + I) # SDNN
z_SDN1 = tf.transpose(z, [0, 2, 1])[:, :, :, None] # SND->SDN1
f = mean + tf.matmul(chol, z_SDN1)[:, :, :, 0] # SDN(1)
return tf.transpose(f, (0, 2, 1)) # SND