本文整理汇总了Python中tensorflow.matrix_diag方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.matrix_diag方法的具体用法?Python tensorflow.matrix_diag怎么用?Python tensorflow.matrix_diag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.matrix_diag方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_matrix_tree
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [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
示例2: build_backward_variance
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def build_backward_variance(self, Yvar):
"""
Additional method for scaling variance backward (used in :class:`.Normalizer`). Can process both the diagonal
variances returned by predict_f, as well as full covariance matrices.
:param Yvar: size N x N x P or size N x P
:return: Yvar scaled, same rank and size as input
"""
rank = tf.rank(Yvar)
# Because TensorFlow evaluates both fn1 and fn2, the transpose can't be in the same line. If a full cov
# matrix is provided fn1 turns it into a rank 4, then tries to transpose it as a rank 3.
# Splitting it in two steps however works fine.
Yvar = tf.cond(tf.equal(rank, 2), lambda: tf.matrix_diag(tf.transpose(Yvar)), lambda: Yvar)
Yvar = tf.cond(tf.equal(rank, 2), lambda: tf.transpose(Yvar, perm=[1, 2, 0]), lambda: Yvar)
N = tf.shape(Yvar)[0]
D = tf.shape(Yvar)[2]
L = tf.cholesky(tf.square(tf.transpose(self.A)))
Yvar = tf.reshape(Yvar, [N * N, D])
scaled_var = tf.reshape(tf.transpose(tf.cholesky_solve(L, tf.transpose(Yvar))), [N, N, D])
return tf.cond(tf.equal(rank, 2), lambda: tf.reduce_sum(scaled_var, axis=1), lambda: scaled_var)
示例3: __call__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def __call__(self, query, key, value, mask=None):
batch_size = tf.shape(query)[0]
max_query_len = tf.shape(query)[1]
max_key_len = tf.shape(key)[1]
wq = tf.transpose(
tf.reshape(self.dense_layers[0](query), [batch_size, max_query_len, self.heads, self.units // self.heads]),
[2, 0, 1, 3]) # Head*B*QL*(U/Head)
wk = tf.transpose(
tf.reshape(self.dense_layers[1](key), [batch_size, max_key_len, self.heads, self.units // self.heads]),
[2, 0, 1, 3]) # Head*B*KL*(U/Head)
wv = tf.transpose(
tf.reshape(self.dense_layers[2](value), [batch_size, max_key_len, self.heads, self.units // self.heads]),
[2, 0, 1, 3]) # Head*B*KL*(U/Head)
attention_score = tf.matmul(wq, wk, transpose_b=True) / tf.sqrt(float(self.units) / self.heads) # Head*B*QL*KL
if query == key and not self.attention_on_itself:
attention_score += tf.matrix_diag(tf.zeros(max_key_len) - 100.0)
if mask is not None:
attention_score += tf.expand_dims(mask, 1)
similarity = tf.nn.softmax(attention_score, -1) # Head*B*QL*KL
return tf.reshape(tf.transpose(tf.matmul(similarity, wv), [1, 2, 0, 3]),
[batch_size, max_query_len, self.units]) # B*QL*U
示例4: _updated_mat
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def _updated_mat(self, mat, v, diag):
# Get dense matrix defined by its square root, which is an update of `mat`:
# A = (mat + v D v^T) (mat + v D v^T)^T
# D is the diagonal matrix with `diag` on the diagonal.
# If diag is None, then it defaults to the identity matrix, so DV^T = V^T
if diag is None:
diag_vt = tf.matrix_transpose(v)
else:
diag_mat = tf.matrix_diag(diag)
diag_vt = tf.batch_matmul(diag_mat, v, adj_y=True)
v_diag_vt = tf.batch_matmul(v, diag_vt)
sqrt = mat + v_diag_vt
a = tf.batch_matmul(sqrt, sqrt, adj_y=True)
return a.eval()
示例5: upsample2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def upsample2d(inputs, strides, padding='SAME', upsample_mode='bilinear'):
if upsample_mode == 'bilinear':
single_bilinear_kernel = get_bilinear_kernel(strides).astype(np.float32)
input_shape = inputs.get_shape().as_list()
bilinear_kernel = tf.matrix_diag(tf.tile(tf.constant(single_bilinear_kernel)[..., None], (1, 1, input_shape[-1])))
outputs = deconv2d(inputs, input_shape[-1], kernel_size=single_bilinear_kernel.shape,
strides=strides, kernel=bilinear_kernel, padding=padding, use_bias=False)
elif upsample_mode == 'nearest':
strides = list(strides) if isinstance(strides, (tuple, list)) else [strides] * 2
input_shape = inputs.get_shape().as_list()
inputs_tiled = tf.tile(inputs[:, :, None, :, None, :], [1, 1, strides[0], 1, strides[1], 1])
outputs = tf.reshape(inputs_tiled, [input_shape[0], input_shape[1] * strides[0],
input_shape[2] * strides[1], input_shape[3]])
else:
raise ValueError("Unknown upsample mode %s" % upsample_mode)
return outputs
示例6: regularizer
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def regularizer(self, kl_loss, z_mean, z_logvar, z_sampled):
cov_z_mean = compute_covariance_z_mean(z_mean)
lambda_d = self.lambda_d_factor * self.lambda_od
if self.dip_type == "i": # Eq 6 page 4
# mu = z_mean is [batch_size, num_latent]
# Compute cov_p(x) [mu(x)] = E[mu*mu^T] - E[mu]E[mu]^T]
cov_dip_regularizer = regularize_diag_off_diag_dip(
cov_z_mean, self.lambda_od, lambda_d)
elif self.dip_type == "ii":
cov_enc = tf.matrix_diag(tf.exp(z_logvar))
expectation_cov_enc = tf.reduce_mean(cov_enc, axis=0)
cov_z = expectation_cov_enc + cov_z_mean
cov_dip_regularizer = regularize_diag_off_diag_dip(
cov_z, self.lambda_od, lambda_d)
else:
raise NotImplementedError("DIP variant not supported.")
return kl_loss + cov_dip_regularizer
示例7: get_laplacian
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def get_laplacian(self, adj_matrix, normalize=True):
"""Compute pairwise distance of a point cloud.
Args:
pairwise distance: tensor (batch_size, num_points, num_points)
Returns:
pairwise distance: (batch_size, num_points, num_points)
"""
if normalize:
D = tf.reduce_sum(adj_matrix, axis=1) # (batch_size,num_points)
eye = tf.ones_like(D)
eye = tf.matrix_diag(eye)
D = 1 / tf.sqrt(D)
D = tf.matrix_diag(D)
L = eye - tf.matmul(tf.matmul(D, adj_matrix), D)
else:
D = tf.reduce_sum(adj_matrix, axis=1) # (batch_size,num_points)
# eye = tf.ones_like(D)
# eye = tf.matrix_diag(eye)
# D = 1 / tf.sqrt(D)
D = tf.matrix_diag(D)
L = D - adj_matrix
return L
示例8: gradient_svd
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def gradient_svd(op, ds, dU, dV):
s, U, V = op.outputs
u_sz = tf.squeeze(tf.slice(tf.shape(dU),[1],[1]))
v_sz = tf.squeeze(tf.slice(tf.shape(dV),[1],[1]))
s_sz = tf.squeeze(tf.slice(tf.shape(ds),[1],[1]))
S = tf.matrix_diag(s)
s_2 = tf.square(s)
eye = tf.expand_dims(tf.eye(s_sz),0)
k = (1 - eye)/(tf.expand_dims(s_2,2)-tf.expand_dims(s_2,1) + eye)
KT = tf.matrix_transpose(k)
KT = removenan(KT)
def msym(X):
return (X+tf.matrix_transpose(X))
def left_grad(U,S,V,dU,dV):
U, V = (V, U); dU, dV = (dV, dU)
D = tf.matmul(dU,tf.matrix_diag(1/(s+1e-8)))
US = tf.matmul(U,S)
grad = tf.matmul(D, V, transpose_b=True)\
+tf.matmul(tf.matmul(U,tf.matrix_diag(tf.matrix_diag_part(-tf.matmul(U,D,transpose_a=True)))), V, transpose_b=True)\
+tf.matmul(2*tf.matmul(US, msym(KT*(tf.matmul(V,-tf.matmul(V,tf.matmul(D,US,transpose_a=True)),transpose_a=True)))),V,transpose_b=True)
grad = tf.matrix_transpose(grad)
return grad
def right_grad(U,S,V,dU,dV):
US = tf.matmul(U,S)
grad = tf.matmul(2*tf.matmul(US, msym(KT*(tf.matmul(V,dV,transpose_a=True))) ),V,transpose_b=True)
return grad
grad = tf.cond(tf.greater(v_sz, u_sz), lambda : left_grad(U,S,V,dU,dV),
lambda : right_grad(U,S,V,dU,dV))
return [grad]
示例9: testVector
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def testVector(self):
with self.test_session(use_gpu=self._use_gpu):
v = np.array([1.0, 2.0, 3.0])
mat = np.diag(v)
v_diag = tf.matrix_diag(v)
self.assertEqual((3, 3), v_diag.get_shape())
self.assertAllEqual(v_diag.eval(), mat)
示例10: testBatchVector
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def testBatchVector(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]]])
v_batch_diag = tf.matrix_diag(v_batch)
self.assertEqual((2, 3, 3), v_batch_diag.get_shape())
self.assertAllEqual(v_batch_diag.eval(), mat_batch)
示例11: testInvalidShape
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def testInvalidShape(self):
with self.assertRaisesRegexp(ValueError, "must be at least rank 1"):
tf.matrix_diag(0)
示例12: testInvalidShapeAtEval
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def testInvalidShapeAtEval(self):
with self.test_session(use_gpu=self._use_gpu):
v = tf.placeholder(dtype=tf.float32)
with self.assertRaisesOpError("input must be at least 1-dim"):
tf.matrix_diag(v).eval(feed_dict={v: 0.0})
示例13: testGrad
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def testGrad(self):
shapes = ((3,), (7, 4))
with self.test_session(use_gpu=self._use_gpu):
for shape in shapes:
x = tf.constant(np.random.rand(*shape), np.float32)
y = tf.matrix_diag(x)
error = tf.test.compute_gradient_error(x, x.get_shape().as_list(),
y, y.get_shape().as_list())
self.assertLess(error, 1e-4)
示例14: _diag_to_matrix
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def _diag_to_matrix(self, diag):
return tf.matrix_diag(diag).eval()
示例15: testSample
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_diag [as 别名]
def testSample(self):
mu = [-1.0, 1.0]
diag = [1.0, 2.0]
with self.test_session():
dist = distributions.MultivariateNormalDiag(mu, diag)
samps = dist.sample(1000, seed=0).eval()
cov_mat = tf.matrix_diag(diag).eval()**2
self.assertAllClose(mu, samps.mean(axis=0), atol=0.1)
self.assertAllClose(cov_mat, np.cov(samps.T), atol=0.1)