def psd_solve_with_chol(node): if node.op == solve: A, b = node.inputs # result is solution Ax=b if is_psd(A): L = cholesky(A) # N.B. this can be further reduced to a yet-unwritten cho_solve Op # __if__ no other Op makes use of the the L matrix during the # stabilization Li_b = Solve('lower_triangular')(L, b) x = Solve('upper_triangular')(L.T, Li_b) return [x]
def tag_solve_triangular(node): """ If a general solve() is applied to the output of a cholesky op, then replace it with a triangular solve. """ if node.op == solve: if node.op.A_structure == 'general': A, b = node.inputs # result is solution Ax=b if A.owner and isinstance(A.owner.op, type(cholesky)): if A.owner.op.lower: return [Solve('lower_triangular')(A, b)] else: return [Solve('upper_triangular')(A, b)] if (A.owner and isinstance(A.owner.op, DimShuffle) and A.owner.op.new_order == (1, 0)): A_T, = A.owner.inputs if A_T.owner and isinstance(A_T.owner.op, type(cholesky)): if A_T.owner.op.lower: return [Solve('upper_triangular')(A, b)] else: return [Solve('lower_triangular')(A, b)]
def verify_solve_grad(self, m, n, A_structure, lower, rng): # ensure diagonal elements of A relatively large to avoid numerical # precision issues A_val = (rng.normal(size=(m, m)) * 0.5 + np.eye(m)).astype(config.floatX) if A_structure == "lower_triangular": A_val = np.tril(A_val) elif A_structure == "upper_triangular": A_val = np.triu(A_val) if n is None: b_val = rng.normal(size=m).astype(config.floatX) else: b_val = rng.normal(size=(m, n)).astype(config.floatX) eps = None if config.floatX == "float64": eps = 2e-8 solve_op = Solve(A_structure=A_structure, lower=lower) utt.verify_grad(solve_op, [A_val, b_val], 3, rng, eps=eps)
def setUp(self): super(test_Solve, self).setUp() self.op_class = Solve self.op = Solve()
import numpy as np try: import scipy.linalg imported_scipy = True except ImportError: # some ops (e.g. Cholesky, Solve, A_Xinv_b) won't work imported_scipy = False from theano import tensor import theano.tensor from theano.tensor import as_tensor_variable from theano.gof import Op, Apply from theano.tensor.slinalg import Solve solve_upper_triangular = Solve(A_structure='upper_triangular', lower=False) class QR_Chol(Op): """ (mostly) Copy from theano: Incomplete QR Decomposition. Computes the QR decomposition of a matrix. Factor the matrix a as qr and return a single matrix. """ __props__ = ('mode', ) def __init__(self, mode): self.mode = mode
def setup_method(self): self.op_class = Solve self.op = Solve() super().setup_method()
def __init__(self, driver='gelsy'): self.driver = driver Solve.__init__(self)
def __init__(self, lambda_=None): Solve.__init__(self) self.lambda_ = lambda_ if lambda_ is not None else 0.