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


Python array_ops.matrix_diag函数代码示例

本文整理汇总了Python中tensorflow.python.ops.array_ops.matrix_diag函数的典型用法代码示例。如果您正苦于以下问题:Python matrix_diag函数的具体用法?Python matrix_diag怎么用?Python matrix_diag使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了matrix_diag函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _makeTridiagonalMatrix

  def _makeTridiagonalMatrix(self, superdiag, maindiag, subdiag):
    super_pad = [[0, 0], [0, 1], [1, 0]]
    sub_pad = [[0, 0], [1, 0], [0, 1]]

    super_part = array_ops.pad(array_ops.matrix_diag(superdiag), super_pad)
    main_part = array_ops.matrix_diag(maindiag)
    sub_part = array_ops.pad(array_ops.matrix_diag(subdiag), sub_pad)
    return super_part + main_part + sub_part
开发者ID:aritratony,项目名称:tensorflow,代码行数:8,代码来源:tridiagonal_matmul_op_test.py

示例2: Test

  def Test(self):
    np.random.seed(1)
    n = shape_[-1]
    batch_shape = shape_[:-2]
    np_dtype = dtype_.as_numpy_dtype
    a = np.random.uniform(
        low=-1.0, high=1.0, size=n * n).reshape([n, n]).astype(np_dtype)
    if dtype_.is_complex:
      a += 1j * np.random.uniform(
          low=-1.0, high=1.0, size=n * n).reshape([n, n]).astype(np_dtype)
    a += np.conj(a.T)
    a = np.tile(a, batch_shape + (1, 1))
    if dtype_ in (dtypes_lib.float32, dtypes_lib.complex64):
      atol = 1e-4
    else:
      atol = 1e-12
    np_e, np_v = np.linalg.eigh(a)
    with self.test_session():
      if compute_v_:
        tf_e, tf_v = linalg_ops.self_adjoint_eig(constant_op.constant(a))

        # Check that V*diag(E)*V^T is close to A.
        a_ev = math_ops.matmul(
            math_ops.matmul(tf_v, array_ops.matrix_diag(tf_e)),
            tf_v,
            adjoint_b=True)
        self.assertAllClose(a_ev.eval(), a, atol=atol)

        # Compare to numpy.linalg.eigh.
        CompareEigenDecompositions(self, np_e, np_v,
                                   tf_e.eval(), tf_v.eval(), atol)
      else:
        tf_e = linalg_ops.self_adjoint_eigvals(constant_op.constant(a))
        self.assertAllClose(
            np.sort(np_e, -1), np.sort(tf_e.eval(), -1), atol=atol)
开发者ID:1000sprites,项目名称:tensorflow,代码行数:35,代码来源:self_adjoint_eig_op_test.py

示例3: testVector

 def testVector(self):
   with self.session(use_gpu=True):
     v = np.array([1.0, 2.0, 3.0])
     mat = np.diag(v)
     v_diag = array_ops.matrix_diag(v)
     self.assertEqual((3, 3), v_diag.get_shape())
     self.assertAllEqual(v_diag.eval(), mat)
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:7,代码来源:diag_op_test.py

示例4: testSampleWithBroadcastScale

  def testSampleWithBroadcastScale(self):
    # mu corresponds to a 2-batch of 3-variate normals
    mu = np.zeros([2, 3])

    # diag corresponds to no batches of 3-variate normals
    diag = np.ones([3])

    with self.test_session():
      dist = ds.VectorExponentialDiag(mu, diag, validate_args=True)

      mean = dist.mean()
      self.assertAllEqual([2, 3], mean.get_shape())
      self.assertAllClose(mu + diag, mean.eval())

      n = int(1e4)
      samps = dist.sample(n, seed=0).eval()
      samps_centered = samps - samps.mean(axis=0)
      cov_mat = array_ops.matrix_diag(diag).eval()**2
      sample_cov = np.matmul(samps_centered.transpose([1, 2, 0]),
                             samps_centered.transpose([1, 0, 2])) / n

      self.assertAllClose(mu + diag, samps.mean(axis=0),
                          atol=0.10, rtol=0.05)
      self.assertAllClose([cov_mat, cov_mat], sample_cov,
                          atol=0.10, rtol=0.05)
开发者ID:1000sprites,项目名称:tensorflow,代码行数:25,代码来源:vector_exponential_diag_test.py

示例5: test_broadcast_matmul_and_solve

  def test_broadcast_matmul_and_solve(self):
    # These cannot be done in the automated (base test class) tests since they
    # test shapes that tf.matmul cannot handle.
    # In particular, tf.matmul does not broadcast.
    with self.test_session() as sess:
      x = random_ops.random_normal(shape=(2, 2, 3, 4))

      # This LinearOperatorDiag will be broadcast to (2, 2, 3, 3) during solve
      # and matmul with 'x' as the argument.
      diag = random_ops.random_uniform(shape=(2, 1, 3))
      operator = linalg.LinearOperatorDiag(diag, is_self_adjoint=True)
      self.assertAllEqual((2, 1, 3, 3), operator.shape)

      # Create a batch matrix with the broadcast shape of operator.
      diag_broadcast = array_ops.concat((diag, diag), 1)
      mat = array_ops.matrix_diag(diag_broadcast)
      self.assertAllEqual((2, 2, 3, 3), mat.get_shape())  # being pedantic.

      operator_matmul = operator.matmul(x)
      mat_matmul = math_ops.matmul(mat, x)
      self.assertAllEqual(operator_matmul.get_shape(), mat_matmul.get_shape())
      self.assertAllClose(*sess.run([operator_matmul, mat_matmul]))

      operator_solve = operator.solve(x)
      mat_solve = linalg_ops.matrix_solve(mat, x)
      self.assertAllEqual(operator_solve.get_shape(), mat_solve.get_shape())
      self.assertAllClose(*sess.run([operator_solve, mat_solve]))
开发者ID:AndrewTwinz,项目名称:tensorflow,代码行数:27,代码来源:linear_operator_diag_test.py

示例6: _covariance

 def _covariance(self):
   if (isinstance(self.scale, linalg.LinearOperatorIdentity) or
       isinstance(self.scale, linalg.LinearOperatorScaledIdentity) or
       isinstance(self.scale, linalg.LinearOperatorDiag)):
     return array_ops.matrix_diag(math_ops.square(self.scale.diag_part()))
   else:
     # TODO(b/35040238): Remove transpose once LinOp supports `transpose`.
     return self.scale.apply(array_ops.matrix_transpose(self.scale.to_dense()))
开发者ID:jzuern,项目名称:tensorflow,代码行数:8,代码来源:mvn_linear_operator.py

示例7: _testBatchVector

 def _testBatchVector(self, dtype):
   with self.cached_session(use_gpu=True):
     v_batch = np.array([[1.0, 0.0, 3.0], [4.0, 5.0, 6.0]]).astype(dtype)
     mat_batch = np.array([[[1.0, 0.0, 0.0], [0.0, 0.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]]]).astype(dtype)
     v_batch_diag = array_ops.matrix_diag(v_batch)
     self.assertEqual((2, 3, 3), v_batch_diag.get_shape())
     self.assertAllEqual(v_batch_diag.eval(), mat_batch)
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:9,代码来源:diag_op_test.py

示例8: eye

def eye(
    num_rows,
    num_columns=None,
    batch_shape=None,
    dtype=dtypes.float32,
    name=None):
  """Construct an identity matrix, or a batch of matrices.

  ```python
  # Construct one identity matrix.
  tf.eye(2)
  ==> [[1., 0.],
       [0., 1.]]

  # Construct a batch of 3 identity matricies, each 2 x 2.
  # batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2.
  batch_identity = tf.eye(2, batch_shape=[3])

  # Construct one 2 x 3 "identity" matrix
  tf.eye(2, num_columns=3)
  ==> [[ 1.,  0.,  0.],
       [ 0.,  1.,  0.]]
  ```

  Args:
    num_rows: Non-negative `int32` scalar `Tensor` giving the number of rows
      in each batch matrix.
    num_columns: Optional non-negative `int32` scalar `Tensor` giving the number
      of columns in each batch matrix.  Defaults to `num_rows`.
    batch_shape:  `int32` `Tensor`.  If provided, returned `Tensor` will have
      leading batch dimensions of this shape.
    dtype:  The type of an element in the resulting `Tensor`
    name:  A name for this `Op`.  Defaults to "eye".

  Returns:
    A `Tensor` of shape `batch_shape + [num_rows, num_columns]`
  """
  with ops.name_scope(
      name, default_name="eye", values=[num_rows, num_columns, batch_shape]):

    batch_shape = [] if batch_shape is None else batch_shape
    batch_shape = ops.convert_to_tensor(
        batch_shape, name="shape", dtype=dtypes.int32)

    if num_columns is None:
      diag_size = num_rows
    else:
      diag_size = math_ops.minimum(num_rows, num_columns)
    diag_shape = array_ops.concat_v2((batch_shape, [diag_size]), 0)
    diag_ones = array_ops.ones(diag_shape, dtype=dtype)

    if num_columns is None:
      return array_ops.matrix_diag(diag_ones)
    else:
      shape = array_ops.concat_v2((batch_shape, [num_rows, num_columns]), 0)
      zero_matrix = array_ops.zeros(shape, dtype=dtype)
      return array_ops.matrix_set_diag(zero_matrix, diag_ones)
开发者ID:curtiszimmerman,项目名称:tensorflow,代码行数:57,代码来源:linalg_ops.py

示例9: testSample

  def testSample(self):
    mu = [-1., 1]
    diag = [1., -2]
    with self.cached_session():
      dist = ds.MultivariateNormalDiag(mu, diag, validate_args=True)
      samps = dist.sample(int(1e3), seed=0).eval()
      cov_mat = array_ops.matrix_diag(diag).eval()**2

      self.assertAllClose(mu, samps.mean(axis=0), atol=0., rtol=0.05)
      self.assertAllClose(cov_mat, np.cov(samps.T), atol=0.05, rtol=0.05)
开发者ID:ahmedsaiduk,项目名称:tensorflow,代码行数:10,代码来源:mvn_diag_test.py

示例10: testMultivariateNormalDiagWithSoftplusStDev

  def testMultivariateNormalDiagWithSoftplusStDev(self):
    mu = [-1.0, 1.0]
    diag = [-1.0, -2.0]
    with self.test_session():
      dist = distributions.MultivariateNormalDiagWithSoftplusStDev(mu, diag)
      samps = dist.sample(1000, seed=0).eval()
      cov_mat = array_ops.matrix_diag(nn_ops.softplus(diag)).eval()**2

      self.assertAllClose(mu, samps.mean(axis=0), atol=0.1)
      self.assertAllClose(cov_mat, np.cov(samps.T), atol=0.1)
开发者ID:AliMiraftab,项目名称:tensorflow,代码行数:10,代码来源:mvn_test.py

示例11: _covariance

 def _covariance(self):
   # Let
   #   W = (w1,...,wk), with wj ~ iid Exponential(0, 1).
   # Then this distribution is
   #   X = loc + LW,
   # and then since Cov(wi, wj) = 1 if i=j, and 0 otherwise,
   #   Cov(X) = L Cov(W W^T) L^T = L L^T.
   if distribution_util.is_diagonal_scale(self.scale):
     return array_ops.matrix_diag(math_ops.square(self.scale.diag_part()))
   else:
     return self.scale.matmul(self.scale.to_dense(), adjoint_arg=True)
开发者ID:AbhinavJain13,项目名称:tensorflow,代码行数:11,代码来源:vector_exponential_linear_operator.py

示例12: testGrad

 def testGrad(self):
   shapes = ((3,), (7, 4))
   with self.session(use_gpu=True):
     for shape in shapes:
       x = constant_op.constant(np.random.rand(*shape), np.float32)
       y = array_ops.matrix_diag(x)
       error = gradient_checker.compute_gradient_error(x,
                                                       x.get_shape().as_list(),
                                                       y,
                                                       y.get_shape().as_list())
       self.assertLess(error, 1e-4)
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:11,代码来源:diag_op_test.py

示例13: testSample

  def testSample(self):
    mu = [-1., 1]
    diag = [1., -2]
    with self.test_session():
      dist = ds.VectorLaplaceDiag(mu, diag, validate_args=True)
      samps = dist.sample(int(1e4), seed=0).eval()
      cov_mat = 2. * array_ops.matrix_diag(diag).eval()**2

      self.assertAllClose(mu, samps.mean(axis=0),
                          atol=0., rtol=0.05)
      self.assertAllClose(cov_mat, np.cov(samps.T),
                          atol=0.05, rtol=0.05)
开发者ID:1000sprites,项目名称:tensorflow,代码行数:12,代码来源:vector_laplace_diag_test.py

示例14: _build_operator_and_mat

 def _build_operator_and_mat(self, batch_shape, k, dtype=np.float64):
   # Build an identity matrix with right shape and dtype.
   # Build an operator that should act the same way.
   batch_shape = list(batch_shape)
   diag_shape = batch_shape + [k]
   matrix_shape = batch_shape + [k, k]
   diag = array_ops.ones(diag_shape, dtype=dtype)
   scale = constant_op.constant(2.0, dtype=dtype)
   scaled_identity_matrix = scale * array_ops.matrix_diag(diag)
   operator = operator_pd_identity.OperatorPDIdentity(
       matrix_shape, dtype, scale=scale)
   return operator, scaled_identity_matrix.eval()
开发者ID:AlbertXiebnu,项目名称:tensorflow,代码行数:12,代码来源:operator_pd_identity_test.py

示例15: testSample

  def testSample(self):
    mu = [-2., 1]
    diag = [1., -2]
    with self.cached_session():
      dist = ds.VectorExponentialDiag(mu, diag, validate_args=True)
      samps = dist.sample(int(1e4), seed=0).eval()
      cov_mat = array_ops.matrix_diag(diag).eval()**2

      self.assertAllClose([-2 + 1, 1. - 2], samps.mean(axis=0),
                          atol=0., rtol=0.05)
      self.assertAllClose(cov_mat, np.cov(samps.T),
                          atol=0.05, rtol=0.05)
开发者ID:Ajaycs99,项目名称:tensorflow,代码行数:12,代码来源:vector_exponential_diag_test.py


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