def make_node(self, x): ctx_name = infer_context_name(x) x = as_gpuarray_variable(x, ctx_name) assert x.dtype == 'float32' if x.ndim != 2: raise LinAlgError("Matrix rank error") return theano.Apply(self, [x], [x.type()])
def make_node(self, A): ctx_name = infer_context_name(A) A = as_gpuarray_variable(A, ctx_name) A = gpu_contiguous(A) if A.ndim != 2: raise LinAlgError("Matrix rank error") if A.dtype != "float32": raise TypeError("only `float32` is supported for now") if self.compute_uv: return Apply( self, [A], # return S, U, VT [ GpuArrayType(A.dtype, broadcastable=[False], context_name=ctx_name)(), A.type(), A.type(), ], ) else: return Apply( self, [A], # return only S [ GpuArrayType(A.dtype, broadcastable=[False], context_name=ctx_name)() ], )
def perform(self, node, inputs, outputs): context = inputs[0][0].context # Input matrix. A = inputs[0] l, n = A.shape if l != n: raise ValueError('A must be a square matrix') lda = max(1, n) # cusolver operates on F ordered matrices, but A is expected # to be symmetric so it does not matter. # We copy A if needed if self.inplace: L = A else: L = pygpu.array(A, copy=True) # The output matrix will contain only the upper or lower # triangular factorization of A. If L is C ordered (it # probably is as it is the default in Theano) we just switch # the fill mode parameter of cusolver l_parameter = 0 if self.lower else 1 if L.flags['C_CONTIGUOUS']: l_parameter = 1 - l_parameter L_ptr = L.gpudata with context: workspace_size = cusolver.cusolverDnSpotrf_bufferSize( context.cusolver_handle, l_parameter, n, L_ptr, lda) workspace = pygpu.zeros(workspace_size, dtype='float32', context=context) dev_info = pygpu.zeros((1, ), dtype='int32', context=context) workspace_ptr = workspace.gpudata dev_info_ptr = dev_info.gpudata cusolver.cusolverDnSpotrf(context.cusolver_handle, l_parameter, n, L_ptr, lda, workspace_ptr, workspace_size, dev_info_ptr) val_dev_info = np.asarray(dev_info)[0] if val_dev_info > 0: raise LinAlgError('Cholesky decomposition failed (is A SPD?)') # cusolver leaves the elements in the matrix outside the considered # upper or lower triangle unchanged, so we need to put zeros outside # the triangle if self.lower: tril(L) else: triu(L) outputs[0][0] = L
def make_node(self, A): ctx_name = infer_context_name(A) A = as_gpuarray_variable(A, ctx_name) if A.ndim != 2: raise LinAlgError("Matrix rank error") assert A.dtype == 'float32' if self.compute_uv: return theano.Apply( self, [A], # return S, U, VT [ GpuArrayType(A.dtype, broadcastable=[False], context_name=ctx_name)(), A.type(), A.type() ]) else: return theano.Apply( self, [A], # return only S [ GpuArrayType(A.dtype, broadcastable=[False], context_name=ctx_name)() ])
def make_node(self, A): ctx_name = infer_context_name(A) A = as_gpuarray_variable(A, ctx_name) A = gpu_contiguous(A) if A.ndim != 2: raise LinAlgError("Matrix rank error") if A.dtype != "float32": raise TypeError("only `float32` is supported for now") return Apply(self, [A], [A.type()])
def perform(self, node, inputs, outputs): context = inputs[0][0].context # Input matrix. A = inputs[0] l, n = A.shape if l != n: raise ValueError('A must be a square matrix') lda = max(1, n) # cusolver operates on F ordered matrices if not self.inplace: LU = pygpu.array(A, copy=True, order='F') else: LU = A.T if A.flags['C_CONTIGUOUS'] else A LU_ptr = LU.gpudata with context: workspace_size = cusolver.cusolverDnSgetrf_bufferSize( context.cusolver_handle, n, n, LU_ptr, lda) workspace = pygpu.zeros(workspace_size, dtype='float32', context=context) pivots = pygpu.zeros(n, dtype='int32', context=context) dev_info = pygpu.zeros((1, ), dtype='int32', context=context) workspace_ptr = workspace.gpudata pivots_ptr = pivots.gpudata dev_info_ptr = dev_info.gpudata cusolver.cusolverDnSgetrf(context.cusolver_handle, n, n, LU_ptr, lda, workspace_ptr, pivots_ptr, dev_info_ptr) if self.check_output: val_dev_info = np.asarray(dev_info)[0] if val_dev_info > 0: raise LinAlgError('LU decomposition failed') outputs[1][0] = pivots outputs[0][0] = LU
def make_node(self, A): ctx_name = infer_context_name(A) A = as_gpuarray_variable(A, ctx_name) A = gpu_contiguous(A) if A.ndim != 2: raise LinAlgError("Matrix rank error") if A.dtype != 'float32': raise TypeError("only `float32` is supported for now") if self.complete: return theano.Apply(self, [A], # return R, Q [A.type(), A.type()]) else: return theano.Apply(self, [A], # return R [A.type()])
def PolynomialRegression(y,x,n): try: #due to recursive error handling there must be a base case if n < 0: return 0 #manually raises error if y and x aren't compatible if len(y) != len(x): raise ValueError("the length of 'y' and 'x' are different") #creates matrix of values corresponding to powers of x from 0 to n X = np.matrix([[k**i for i in range(n+1)] for k in x]) #matrix is Xt * X, where Xt is the transpose of X XX = transpose(X)*X #finds eigenvectors and eigenvalues lambdas, V = np.linalg.eig(XX) #if zero is an eigenvalue, then there is a linear dependency: a non-trivial definition of 0 #this is better than relying on the program to raise a LinAlgError when attempting to invert XX #because the method used to calculate inverse for larger matrices can be quite inaccurate if any([abs(i) < 10**-13 for i in lambdas]): raise LinAlgError("n") #in this model, y = Xb, where X is the matrix of powers of X, and b are their corresponding coefficients #it can be proven that the equation of best fit is given by b = (Xt * X)^-1(Xt * y) Xy = transpose(X)*transpose(np.matrix(y)) b = XX.I*Xy #creates symbolic equation for polynomial x = sym.Symbol("x") #evaluates polynomial using Horner's rule y = float(b[n][0]) for i in range(n-1,-1,-1): y = y * x + float(b[i][0]); #returns polynomial return sym.expand(y) except LinAlgError: #LinAlgError occurs only when Xt X is not invertible #this occurs when the degree of the polynomial is too high #to combat this, reduce the degree of the polynomial by 1 and trying again return PolynomialRegression(y,x,n-1)
def svd_gesvd(a, full_matrices=True, compute_uv=True, check_finite=True): """svd with LAPACK's '#gesvd' (with # = d/z for float/complex). Similar as :func:`numpy.linalg.svd`, but use LAPACK 'gesvd' driver. Works only with 2D arrays. Outer part is based on the code of `numpy.linalg.svd`. Parameters ---------- a, full_matrices, compute_uv : See :func:`numpy.linalg.svd` for details. check_finite : check whether input arrays contain 'NaN' or 'inf'. Returns ------- U, S, Vh : ndarray See :func:`numpy.linalg.svd` for details. """ a, wrap = _makearray(a) # uses order='C' if a.ndim != 2: raise LinAlgError("array must be 2D!") if a.size == 0 or np.product(a.shape) == 0: raise LinAlgError("array cannot be empty") if check_finite: if not isfinite(a).all(): raise LinAlgError("Array must not contain infs or NaNs") M, N = a.shape # determine types t, result_t = _commonType(a) # t = type for calculation, (for my numpy version) actually always one of {double, cdouble} # result_t = one of {single, double, csingle, cdouble} is_complex = isComplexType(t) real_t = _realType(t) # real version of t with same precision # copy: the array is destroyed a = _fastCopyAndTranspose( t, a) # casts a to t, copy and transpose (=change to order='F') lapack_routine = _get_gesvd(t) # allocate output space & options if compute_uv: if full_matrices: nu = M lvt = N option = b'A' else: nu = min(N, M) lvt = min(N, M) option = b'S' u = np.zeros((M, nu), t, order='F') vt = np.zeros((lvt, N), t, order='F') else: option = b'N' nu = 1 u = np.empty((1, 1), t, order='F') vt = np.empty((1, 1), t, order='F') s = np.zeros((min(N, M), ), real_t, order='F') INFO = c_int(0) m = c_int(M) n = c_int(N) lu = c_int(u.shape[0]) lvt = c_int(vt.shape[0]) work = np.zeros((1, ), t) lwork = c_int(-1) # first call with lwork=-1 args = [option, option, m, n, a, m, s, u, lu, vt, lvt, work, lwork, INFO] if is_complex: # differnt call signature: additional array 'rwork' of fixed size rwork = np.zeros((5 * min(N, M), ), real_t) args.insert(-1, rwork) lapack_routine( *args) # first call: just calculate the required `work` size if INFO.value < 0: raise Exception('%d-th argument had an illegal value' % INFO.value) if is_complex: lwork = int(work[0].real) else: lwork = int(work[0]) work = np.zeros((lwork, ), t, order='F') args[11] = work args[12] = c_int(lwork) lapack_routine(*args) # second call: the actual calculation if INFO.value < 0: raise Exception('%d-th argument had an illegal value' % INFO.value) if INFO.value > 0: raise LinAlgError("SVD did not converge with 'gesvd'") s = s.astype(_realType(result_t)) if compute_uv: u = u.astype(result_t) # no repeated transpose: used fortran order vt = vt.astype(result_t) return wrap(u), s, wrap(vt) else: return s
def check_dev_info(self, dev_info): val = np.asarray(dev_info)[0] if val > 0: raise LinAlgError("A is singular")
def calc_Wc(self, method=None): """ Computes observers :math:`W_c` with method 'method' : :math:`W_c` is solution of equation : .. math:: A * W_c * A^T + B * B^T = W_c Available methods : - ``linalg`` : ``scipy.linalg.solve_discrete_lyapunov``, 4-digit precision with small sizes, 1 digit precision with bilinear algorithm for big matrixes (really bad). not good enough with usual python data types - ``slycot`` : using ``slycot`` lib with func ``sb03md``, like in [matlab ,pydare] see http://slicot.org/objects/software/shared/libindex.html - ``None`` (default) : use the default method defined in the dSS class (dSS._W_method) ..Example:: >>> mydSS = random_dSS() ## define a new state space from random data >>> mydSS.calc_Wc('linalg') # use numpy >>> mydSS.calc_Wc('slycot') # use slycot >>> mydSS.calc_Wo() # use the default method defined in dSS .. warning:: solve_discrete_lyapunov does not work as intended, see http://stackoverflow.com/questions/16315645/am-i-using-scipy-linalg-solve-discrete-lyapunov-correctl Precision is not good (4 digits, failed tests) """ if method is None: method = dSS._W_method if method == 'linalg': try: X = solve_discrete_lyapunov(self._A, self._B * self._B.transpose()) self._Wc = mat(X) except LinAlgError as ve: if ve.info < 0: e = LinAlgError(ve.message) e.info = ve.info else: e = LinAlgError( "dSS: Wc: scipy Linalg failed to compute eigenvalues of Lyapunov equation" ) e.info = ve.info raise e elif method == 'slycot': # Solve the Lyapunov equation by calling the Slycot function sb03md # If we don't use "copy" in the call, the result is plain false try: X, scale, sep, ferr, w = sb03md(self.n, -self._B * self._B.transpose(), copy(self._A), eye(self.n, self.n), dico='D', trana='T') self._Wc = mat(X) except ValueError as ve: if ve.info < 0: e = ValueError(ve.message) e.info = ve.info else: e = ValueError( "dSS: Wc: The QR algorithm failed to compute all the eigenvalues " "(see LAPACK Library routine DGEES).") e.info = ve.info raise e except NameError: return self.calc_Wc(method='linalg') else: raise ValueError( "dSS: Unknown method to calculate observers (method=%s)" % method)
def check_dev_info(self, dev_info): val = numpy.asarray(dev_info)[0] if val > 0: raise LinAlgError('A is singular')
xs_valid = [x_valid.transpose(0, 3, 1, 2) for x_valid in xs_valid] if debug: print np.shape(xs_valid[0]) from numpy.linalg.linalg import LinAlgError validation_data = ([], y_valid) c = 0 for x in xs_valid[0]: try: validation_data[0].append(get_ellipse_kaggle_par(x)) except LinAlgError, e: print 'try_conv' print c raise LinAlgError(e) c += 1 validation_data = (np.asarray(validation_data[0]), validation_data[1]) t_val = (time.time() - start_time) print " took %.2f seconds" % (t_val) if continueAnalysis: print "Load model weights" winsol.load_weights(path=WEIGHTS_PATH) winsol.WEIGHTS_PATH = ((WEIGHTS_PATH.split('.', 1)[0] + '_next.h5')) elif get_winsol_weights: print "import weights from run with original kaggle winner solution" winsol.load_weights() elif DO_LSUV_INIT: