本文整理汇总了Python中tensorflow.matrix_diag_part方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.matrix_diag_part方法的具体用法?Python tensorflow.matrix_diag_part怎么用?Python tensorflow.matrix_diag_part使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.matrix_diag_part方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: effective_steps
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def effective_steps(masks, num_edge_residues, name=None):
""" Returns the effective number of steps, i.e. number of residues that are non-missing and are not just
padding, given a masking matrix.
Args:
masks: A batch of square masking matrices (batch is last dimension)
[MAX_SEQ_LENGTH, MAX_SEQ_LENGTH, BATCH_SIZE]
Returns:
A vector with the effective number of steps
[BATCH_SIZE]
"""
with tf.name_scope(name, 'effective_steps', [masks]) as scope:
masks = tf.convert_to_tensor(masks, name='masks')
traces = tf.matrix_diag_part(tf.transpose(masks, [2, 0, 1]))
eff_stepss = tf.add(tf.reduce_sum(traces, [1]), num_edge_residues, name=scope) # NUM_EDGE_RESIDUES shouldn't be here, but I'm keeping it for
# legacy reasons. Just be clear that it's _always_ wrong to have
# it here, even when it's not equal to 0.
return eff_stepss
示例2: fit
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def fit(self, x=None, y=None):
# p(coeffs | x, y) = Normal(coeffs |
# mean = (1/noise_variance) (1/noise_variance x^T x + I)^{-1} x^T y,
# covariance = (1/noise_variance x^T x + I)^{-1})
# TODO(trandustin): We newly fit the data at each call. Extend to do
# Bayesian updating.
kernel_matrix = tf.matmul(x, x, transpose_a=True) / self.noise_variance
coeffs_precision = tf.matrix_set_diag(
kernel_matrix, tf.matrix_diag_part(kernel_matrix) + 1.)
coeffs_precision_tril = tf.linalg.cholesky(coeffs_precision)
self.coeffs_precision_tril_op = tf.linalg.LinearOperatorLowerTriangular(
coeffs_precision_tril)
self.coeffs_mean = self.coeffs_precision_tril_op.solvevec(
self.coeffs_precision_tril_op.solvevec(tf.einsum('nm,n->m', x, y)),
adjoint=True) / self.noise_variance
# TODO(trandustin): To be fully Keras-compatible, return History object.
return
示例3: get_matrix_tree
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def get_matrix_tree(r, A):
L = tf.reduce_sum(A, 1)
L = tf.matrix_diag(L)
L = L - A
r_diag = tf.matrix_diag(r)
LL = L + r_diag
LL_inv = tf.matrix_inverse(LL) #batch_l, doc_l, doc_l
LL_inv_diag_ = tf.matrix_diag_part(LL_inv)
d0 = tf.multiply(r, LL_inv_diag_)
LL_inv_diag = tf.expand_dims(LL_inv_diag_, 2)
tmp1 = tf.multiply(A, tf.matrix_transpose(LL_inv_diag))
tmp2 = tf.multiply(A, tf.matrix_transpose(LL_inv))
d = tmp1 - tmp2
d = tf.concat([tf.expand_dims(d0,[1]), d], 1)
return d
示例4: _define_full_covariance_probs
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [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)
示例5: _svd
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def _svd(A, name=None):
"""Tensorflow svd with gradient.
Parameters
----------
A : Tensor
The matrix for which to compute singular values.
name : string, optional
Returns
-------
s : Tensor
The singular values of A.
"""
S0, U0, V0 = map(tf.stop_gradient,
tf.svd(A, full_matrices=True, name=name))
# A = U * S * V.T
# S = inv(U) * A * inv(V.T) = U.T * A * V (orthogonal matrices)
S = tf.matmul(U0, tf.matmul(A, V0),
transpose_a=True)
return tf.matrix_diag_part(S)
示例6: _update_memory
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def _update_memory(self, old_memory, w_samples, new_z_mean, new_z_var):
"""Setting new_z_var=0 for sample based update."""
old_mean, old_cov = old_memory
wR = self.get_w_to_z_mean(w_samples, old_memory.M_mean)
wU, wUw = self._read_cov(w_samples, old_memory)
sigma_z = wUw + new_z_var + self._obs_noise_stddev**2 # [S, B]
delta = new_z_mean - wR # [S, B, C]
c_z = wU / tf.expand_dims(sigma_z, -1) # [S, B, M]
posterior_mean = old_mean + tf.einsum('sbm,sbc->bmc', c_z, delta)
posterior_cov = old_cov - tf.einsum('sbm,sbn->bmn', c_z, wU)
# Clip diagonal elements for numerical stability
posterior_cov = tf.matrix_set_diag(
posterior_cov,
tf.clip_by_value(tf.matrix_diag_part(posterior_cov), EPSILON, 1e10))
new_memory = MemoryState(M_mean=posterior_mean, M_cov=posterior_cov)
return new_memory
示例7: matrix_mean_wo_diagonal
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def matrix_mean_wo_diagonal(matrix, num_row, num_col=None, name='mu_wo_diag'):
""" This function calculates the mean of the matrix elements not in the diagonal
2018.4.9 - replace tf.diag_part with tf.matrix_diag_part
tf.matrix_diag_part can be used for rectangle matrix while tf.diag_part can only be used for square matrix
:param matrix:
:param num_row:
:type num_row: float
:param num_col:
:type num_col: float
:param name:
:return:
"""
with tf.name_scope(name):
if num_col is None:
mu = (tf.reduce_sum(matrix) - tf.reduce_sum(tf.matrix_diag_part(matrix))) / (num_row * (num_row - 1.0))
else:
mu = (tf.reduce_sum(matrix) - tf.reduce_sum(tf.matrix_diag_part(matrix))) \
/ (num_row * num_col - tf.minimum(num_col, num_row))
return mu
########################################################################
示例8: build_variational
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def build_variational(hps, kernel, z_pos, x, n_particles):
bn = zs.BayesianNet()
z_mean = tf.get_variable(
'z/mean', [hps.n_z], hps.dtype, tf.zeros_initializer())
z_cov_raw = tf.get_variable(
'z/cov_raw', initializer=tf.eye(hps.n_z, dtype=hps.dtype))
z_cov_tril = tf.matrix_set_diag(
tf.matrix_band_part(z_cov_raw, -1, 0),
tf.nn.softplus(tf.matrix_diag_part(z_cov_raw)))
fz = bn.multivariate_normal_cholesky(
'fz', z_mean, z_cov_tril, n_samples=n_particles)
bn.stochastic('fx', gp_conditional(z_pos, fz, x, False, kernel))
return bn
示例9: _chollogdet
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def _chollogdet(L):
"""Log det of a cholesky, where L is (..., D, D)."""
ldiag = tf.maximum(tf.abs(tf.matrix_diag_part(L)), JIT) # keep > 0
logdet = 2. * tf.reduce_sum(tf.log(ldiag))
return logdet
示例10: test_MatrixDiagPart
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def test_MatrixDiagPart(self):
if td._tf_version[:2] >= (0, 12):
t = tf.matrix_diag_part(self.random(3, 4, 4, 5))
self.check(t)
示例11: testSquare
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def testSquare(self):
with self.test_session(use_gpu=self._use_gpu):
v = np.array([1.0, 2.0, 3.0])
mat = np.diag(v)
mat_diag = tf.matrix_diag_part(mat)
self.assertEqual((3,), mat_diag.get_shape())
self.assertAllEqual(mat_diag.eval(), v)
示例12: testRectangular
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def testRectangular(self):
with self.test_session(use_gpu=self._use_gpu):
mat = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
mat_diag = tf.matrix_diag_part(mat)
self.assertAllEqual(mat_diag.eval(), np.array([1.0, 5.0]))
mat = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
mat_diag = tf.matrix_diag_part(mat)
self.assertAllEqual(mat_diag.eval(), np.array([1.0, 4.0]))
示例13: testSquareBatch
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def testSquareBatch(self):
with self.test_session(use_gpu=self._use_gpu):
v_batch = np.array([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]])
mat_batch = np.array(
[[[1.0, 0.0, 0.0],
[0.0, 2.0, 0.0],
[0.0, 0.0, 3.0]],
[[4.0, 0.0, 0.0],
[0.0, 5.0, 0.0],
[0.0, 0.0, 6.0]]])
self.assertEqual(mat_batch.shape, (2, 3, 3))
mat_batch_diag = tf.matrix_diag_part(mat_batch)
self.assertEqual((2, 3), mat_batch_diag.get_shape())
self.assertAllEqual(mat_batch_diag.eval(), v_batch)
示例14: testRectangularBatch
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def testRectangularBatch(self):
with self.test_session(use_gpu=self._use_gpu):
v_batch = np.array([[1.0, 2.0],
[4.0, 5.0]])
mat_batch = np.array(
[[[1.0, 0.0, 0.0],
[0.0, 2.0, 0.0]],
[[4.0, 0.0, 0.0],
[0.0, 5.0, 0.0]]])
self.assertEqual(mat_batch.shape, (2, 2, 3))
mat_batch_diag = tf.matrix_diag_part(mat_batch)
self.assertEqual((2, 2), mat_batch_diag.get_shape())
self.assertAllEqual(mat_batch_diag.eval(), v_batch)
示例15: testInvalidShape
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag_part [as 别名]
def testInvalidShape(self):
with self.assertRaisesRegexp(ValueError, "must be at least rank 2"):
tf.matrix_diag_part(0)