当前位置: 首页>>代码示例>>Python>>正文


Python tensorflow.matrix_diag_part方法代码示例

本文整理汇总了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 
开发者ID:aqlaboratory,项目名称:rgn,代码行数:25,代码来源:net_ops.py

示例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 
开发者ID:yyht,项目名称:BERT,代码行数:19,代码来源:gaussian_process.py

示例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 
开发者ID:misonuma,项目名称:strsum,代码行数:23,代码来源:components.py

示例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) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:22,代码来源:gmm_ops.py

示例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) 
开发者ID:befelix,项目名称:safe_learning,代码行数:24,代码来源:functions.py

示例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 
开发者ID:deepmind,项目名称:dynamic-kanerva-machines,代码行数:18,代码来源:memory.py

示例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


######################################################################## 
开发者ID:richardwth,项目名称:MMD-GAN,代码行数:27,代码来源:math_func.py

示例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 
开发者ID:thu-ml,项目名称:zhusuan,代码行数:15,代码来源:svgp.py

示例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 
开发者ID:gradientinstitute,项目名称:aboleth,代码行数:7,代码来源:distributions.py

示例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) 
开发者ID:riga,项目名称:tfdeploy,代码行数:6,代码来源:ops.py

示例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) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:9,代码来源:diag_op_test.py

示例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])) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:10,代码来源:diag_op_test.py

示例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) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:17,代码来源:diag_op_test.py

示例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) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:15,代码来源:diag_op_test.py

示例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) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:5,代码来源:diag_op_test.py


注:本文中的tensorflow.matrix_diag_part方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。