def test_mean_argument(self): with pytest.raises(ModelSpecificationError): dtype = 'float64' net = nn.HybridSequential(prefix='nn_') with net.name_scope(): net.add( nn.Dense(1, flatten=False, activation="tanh", in_units=2, dtype=dtype)) net.initialize(mx.init.Xavier(magnitude=3)) rbf = RBF(2, True, 1., 1., 'rbf', None, dtype) X_var = Variable(shape=(5, 2)) X_cond_var = Variable(shape=(8, 2)) Y_cond_var = Variable(shape=(8, 1)) mean_func = MXFusionGluonFunction(net, num_outputs=1, broadcastable=True) mean_var = mean_func(X_var) mean_cond_var = mean_func(X_cond_var) gp = ConditionalGaussianProcess.define_variable( X=X_var, X_cond=X_cond_var, Y_cond=Y_cond_var, mean_cond=mean_cond_var, kernel=rbf, shape=(5, 1), dtype=dtype)
def test_kernel_as_MXFusionFunction(self, dtype, X, X_isSamples, X2, X2_isSamples, lengthscale, lengthscale_isSamples, variance, variance_isSamples, num_samples, input_dim, ARD): X_mx = prepare_mxnet_array(X, X_isSamples, dtype) X2_mx = prepare_mxnet_array(X2, X2_isSamples, dtype) var_mx = prepare_mxnet_array(variance, variance_isSamples, dtype) l_mx = prepare_mxnet_array(lengthscale, lengthscale_isSamples, dtype) X_mf = Variable(shape=X.shape) l_mf = Variable(shape=lengthscale.shape) var_mf = Variable(shape=variance.shape) rbf = RBF(input_dim, ARD, 1., 1., 'rbf', None, dtype) eval = rbf(X_mf, rbf_lengthscale=l_mf, rbf_variance=var_mf).factor variables = {eval.X.uuid: X_mx, eval.rbf_lengthscale.uuid: l_mx, eval.rbf_variance.uuid: var_mx} res_eval = eval.eval(F=mx.nd, variables=variables) kernel_params = rbf.fetch_parameters(variables) res_direct = rbf.K(F=mx.nd, X=X_mx, **kernel_params) assert np.allclose(res_eval.asnumpy(), res_direct.asnumpy()) X_mf = Variable(shape=X.shape) X2_mf = Variable(shape=X2.shape) l_mf = Variable(shape=lengthscale.shape) var_mf = Variable(shape=variance.shape) rbf = RBF(input_dim, ARD, 1., 1., 'rbf', None, dtype) eval = rbf(X_mf, X2_mf, rbf_lengthscale=l_mf, rbf_variance=var_mf).factor variables = {eval.X.uuid: X_mx, eval.X2.uuid: X2_mx, eval.rbf_lengthscale.uuid: l_mx, eval.rbf_variance.uuid: var_mx} res_eval = eval.eval(F=mx.nd, variables=variables) kernel_params = rbf.fetch_parameters(variables) res_direct = rbf.K(F=mx.nd, X=X_mx, X2=X2_mx, **kernel_params) assert np.allclose(res_eval.asnumpy(), res_direct.asnumpy())
def test_draw_samples_with_broadcast_no_numpy_verification(self, dtype, a, a_is_samples, rv_shape, num_samples): a_mx = mx.nd.array(a, dtype=dtype) if not a_is_samples: a_mx = add_sample_dimension(mx.nd, a_mx) dirichlet = Dirichlet.define_variable(alpha=Variable(), shape=rv_shape, dtype=dtype).factor variables = {dirichlet.alpha.uuid: a_mx} draw_samples_rt = dirichlet.draw_samples(F=mx.nd, variables=variables, num_samples=num_samples) assert np.issubdtype(draw_samples_rt.dtype, dtype) assert draw_samples_rt.shape == (5,) + rv_shape
def test_draw_samples_no_broadcast(self, dtype, a, a_is_samples, rv_shape, num_samples): a_mx = mx.nd.array(a, dtype=dtype) if not a_is_samples: a_mx = add_sample_dimension(mx.nd, a_mx) rand = np.random.gamma(shape=a, scale=np.ones(a.shape), size=(num_samples,)+rv_shape) draw_samples_np = rand / np.sum(rand) rand_gen = MockMXNetRandomGenerator(mx.nd.array(rand.flatten(), dtype=dtype)) dirichlet = Dirichlet.define_variable(alpha=Variable(), shape=rv_shape, dtype=dtype, rand_gen=rand_gen).factor variables = {dirichlet.alpha.uuid: a_mx} draw_samples_rt = dirichlet.draw_samples(F=mx.nd, variables=variables, num_samples=num_samples) assert np.issubdtype(draw_samples_rt.dtype, dtype) assert np.allclose(draw_samples_np, draw_samples_rt.asnumpy())
def make_gpregr_model(self, lengthscale, variance, noise_var): from mxfusion.models import Model from mxfusion.components.variables import Variable, PositiveTransformation from mxfusion.modules.gp_modules import GPRegression from mxfusion.components.distributions.gp.kernels import RBF dtype = 'float64' m = Model() m.N = Variable() m.X = Variable(shape=(m.N, 3)) m.noise_var = Variable(transformation=PositiveTransformation(), initial_value=mx.nd.array(noise_var, dtype=dtype)) kernel = RBF(input_dim=3, ARD=True, variance=mx.nd.array(variance, dtype=dtype), lengthscale=mx.nd.array(lengthscale, dtype=dtype), dtype=dtype) m.Y = GPRegression.define_variable(X=m.X, kernel=kernel, noise_var=m.noise_var, shape=(m.N, 1), dtype=dtype) return m
def test_log_pdf_with_broadcast(self, dtype, a, a_is_samples, rv, rv_is_samples, num_samples): # Add sample dimension if varaible is not samples a_mx = mx.nd.array(a, dtype=dtype) if not a_is_samples: a_mx = add_sample_dimension(mx.nd, a_mx) a = a_mx.asnumpy() rv_mx = mx.nd.array(rv, dtype=dtype) if not rv_is_samples: rv_mx = add_sample_dimension(mx.nd, rv_mx) rv = rv_mx.asnumpy() is_samples_any = a_is_samples or rv_is_samples rv_shape = rv.shape[1:] n_dim = 1 + len(rv.shape) if is_samples_any and not rv_is_samples else len(rv.shape) a_np = np.broadcast_to(a, (num_samples, 3, 2)) rv_np = numpy_array_reshape(rv, is_samples_any, n_dim) # Initialize rand_gen rand = np.random.rand(num_samples, *rv_shape) rand_gen = MockMXNetRandomGenerator(mx.nd.array(rand.flatten(), dtype=dtype)) # Calculate correct Dirichlet logpdf r = [] for s in range(len(rv_np)): a = [] for i in range(len(rv_np[s])): a.append(scipy_dirichlet.logpdf(rv_np[s][i]/sum(rv_np[s][i]), a_np[s][i])) r.append(a) log_pdf_np = np.array(r) dirichlet = Dirichlet.define_variable(alpha=Variable(), shape=rv_shape, dtype=dtype, rand_gen=rand_gen).factor variables = {dirichlet.alpha.uuid: a_mx, dirichlet.random_variable.uuid: rv_mx} log_pdf_rt = dirichlet.log_pdf(F=mx.nd, variables=variables) assert np.issubdtype(log_pdf_rt.dtype, dtype) assert array_has_samples(mx.nd, log_pdf_rt) == is_samples_any if is_samples_any: assert get_num_samples(mx.nd, log_pdf_rt) == num_samples, (get_num_samples(mx.nd, log_pdf_rt), num_samples) assert np.allclose(log_pdf_np, log_pdf_rt.asnumpy())
def test_log_pdf(self, dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples, rv, rv_isSamples, num_samples): from scipy.linalg.lapack import dtrtrs X_mx = prepare_mxnet_array(X, X_isSamples, dtype) X_cond_mx = prepare_mxnet_array(X_cond, X_cond_isSamples, dtype) Y_cond_mx = prepare_mxnet_array(Y_cond, Y_cond_isSamples, dtype) rbf_lengthscale_mx = prepare_mxnet_array(rbf_lengthscale, rbf_lengthscale_isSamples, dtype) rbf_variance_mx = prepare_mxnet_array(rbf_variance, rbf_variance_isSamples, dtype) rv_mx = prepare_mxnet_array(rv, rv_isSamples, dtype) rv_shape = rv.shape[1:] if rv_isSamples else rv.shape rbf = RBF(2, True, 1., 1., 'rbf', None, dtype) X_var = Variable(shape=(5, 2)) X_cond_var = Variable(shape=(8, 2)) Y_cond_var = Variable(shape=(8, 1)) gp = ConditionalGaussianProcess.define_variable(X=X_var, X_cond=X_cond_var, Y_cond=Y_cond_var, kernel=rbf, shape=rv_shape, dtype=dtype).factor variables = { gp.X.uuid: X_mx, gp.X_cond.uuid: X_cond_mx, gp.Y_cond.uuid: Y_cond_mx, gp.rbf_lengthscale.uuid: rbf_lengthscale_mx, gp.rbf_variance.uuid: rbf_variance_mx, gp.random_variable.uuid: rv_mx } log_pdf_rt = gp.log_pdf(F=mx.nd, variables=variables).asnumpy() log_pdf_np = [] for i in range(num_samples): X_i = X[i] if X_isSamples else X X_cond_i = X_cond[i] if X_cond_isSamples else X_cond Y_cond_i = Y_cond[i] if Y_cond_isSamples else Y_cond lengthscale_i = rbf_lengthscale[ i] if rbf_lengthscale_isSamples else rbf_lengthscale variance_i = rbf_variance[ i] if rbf_variance_isSamples else rbf_variance rv_i = rv[i] if rv_isSamples else rv rbf_np = GPy.kern.RBF(input_dim=2, ARD=True) rbf_np.lengthscale = lengthscale_i rbf_np.variance = variance_i K_np = rbf_np.K(X_i) Kc_np = rbf_np.K(X_cond_i, X_i) Kcc_np = rbf_np.K(X_cond_i) L = np.linalg.cholesky(Kcc_np) LInvY = dtrtrs(L, Y_cond_i, lower=1, trans=0)[0] LinvKxt = dtrtrs(L, Kc_np, lower=1, trans=0)[0] mu = LinvKxt.T.dot(LInvY) cov = K_np - LinvKxt.T.dot(LinvKxt) log_pdf_np.append( multivariate_normal.logpdf(rv_i[:, 0], mean=mu[:, 0], cov=cov)) log_pdf_np = np.array(log_pdf_np) isSamples_any = any([ X_isSamples, rbf_lengthscale_isSamples, rbf_variance_isSamples, rv_isSamples ]) assert np.issubdtype(log_pdf_rt.dtype, dtype) assert array_has_samples(mx.nd, log_pdf_rt) == isSamples_any if isSamples_any: assert get_num_samples(mx.nd, log_pdf_rt) == num_samples assert np.allclose(log_pdf_np, log_pdf_rt)
def test_draw_samples_w_mean(self, dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples, rv_shape, num_samples): net = nn.HybridSequential(prefix='nn_') with net.name_scope(): net.add( nn.Dense(rv_shape[-1], flatten=False, activation="tanh", in_units=X.shape[-1], dtype=dtype)) net.initialize(mx.init.Xavier(magnitude=3)) from scipy.linalg.lapack import dtrtrs X_mx = prepare_mxnet_array(X, X_isSamples, dtype) X_cond_mx = prepare_mxnet_array(X_cond, X_cond_isSamples, dtype) Y_cond_mx = prepare_mxnet_array(Y_cond, Y_cond_isSamples, dtype) rbf_lengthscale_mx = prepare_mxnet_array(rbf_lengthscale, rbf_lengthscale_isSamples, dtype) rbf_variance_mx = prepare_mxnet_array(rbf_variance, rbf_variance_isSamples, dtype) mean_mx = net(X_mx) mean_np = mean_mx.asnumpy() mean_cond_mx = net(X_cond_mx) mean_cond_np = mean_cond_mx.asnumpy() rand = np.random.randn(num_samples, *rv_shape) rand_gen = MockMXNetRandomGenerator( mx.nd.array(rand.flatten(), dtype=dtype)) rbf = RBF(2, True, 1., 1., 'rbf', None, dtype) X_var = Variable(shape=(5, 2)) X_cond_var = Variable(shape=(8, 2)) Y_cond_var = Variable(shape=(8, 1)) mean_func = MXFusionGluonFunction(net, num_outputs=1, broadcastable=True) mean_var = mean_func(X_var) mean_cond_var = mean_func(X_cond_var) gp = ConditionalGaussianProcess.define_variable( X=X_var, X_cond=X_cond_var, Y_cond=Y_cond_var, mean=mean_var, mean_cond=mean_cond_var, kernel=rbf, shape=rv_shape, dtype=dtype, rand_gen=rand_gen).factor variables = { gp.X.uuid: X_mx, gp.X_cond.uuid: X_cond_mx, gp.Y_cond.uuid: Y_cond_mx, gp.rbf_lengthscale.uuid: rbf_lengthscale_mx, gp.rbf_variance.uuid: rbf_variance_mx, gp.mean.uuid: mean_mx, gp.mean_cond.uuid: mean_cond_mx } samples_rt = gp.draw_samples(F=mx.nd, variables=variables, num_samples=num_samples).asnumpy() samples_np = [] for i in range(num_samples): X_i = X[i] if X_isSamples else X X_cond_i = X_cond[i] if X_cond_isSamples else X_cond Y_cond_i = Y_cond[i] if Y_cond_isSamples else Y_cond Y_cond_i = Y_cond_i - mean_cond_np[ i] if X_cond_isSamples else Y_cond_i - mean_cond_np[0] lengthscale_i = rbf_lengthscale[ i] if rbf_lengthscale_isSamples else rbf_lengthscale variance_i = rbf_variance[ i] if rbf_variance_isSamples else rbf_variance rand_i = rand[i] rbf_np = GPy.kern.RBF(input_dim=2, ARD=True) rbf_np.lengthscale = lengthscale_i rbf_np.variance = variance_i K_np = rbf_np.K(X_i) Kc_np = rbf_np.K(X_cond_i, X_i) Kcc_np = rbf_np.K(X_cond_i) L = np.linalg.cholesky(Kcc_np) LInvY = dtrtrs(L, Y_cond_i, lower=1, trans=0)[0] LinvKxt = dtrtrs(L, Kc_np, lower=1, trans=0)[0] mu = LinvKxt.T.dot(LInvY) cov = K_np - LinvKxt.T.dot(LinvKxt) L_cov_np = np.linalg.cholesky(cov) sample_np = mu + L_cov_np.dot(rand_i) samples_np.append(sample_np) samples_np = np.array(samples_np) + mean_np assert np.issubdtype(samples_rt.dtype, dtype) assert get_num_samples(mx.nd, samples_rt) == num_samples assert np.allclose(samples_np, samples_rt)
def test_draw_samples(self, dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples, rv_shape, num_samples): from scipy.linalg.lapack import dtrtrs X_mx = prepare_mxnet_array(X, X_isSamples, dtype) X_cond_mx = prepare_mxnet_array(X_cond, X_cond_isSamples, dtype) Y_cond_mx = prepare_mxnet_array(Y_cond, Y_cond_isSamples, dtype) rbf_lengthscale_mx = prepare_mxnet_array(rbf_lengthscale, rbf_lengthscale_isSamples, dtype) rbf_variance_mx = prepare_mxnet_array(rbf_variance, rbf_variance_isSamples, dtype) rand = np.random.randn(num_samples, *rv_shape) rand_gen = MockMXNetRandomGenerator( mx.nd.array(rand.flatten(), dtype=dtype)) rbf = RBF(2, True, 1., 1., 'rbf', None, dtype) X_var = Variable(shape=(5, 2)) X_cond_var = Variable(shape=(8, 2)) Y_cond_var = Variable(shape=(8, 1)) gp = ConditionalGaussianProcess.define_variable( X=X_var, X_cond=X_cond_var, Y_cond=Y_cond_var, kernel=rbf, shape=rv_shape, dtype=dtype, rand_gen=rand_gen).factor variables = { gp.X.uuid: X_mx, gp.X_cond.uuid: X_cond_mx, gp.Y_cond.uuid: Y_cond_mx, gp.rbf_lengthscale.uuid: rbf_lengthscale_mx, gp.rbf_variance.uuid: rbf_variance_mx } samples_rt = gp.draw_samples(F=mx.nd, variables=variables, num_samples=num_samples).asnumpy() samples_np = [] for i in range(num_samples): X_i = X[i] if X_isSamples else X X_cond_i = X_cond[i] if X_cond_isSamples else X_cond Y_cond_i = Y_cond[i] if Y_cond_isSamples else Y_cond lengthscale_i = rbf_lengthscale[ i] if rbf_lengthscale_isSamples else rbf_lengthscale variance_i = rbf_variance[ i] if rbf_variance_isSamples else rbf_variance rand_i = rand[i] rbf_np = GPy.kern.RBF(input_dim=2, ARD=True) rbf_np.lengthscale = lengthscale_i rbf_np.variance = variance_i K_np = rbf_np.K(X_i) Kc_np = rbf_np.K(X_cond_i, X_i) Kcc_np = rbf_np.K(X_cond_i) L = np.linalg.cholesky(Kcc_np) LInvY = dtrtrs(L, Y_cond_i, lower=1, trans=0)[0] LinvKxt = dtrtrs(L, Kc_np, lower=1, trans=0)[0] mu = LinvKxt.T.dot(LInvY) cov = K_np - LinvKxt.T.dot(LinvKxt) L_cov_np = np.linalg.cholesky(cov) sample_np = mu + L_cov_np.dot(rand_i) samples_np.append(sample_np) samples_np = np.array(samples_np) assert np.issubdtype(samples_rt.dtype, dtype) assert get_num_samples(mx.nd, samples_rt) == num_samples assert np.allclose(samples_np, samples_rt)