本文整理汇总了Python中tensorflow.matrix_triangular_solve方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.matrix_triangular_solve方法的具体用法?Python tensorflow.matrix_triangular_solve怎么用?Python tensorflow.matrix_triangular_solve使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.matrix_triangular_solve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _log_prob
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def _log_prob(self, given):
mean, cov_tril = (self.path_param(self.mean),
self.path_param(self.cov_tril))
log_det = 2 * tf.reduce_sum(
tf.log(tf.matrix_diag_part(cov_tril)), axis=-1)
n_dim = tf.cast(self._n_dim, self.dtype)
log_z = - n_dim / 2 * tf.log(
2 * tf.constant(np.pi, dtype=self.dtype)) - log_det / 2
# log_z.shape == batch_shape
if self._check_numerics:
log_z = tf.check_numerics(log_z, "log[det(Cov)]")
# (given-mean)' Sigma^{-1} (given-mean) =
# (g-m)' L^{-T} L^{-1} (g-m) = |x|^2, where Lx = g-m =: y.
y = tf.expand_dims(given - mean, -1)
L, _ = maybe_explicit_broadcast(
cov_tril, y, 'MultivariateNormalCholesky.cov_tril',
'expand_dims(given, -1)')
x = tf.matrix_triangular_solve(L, y, lower=True)
x = tf.squeeze(x, -1)
stoc_dist = -0.5 * tf.reduce_sum(tf.square(x), axis=-1)
return log_z + stoc_dist
示例2: _define_full_covariance_probs
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def _define_full_covariance_probs(self, shard_id, shard):
"""Defines the full covariance probabilties per example in a class.
Updates a matrix with dimension num_examples X num_classes.
Args:
shard_id: id of the current shard.
shard: current data shard, 1 X num_examples X dimensions.
"""
diff = shard - self._means
cholesky = tf.cholesky(self._covs + self._min_var)
log_det_covs = 2.0 * tf.reduce_sum(tf.log(tf.matrix_diag_part(cholesky)), 1)
x_mu_cov = tf.square(
tf.matrix_triangular_solve(
cholesky, tf.transpose(
diff, perm=[0, 2, 1]), lower=True))
diag_m = tf.transpose(tf.reduce_sum(x_mu_cov, 1))
self._probs[shard_id] = -0.5 * (
diag_m + tf.to_float(self._dimensions) * tf.log(2 * np.pi) +
log_det_covs)
示例3: log_det_kron_sum
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def log_det_kron_sum(L1, L2):
"""
L1 is a list of lower triangular arrays.
L2 is a list of lower triangular arrays.
if S1 = kron(L1) * kron(L1).T, and S2 similarly,
this function computes the log determinant of S1 + S2
"""
L1_logdets = [tf.reduce_sum(tf.log(tf.square(tf.diag_part(L)))) for L in L1]
total_size = reduce(tf.mul, [L.shape[0] for L in L1])
N_other = [total_size / tf.shape(L)[0] for L in L1]
L1_logdet = reduce(tf.add, [s*ld for s, ld in zip(N_other, L1_logdets)])
LiL = [tf.matrix_triangular_solve(L, R) for L, R in zip(L1, L2)]
eigvals = [tf.self_adjoint_eigvals(tf.matmul(mat, mat.T)) for mat in LiL]
eigvals_kronned = kron_vec_mul([tf.reshape(e, [-1, 1]) for e in eigvals], tf.ones([1, 1], tf.float64))
return tf.reduce_sum(tf.log(1 + eigvals_kronned)) + L1_logdet
示例4: build_likelihood_terms
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def build_likelihood_terms(self):
Kdiag = self.kern.Kdiag(self.X)
Kuu = make_Kuu(self.kern, self.a, self.b, self.ms)
sigma2 = self.likelihood.variance
# Compute intermediate matrices
P = self.KufKfu / sigma2 + Kuu.get()
L = tf.cholesky(P)
log_det_P = tf.reduce_sum(tf.log(tf.square(tf.diag_part(L))))
c = tf.matrix_triangular_solve(L, self.KufY) / sigma2
# compute log marginal bound
ND = tf.cast(tf.size(self.Y), float_type)
D = tf.cast(tf.shape(self.Y)[1], float_type)
return (-0.5 * ND * tf.log(2 * np.pi * sigma2),
-0.5 * D * log_det_P,
0.5 * D * Kuu.logdet(),
-0.5 * self.tr_YTY / sigma2,
0.5 * tf.reduce_sum(tf.square(c)),
-0.5 * tf.reduce_sum(Kdiag)/sigma2,
0.5 * Kuu.trace_KiX(self.KufKfu) / sigma2)
示例5: _build_predict
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def _build_predict(self, n_test, full_cov=False):
with tf.name_scope("build_predict"):
self.x_test_pl = tf.identity(
tf.placeholder(tf.float64, [n_test, self.input_dim], name="x_test_pl")
)
tf.logging.info("Using pre-computed Kernel")
self.k_data_test = self.kern.k_full(self.x_pl, self.x_test_pl)
with tf.name_scope("build_predict"):
a = tf.matrix_triangular_solve(self.l, self.k_data_test)
fmean = tf.matmul(a, self.v, transpose_a=True)
if full_cov:
fvar = self.kern.k_full(self.x_test_pl) - tf.matmul(
a, a, transpose_a=True)
shape = [1, 1, self.y_pl.shape[1]]
fvar = tf.tile(tf.expand_dims(fvar, 2), shape)
else:
fvar = self.kern.k_diag(self.x_test_pl) - tf.reduce_sum(tf.square(a), 0)
fvar = tf.tile(tf.reshape(fvar, (-1, 1)), [1, self.output_y.shape[1]])
self.fmean = fmean
self.fvar = fvar
示例6: conditional_ND
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def conditional_ND(self, Xnew, full_cov=False):
## modified from GPR
Kx = self.kern.K(self._X_mean, Xnew)
K = self.kern.K(self._X_mean) + tf.eye(tf.shape(self._X_mean)[0], dtype=settings.float_type) * self._lik_variance
L = tf.cholesky(K)
A = tf.matrix_triangular_solve(L, Kx, lower=True)
V = tf.matrix_triangular_solve(L, self._Y - self.mean_function(self._X_mean))
fmean = tf.matmul(A, V, transpose_a=True) + self.mean_function(Xnew)
if full_cov:
fvar = self.kern.K(Xnew) - tf.matmul(A, A, transpose_a=True)
shape = tf.stack([1, 1, tf.shape(self._Y)[1]])
fvar = tf.tile(tf.expand_dims(fvar, 2), shape)
else:
fvar = self.kern.Kdiag(Xnew) - tf.reduce_sum(tf.square(A), 0)
fvar = tf.tile(tf.reshape(fvar, (-1, 1)), [1, tf.shape(self._Y)[1]])
return fmean, fvar
示例7: gp_conditional
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def gp_conditional(z, fz, x, full_cov, kernel, Kzz_chol=None):
'''
GP gp_conditional f(x) | f(z)==fz
:param z: shape [n_z, n_covariates]
:param fz: shape [n_particles, n_z]
:param x: shape [n_x, n_covariates]
:return: a distribution with shape [n_particles, n_x]
'''
n_z = int(z.shape[0])
n_particles = tf.shape(fz)[0]
if Kzz_chol is None:
Kzz_chol = tf.cholesky(kernel(z, z))
# Mean[fx|fz] = Kxz @ inv(Kzz) @ fz; Cov[fx|z] = Kxx - Kxz @ inv(Kzz) @ Kzx
# With ill-conditioned Kzz, the inverse is often asymmetric, which
# breaks further cholesky decomposition. We compute a symmetric one.
Kzz_chol_inv = tf.matrix_triangular_solve(Kzz_chol, tf.eye(n_z))
Kzz_inv = tf.matmul(tf.transpose(Kzz_chol_inv), Kzz_chol_inv)
Kxz = kernel(x, z) # [n_x, n_z]
Kxziz = tf.matmul(Kxz, Kzz_inv)
mean_fx_given_fz = tf.matmul(fz, tf.matrix_transpose(Kxziz))
if full_cov:
cov_fx_given_fz = kernel(x, x) - tf.matmul(Kxziz, tf.transpose(Kxz))
cov_fx_given_fz = tf.tile(
tf.expand_dims(tf.cholesky(cov_fx_given_fz), 0),
[n_particles, 1, 1])
fx_given_fz = zs.distributions.MultivariateNormalCholesky(
mean_fx_given_fz, cov_fx_given_fz)
else:
# diag(AA^T) = sum(A**2, axis=-1)
var = kernel.Kdiag(x) - \
tf.reduce_sum(tf.matmul(
Kxz, tf.matrix_transpose(Kzz_chol_inv)) ** 2, axis=-1)
std = tf.sqrt(var)
fx_given_fz = zs.distributions.Normal(
mean=mean_fx_given_fz, std=std, group_ndims=1)
return fx_given_fz
示例8: test_MatrixTriangularSolve
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def test_MatrixTriangularSolve(self):
t = tf.matrix_triangular_solve(*self.random((2, 3, 3, 3), (2, 3, 3, 1)), adjoint=False, lower=False)
self.check(t)
t = tf.matrix_triangular_solve(*self.random((2, 3, 3, 3), (2, 3, 3, 1)), adjoint=True, lower=False)
self.check(t)
t = tf.matrix_triangular_solve(*self.random((2, 3, 3, 3), (2, 3, 3, 1)), adjoint=False, lower=True)
self.check(t)
示例9: _verifySolve
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def _verifySolve(self, x, y, lower=True, adjoint=False, batch_dims=None, use_gpu=False):
for np_type in [np.float32, np.float64]:
a = x.astype(np_type)
b = y.astype(np_type)
# For numpy.solve we have to explicitly zero out the strictly
# upper or lower triangle.
if lower and a.size > 0:
a_np = np.tril(a)
elif a.size > 0:
a_np = np.triu(a)
else:
a_np = a
if adjoint:
a_np = np.conj(np.transpose(a_np))
if batch_dims is not None:
a = np.tile(a, batch_dims + [1, 1])
a_np = np.tile(a_np, batch_dims + [1, 1])
b = np.tile(b, batch_dims + [1, 1])
with self.test_session(use_gpu=use_gpu):
tf_ans = tf.matrix_triangular_solve(a, b, lower=lower, adjoint=adjoint)
out = tf_ans.eval()
np_ans = np.linalg.solve(a_np, b)
self.assertEqual(np_ans.shape, tf_ans.get_shape())
self.assertEqual(np_ans.shape, out.shape)
self.assertAllClose(np_ans, out)
示例10: _sqrt_solve
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def _sqrt_solve(self, rhs):
return tf.matrix_triangular_solve(self._chol, rhs, lower=True)
示例11: _batch_sqrt_solve
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def _batch_sqrt_solve(self, rhs):
return tf.matrix_triangular_solve(self._chol, rhs, lower=True)
示例12: _build_common_terms
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def _build_common_terms(self):
Mb = tf.shape(self.Z)[0]
# jitter = settings.numerics.jitter_level
jitter = 1e-4
sigma2 = self.likelihood.variance
alpha = self.alpha
# b is inducing points
# f is training points
Kfdiag = self.kern.Kdiag(self.X)
Kbf = self.kern.K(self.Z, self.X)
Kbb = self.kern.K(self.Z) + tf.eye(Mb, dtype=float_type) * jitter
err = self.Y - self.mean_function(self.X)
Lb = tf.cholesky(Kbb)
Lbinv_Kbf = tf.matrix_triangular_solve(Lb, Kbf, lower=True)
Qff_diag = tf.reduce_sum(tf.square(Lbinv_Kbf), axis=0)
Dff = sigma2 + alpha * (Kfdiag - Qff_diag)
Lbinv_Kbf_LDff = Lbinv_Kbf / tf.sqrt(Dff)
d1 = tf.matmul(Lbinv_Kbf_LDff, tf.transpose(Lbinv_Kbf_LDff))
D = tf.eye(Mb, dtype=float_type) + d1
LD = tf.cholesky(D)
Sinv_y = self.Y / tf.reshape(Dff, [self.num_data, 1])
c = tf.matmul(Lbinv_Kbf, Sinv_y)
LDinv_c = tf.matrix_triangular_solve(LD, c, lower=True)
return (Kbf, Kbb, Lb, D, LD, LDinv_c, err, Dff)
示例13: build_predict
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def build_predict(self, Xnew, full_cov=False):
"""
Compute the mean and variance of the latent function at some new points
Xnew.
"""
# jitter = settings.numerics.jitter_level
jitter = 1e-4
# b is inducing points
# f is training points
# s is test points
Kbs = self.kern.K(self.Z, Xnew)
(Kbf, Kbb, Lb, D, LD, LDinv_c, err, Dff) = self._build_common_terms()
Lbinv_Kbs = tf.matrix_triangular_solve(Lb, Kbs, lower=True)
LDinv_Lbinv_Kbs = tf.matrix_triangular_solve(LD, Lbinv_Kbs, lower=True)
mean = tf.matmul(tf.transpose(LDinv_Lbinv_Kbs), LDinv_c)
if full_cov:
Kss = self.kern.K(Xnew) + jitter * tf.eye(tf.shape(Xnew)[0], dtype=float_type)
var1 = Kss
var2 = - tf.matmul(tf.transpose(Lbinv_Kbs), Lbinv_Kbs)
var3 = tf.matmul(tf.transpose(LDinv_Lbinv_Kbs), LDinv_Lbinv_Kbs)
var = var1 + var2 + var3
else:
var1 = self.kern.Kdiag(Xnew) + jitter
var2 = -tf.reduce_sum(tf.square(Lbinv_Kbs), 0)
var3 = tf.reduce_sum(tf.square(LDinv_Lbinv_Kbs), 0)
var = var1 + var2 + var3
return mean + self.mean_function(Xnew), var
示例14: build_predict
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def build_predict(self, Xnew, full_cov=False):
"""
Compute the mean and variance of the latent function at some new points
Xnew.
"""
# jitter = settings.numerics.jitter_level
jitter = 1e-4
# a is old inducing points, b is new
# f is training points
# s is test points
Kbs = self.kern.K(self.Z, Xnew)
(Kbf, Kba, Kaa, Kaa_cur, La, Kbb, Lb, D, LD,
Lbinv_Kba, LDinv_Lbinv_c, err, Qff) = self._build_common_terms()
Lbinv_Kbs = tf.matrix_triangular_solve(Lb, Kbs, lower=True)
LDinv_Lbinv_Kbs = tf.matrix_triangular_solve(LD, Lbinv_Kbs, lower=True)
mean = tf.matmul(tf.transpose(LDinv_Lbinv_Kbs), LDinv_Lbinv_c)
if full_cov:
Kss = self.kern.K(Xnew) + jitter * tf.eye(tf.shape(Xnew)[0], dtype=float_type)
var1 = Kss
var2 = - tf.matmul(tf.transpose(Lbinv_Kbs), Lbinv_Kbs)
var3 = tf.matmul(tf.transpose(LDinv_Lbinv_Kbs), LDinv_Lbinv_Kbs)
var = var1 + var2 + var3
else:
var1 = self.kern.Kdiag(Xnew)
var2 = -tf.reduce_sum(tf.square(Lbinv_Kbs), 0)
var3 = tf.reduce_sum(tf.square(LDinv_Lbinv_Kbs), 0)
var = var1 + var2 + var3
return mean + self.mean_function(Xnew), var
示例15: _compute_cache
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_triangular_solve [as 别名]
def _compute_cache(self):
"""Compute cache."""
# Scaled kernel
identity = tf.eye(tf.shape(self.X)[0], dtype=config.dtype)
kernel = self.kern.K(self.X) + identity * self.likelihood.variance
kernel *= (self._scale ** 2)
# Scaled target
target = self._scale * (self.Y - self.mean_function(self.X))
# Cholesky decomposition
cholesky = tf.cholesky(kernel, name='gp_cholesky')
alpha = tf.matrix_triangular_solve(cholesky, target, name='gp_alpha')
return cholesky, alpha