我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用torch.qr()。
def test_ormqr(self): mat1 = torch.randn(10, 10) mat2 = torch.randn(10, 10) q, r = torch.qr(mat1) m, tau = torch.geqrf(mat1) res1 = torch.mm(q, mat2) res2, _ = torch.ormqr(m, tau, mat2) self.assertEqual(res1, res2) res1 = torch.mm(mat2, q) res2, _ = torch.ormqr(m, tau, mat2, False) self.assertEqual(res1, res2) res1 = torch.mm(q.t(), mat2) res2, _ = torch.ormqr(m, tau, mat2, True, True) self.assertEqual(res1, res2) res1 = torch.mm(mat2, q.t()) res2, _ = torch.ormqr(m, tau, mat2, False, True) self.assertEqual(res1, res2)
def orthogonal(tensor, gain=1): if tensor.ndimension() < 2: raise ValueError("Only tensors with 2 or more dimensions are supported") rows = tensor.size(0) cols = tensor[0].numel() flattened = torch.Tensor(rows, cols).normal_(0, 1) if rows < cols: flattened.t_() # Compute the qr factorization q, r = torch.qr(flattened) # Make Q uniform according to https://arxiv.org/pdf/math-ph/0609050.pdf d = torch.diag(r, 0) ph = d.sign() q *= ph.expand_as(q) if rows < cols: q.t_() tensor.view_as(q).copy_(q) tensor.mul_(gain) return tensor
def orthogonal(tensor, gain=1): """Fills the input Tensor or Variable with a (semi) orthogonal matrix, as described in "Exact solutions to the nonlinear dynamics of learning in deep linear neural networks" - Saxe, A. et al. (2013). The input tensor must have at least 2 dimensions, and for tensors with more than 2 dimensions the trailing dimensions are flattened. Args: tensor: an n-dimensional torch.Tensor or autograd.Variable, where n >= 2 gain: optional scaling factor Examples: >>> w = torch.Tensor(3, 5) >>> nn.init.orthogonal(w) """ if isinstance(tensor, Variable): orthogonal(tensor.data, gain=gain) return tensor if tensor.ndimension() < 2: raise ValueError("Only tensors with 2 or more dimensions are supported") rows = tensor.size(0) cols = tensor[0].numel() flattened = torch.Tensor(rows, cols).normal_(0, 1) # Compute the qr factorization q, r = torch.qr(flattened) # Make Q uniform according to https://arxiv.org/pdf/math-ph/0609050.pdf d = torch.diag(r, 0) ph = d.sign() q *= ph.expand_as(q) # Pad zeros to Q (if rows smaller than cols) if rows < cols: padding = torch.zeros(rows, cols - rows) if q.is_cuda: q = torch.cat([q, padding.cuda()], 1) else: q = torch.cat([q, padding], 1) tensor.view_as(q).copy_(q) tensor.mul_(gain) return tensor
def qr(mat: T.FloatTensor) -> T.Tuple[T.FloatTensor]: """ Compute the QR decomposition of a matrix. The QR decomposition factorizes a matrix A into a product A = QR of an orthonormal matrix Q and an upper triangular matrix R. Provides an orthonormalization of the columns of the matrix. Args: mat: A matrix. Returns: (Q, R): Tuple of tensors. """ return torch.qr(mat)
def orthogonal(tensor, gain=1): """Fills the input Tensor or Variable with a (semi) orthogonal matrix, as described in "Exact solutions to the nonlinear dynamics of learning in deep linear neural networks" - Saxe, A. et al. (2013). The input tensor must have at least 2 dimensions, and for tensors with more than 2 dimensions the trailing dimensions are flattened. Args: tensor: an n-dimensional torch.Tensor or autograd.Variable, where n >= 2 gain: optional scaling factor Examples: >>> w = torch.Tensor(3, 5) >>> nn.init.orthogonal(w) """ if isinstance(tensor, Variable): orthogonal(tensor.data, gain=gain) return tensor if tensor.ndimension() < 2: raise ValueError("Only tensors with 2 or more dimensions are supported") rows = tensor.size(0) cols = tensor[0].numel() flattened = torch.Tensor(rows, cols).normal_(0, 1) if rows < cols: flattened.t_() # Compute the qr factorization q, r = torch.qr(flattened) # Make Q uniform according to https://arxiv.org/pdf/math-ph/0609050.pdf d = torch.diag(r, 0) ph = d.sign() q *= ph.expand_as(q) if rows < cols: q.t_() tensor.view_as(q).copy_(q) tensor.mul_(gain) return tensor