def loss_orth(self): penalty = 0 W = self.U.T Wt = W.T WWt = F.matmul(W, Wt) I = cupy.identity(WWt.shape[0]) penalty = penalty + F.sum((WWt - I)**2) W = self.V Wt = W.T WWt = F.matmul(W, Wt) I = cupy.identity(WWt.shape[0]) penalty = penalty + F.sum((WWt - I)**2) spectral_penalty = 0 if self.mode in (4, 5): if (self.D.size > 1): sd2 = 0.1**2 _d = self.D[cupy.argsort(self.D.data)] spectral_penalty += F.mean((1 - _d[:-1])**2 / sd2 - F.log( (_d[1:] - _d[:-1]) + 1e-7)) * 0.05 elif self.mode == 6: spectral_penalty += F.mean(self.D * F.log(self.D)) elif self.mode == 7: spectral_penalty += F.mean(F.exp(self.D)) elif self.mode == 8: spectral_penalty += -F.mean(F.log(self.D)) return penalty + spectral_penalty * 0.1
def _svd_batched(a, a_dtype, full_matrices, compute_uv): batch_shape = a.shape[:-2] batch_size = internal.prod(batch_shape) n, m = a.shape[-2:] s_dtype = a_dtype.lower() # first handle any 0-size inputs if batch_size == 0: k = min(m, n) s = cupy.empty(batch_shape + (k, ), s_dtype) if compute_uv: if full_matrices: u = cupy.empty(batch_shape + (n, n), dtype=a_dtype) vt = cupy.empty(batch_shape + (m, m), dtype=a_dtype) else: u = cupy.empty(batch_shape + (n, k), dtype=a_dtype) vt = cupy.empty(batch_shape + (k, m), dtype=a_dtype) return u, s, vt else: return s elif m == 0 or n == 0: s = cupy.empty(batch_shape + (0, ), s_dtype) if compute_uv: if full_matrices: u = cupy.empty(batch_shape + (n, n), dtype=a_dtype) u[...] = cupy.identity(n, dtype=a_dtype) vt = cupy.empty(batch_shape + (m, m), dtype=a_dtype) vt[...] = cupy.identity(m, dtype=a_dtype) else: u = cupy.empty(batch_shape + (n, 0), dtype=a_dtype) vt = cupy.empty(batch_shape + (0, m), dtype=a_dtype) return u, s, vt else: return s # ...then delegate real computation to cuSOLVER a = a.reshape(-1, *(a.shape[-2:])) if runtime.is_hip or (m <= 32 and n <= 32): # copy is done in _gesvdj_batched, so let's try not to do it here a = a.astype(a_dtype, order='C', copy=False) out = _gesvdj_batched(a, full_matrices, compute_uv, False) else: # manually loop over cusolverDn<t>gesvd() # copy (via possible type casting) is done in _gesvd_batched # note: _gesvd_batched returns V, not V^H out = _gesvd_batched(a, a_dtype, full_matrices, compute_uv, False) if compute_uv: u, s, v = out u = u.reshape(*batch_shape, *(u.shape[-2:])) s = s.reshape(*batch_shape, *(s.shape[-1:])) v = v.reshape(*batch_shape, *(v.shape[-2:])) return u, s, v.swapaxes(-2, -1).conj() else: s = out s = s.reshape(*batch_shape, *(s.shape[-1:])) return s
def _linalg_cho_factor(A, rho, lower=False, check_finite=True): """Patched version of :func:`sporco.linalg.cho_factor`.""" N, M = A.shape if N >= M: c, lwr = _cho_factor( A.T.dot(A) + rho * cp.identity(M, dtype=A.dtype), lower=lower, check_finite=check_finite) else: c, lwr = _cho_factor( A.dot(A.T) + rho * cp.identity(N, dtype=A.dtype), lower=lower, check_finite=check_finite) return c, lwr
def _linalg_cho_factor(A, rho, lower=False, check_finite=True): """Patched version of :func:`sporco.linalg.cho_factor`.""" N, M = A.shape if N >= M: c, lwr = _cho_factor(A.T.dot(A) + rho * cp.identity(M, dtype=A.dtype), lower=lower, check_finite=check_finite) else: c, lwr = _cho_factor(A.dot(A.T) + rho * cp.identity(N, dtype=A.dtype), lower=lower, check_finite=check_finite) return c, lwr
def init(M, N, inweights): v = cp.identity(M, dtype=np.float32) for key in inweights: for m in range(M): inweights[key][:, m] = inweights[key][:, m] - inweights[key][:, m].mean() inweights[key][:, m] = inweights[key][:, m] / cp.linalg.norm(inweights[key][:, m]) return inweights, v
def _get_psi_k(self, phi, k): """Returns the linear causal effect matrices excluding variable k. Parameters ---------- phi : array-like Coefficient matrices at different lags. k : int Variable index to exclude causal effects through. Returns ------- psi_k : array-like, shape (tau_max + 1, N, N) Matrices of causal effects excluding k. """ psi_k = np.zeros((self.tau_max + 1, self.N, self.N)) psi_k[0] = np.identity(self.N) phi_k = np.copy(phi) phi_k[1:, k, :] = 0. for n in range(1, self.tau_max + 1): psi_k[n] = np.zeros((self.N, self.N)) for s in range(1, n + 1): psi_k[n] += np.dot(phi_k[s], psi_k[n - s]) return psi_k
def Fisher(self, key, label, batchsize, nb_class, convert_dim, dimension, affinity): label = cp.array(label) if (self.n_shot == 1): Sw = cp.identity(dimension, dtype='float32') else: Sw = self.local_cov_in_class(key.data, label, nb_class, batchsize, affinity) #Sw=self.local_cov_in_class_NN(key.data,label,nb_class,batchsize,5) Sb = self.local_cov_bet_class(key.data, label, nb_class, batchsize, affinity) #Sb=self.local_cov_bet_class_NN(key.data,label,nb_class,batchsize,5) Sb_Sw = Sb - 0.5 * Sw if (self.n_shot == 1): Sb_Sw = Sb lam, v = np.linalg.eigh(Sb_Sw) lam = cp.asarray(lam) v = cp.asarray(v) eigen_id = cp.argsort(lam)[::-1] eigen_value = lam[eigen_id] eigen_vector = v[:, eigen_id] W = eigen_vector[:, :convert_dim] W = cp.reshape(W, [dimension, convert_dim]) W = W / cp.reshape(cp.linalg.norm(W, axis=0), [1, convert_dim]) W = F.transpose(W) return W
def _get_covariance_matrix(parents_neighbors_coeffs): """ Determines the covariance matrix for correlated innovations Parameters ---------- parents_neighbors_coeffs : dict Dictionary of format: {..., j:[((var1, lag1), coef1), ((var2, lag2), coef2), ...], ...} for all variables where vars must be in [0..N-1] and lags <= 0 with number of variables N. Returns ------- covar_matrix : numpy array Covariance matrix implied by the parents_neighbors_coeffs. Used to generate correlated innovations. """ # Get the total number of nodes _, max_node_id = \ _find_max_time_lag_and_node_id(parents_neighbors_coeffs) n_nodes = max_node_id + 1 # Initialize the covariance matrix covar_matrix = np.identity(n_nodes) # Iterate through all the node connections for j, i, tau, coeff in _iter_coeffs(parents_neighbors_coeffs): # Add to covar_matrix if node connection is instantaneous if tau == 0: covar_matrix[j, i] = coeff return covar_matrix
def __init__(self, kernel: Callable, lower: float, upper: float, grid_size: int, observations: np.ndarray, sample_size: int, order: int = 1, adjoint: bool = False, quadrature: str = 'rectangle', **kwargs): """ Instance of iterated Tikhonov solver for inverse problem in Poisson noise with integral operator. :param kernel: Kernel of the integral operator. :type kernel: Callable :param lower: Lower end of the interval on which the operator is defined. :type lower: float :param upper: Upper end of the interval on which the operator is defined. :type upper: float :param grid_size: Size pf grid used to approximate the operator. :type grid_size: int :param observations: Observations used for the estimation. :type observations: numpy.ndarray :param sample_size: Theoretical sample size (n). :type sample_size: int :param order: Order of the iterated algorithm. Estimator for each regularization parameter is obtained after order iterations. Ordinary Tikhonov estimator is obtained for order = 1. :type order: int (default: 1) :param adjoint: Whether the operator is adjoint (True) or not (False). :type adjoint: boolean (default: False) :param quadrature: Type of quadrature used to approximate integrals. :type quadrature: str (default: recatngle) :param kwargs: Possible arguments: - tau: Parameter used to rescale the obtained values of estimated noise level (float or int, default: 1). - parameter_space_size: Number of possible values of regularization parameter calculated as values between 10^(-15) and 1 with step dictated by the parameter_space_size (int, default: 100). - grid_max_iter: Maximum number of iterations of routine looking for the reasonable starting point for grid search. In each iteration, parameter grid is shifted by 1. In case no reasonable starting point has been found, RuntimeWarning is raised (int, default: 50). """ Operator.__init__(self, kernel, lower, upper, grid_size, adjoint, quadrature) EstimatorDiscretize.__init__(self, kernel, lower, upper, grid_size, observations, sample_size, quadrature) assert isinstance(order, int), 'Please specify the order as an integer' self.__order: int = order self.grid_max_iter: int = kwargs.get('grid_max_iter', 50) assert isinstance(self.grid_max_iter, int) self.__tau: float = kwargs.get('tau', 1.) assert isinstance(self.__tau, (int, float)), 'tau must be a number' self.__parameter_space_size: int = kwargs.get('parameter_space_size', 100) try: assert isinstance(self.__parameter_space_size, int) except AssertionError: warn('Parameter space size is not an integer, falling back to default value') self.__parameter_space_size = 100 self.__parameter_grid: np.ndarray = np.flip(np.power(10, np.linspace(-15, 0, self.__parameter_space_size))) self.initial: cp.ndarray = cp.empty(self.grid_size, dtype=cp.float64) self.previous: cp.ndarray = cp.empty(self.grid_size, dtype=cp.float64) self.current: cp.ndarray = cp.empty(self.grid_size, dtype=cp.float64) self.__solution: cp.ndarray = cp.empty(self.initial.shape, dtype=cp.float64) Operator.approximate(self) self.__KHK: cp.ndarray = self.__premultiplication(self.KH, self.K) self.identity: cp.ndarray = cp.identity(self.grid_size, dtype=cp.float64) EstimatorDiscretize.estimate_q(self) EstimatorDiscretize.estimate_delta(self) self.__grid: np.ndarray = getattr(super(), quadrature + '_grid')() self.__define_grid()
def get_amce(self, k, lag_mode='absmax', exclude_k=True, exclude_self_effects=True): """Returns the average mediated causal effect. This is the Average Mediated Causal Effect (AMCE) through a variable k With lag_mode='absmax' this is based on the lag of maximum CE for each pair. Parameters ---------- k : int Index of variable. lag_mode : {'absmax', 'all_lags'} Lag mode. Either average across all lags between each pair or only at the lag of maximum absolute causal effect. exclude_k : bool, optional (default: True) Whether to exclude causal effects through the variable itself at previous lags. exclude_self_effects : bool, optional (default: True) Whether to exclude causal self effects of variables on themselves. Returns ------- amce : float Average Mediated Causal Effect. """ all_but_k = np.ones(self.N, dtype='bool') if exclude_k: all_but_k[k] = False N_new = self.N - 1 else: N_new = self.N if exclude_self_effects: weights = np.identity(N_new) == False else: weights = np.ones((N_new, N_new), dtype='bool') if self.tau_max < 2: raise ValueError("Mediation only nonzero for tau_max >= 2") all_mce = self.psi[2:, :, :] - self.all_psi_k[k, 2:, :, :] # all_mce[:, range(self.N), range(self.N)] = 0. if lag_mode == 'absmax': return np.average(np.abs( all_mce[:, all_but_k, :][:, :, all_but_k]).max(axis=0), weights=weights) elif lag_mode == 'all_lags': return np.abs(all_mce[:, all_but_k, :][:, :, all_but_k]).mean() else: raise ValueError("lag_mode = %s not implemented" % lag_mode)
def __init__(self, X, Y, a, b, algo, k): """ X : (number_points_1, dimension) matrix of atoms for the first measure Y : (number_points_2, dimension) matrix of atoms for the second measure a : (number_points_1,) vector of weights for the first measure b : (number_points_2,) vector of weights for the second measure algo : algorithm to compute the SRW distance (instance of class 'ProjectedGradientAscent' or 'FrankWolfe') k : dimension parameter (can be of type 'int', 'list' or 'set' in order to compute SRW for several paremeters 'k'). """ # Check shapes d = X.shape[1] n = X.shape[0] m = Y.shape[0] assert d == Y.shape[1] assert n == a.shape[0] assert m == b.shape[0] if isinstance(k, int): assert k <= d assert k == int(k) assert 1 <= k elif isinstance(k, list) or isinstance(k, set): assert len(k) > 0 k = list(set(k)) k.sort(reverse=True) assert k[0] <= d assert k[-1] >= 1 for l in k: assert l == int(l) else: raise TypeError( "Parameter 'k' should be of type 'int' or 'list' or 'set'.") # Measures if algo.use_gpu: self.X = cp.asarray(X) self.Y = cp.asarray(Y) self.a = cp.asarray(a) self.b = cp.asarray(b) else: self.X = X self.Y = Y self.a = a self.b = b self.d = d # Algorithm self.algo = algo self.k = k if self.algo.use_gpu: self.Omega = cp.identity(self.d) else: self.Omega = np.identity(self.d) self.pi = None self.maxmin_values = [] self.minmax_values = []
def _finalize(self): epsilon = self.transitions.get('epsilon', None) if epsilon is not None: epsilon = epsilon + cupy.identity(epsilon.shape[0], dtype=bool) for i in range(math.ceil(math.log(epsilon.shape[0], 2))): epsilon = cupy.matmul(epsilon, epsilon) for key in self.transitions: self.transitions[key] = cupy.matmul(epsilon, self.transitions[key]) self.transitions['epsilon'] = epsilon return self
def test(use_cupy=False): mat_size = 10000 if use_cupy: rand_mat = cupy.random.randn(mat_size, mat_size) ret = cupy.identity(mat_size) else: rand_mat = numpy.random.randn(mat_size, mat_size) ret = numpy.identity(mat_size) start = time.time() for i in range(2): ret = ret.dot(rand_mat) elapsed_time = time.time() - start print("elapsed_time:{0}".format(elapsed_time)) + "[sec]"
def update(self, z, R=None, H=None): """ Add a new measurement (z) to the Kalman filter. Parameters ---------- z : array(points, dim_z, 1) measurement for this update. z can be a scalar if dim_z is 1, otherwise it must be convertible to a column vector. If you pass in a value of H, z must be a column vector the of the correct size. R : array(points, dim_z, dim_z), scalar, or None Optionally provide R to override the measurement noise for this one call, otherwise self.R will be used. H : array(points, dim_z, dim_x), or None Optionally provide H to override the measurement function for this one call, otherwise self.H will be used. """ if z is None: return if R is None: R = self.R elif cp.isscalar(R): R = cp.repeat( (cp.identity(self.dim_z, dtype=self.x.dtype) * R)[cp.newaxis, :, :], self.points, axis=0, ) else: R = cp.asarray(R) if H is None: H = self.H else: H = cp.asarray(H) z = cp.asarray(z) self.update_kernel( self.x, z, H, self.P, R, )
def __init__(self, n, m, l, xStart, P, A, B, H, R, Q): #Parameters self.n = n self.m = m self.l = l #State self.xHat = xStart self.P = P.reshape(n, n) #System Matrices self.A = A.reshape(n, n) self.B = B.reshape(n, l) self.H = H.reshape(m, n) self.R = R.reshape(m, m) self.Q = Q.reshape(n, n) self.I = identity(n)
def Fisher(self, key, label, batchsize, nb_class, convert_dim, dimension): label = cp.array(label) #label = cp.reshape(cp.array(label),[nb_class,batchsize]) #key = F.reshape(key,[nb_class,batchsize,-1]) if (self.n_shot == 1): Sw = cp.identity(dimension, dtype='float32') else: Sw = self.local_cov_in_class(key.data, label, nb_class, batchsize) Sb = self.local_cov_bet_class(key.data, label, nb_class, batchsize) Sw_inv = cp.linalg.inv(Sw) Sw_inv_Sb = cp.matmul(Sw_inv, Sb) lam, v = cp.linalg.eigh(Sw_inv_Sb) eigen_id = cp.argsort(lam)[::-1] eigen_value = lam[eigen_id] eigen_vector = v[:, eigen_id] W = eigen_vector[:, :convert_dim] W = cp.reshape(W, [dimension, convert_dim]) W = F.transpose(W) return W
def identity(size, requires_grad=False, device='cpu', **kwargs): """Create an identity matrix. Args: size (int): size of the matrix. requires_grad (bool): if ``True`` will track gradients. device (str): name of the device where the tensor is located. Default to ``'cpu'``. Returns: Tensor """ if device == 'cpu': data = np.identity(size, **kwargs) else: data = cp.identity(size, **kwargs) return nets.Tensor(data, requires_grad=requires_grad, device=device, **kwargs)
def matrix_power(M, n): """Raise a square matrix to the (integer) power `n`. Args: M (~cupy.ndarray): Matrix to raise by power n. n (~int): Power to raise matrix to. Returns: ~cupy.ndarray: Output array. .. note:: M must be of dtype `float32` or `float64`. ..seealso:: :func:`numpy.linalg.matrix_power` """ if M.ndim != 2 or M.shape[0] != M.shape[1]: raise ValueError("input must be a square array") if not isinstance(n, six.integer_types): raise TypeError("exponent must be an integer") if n == 0: return cupy.identity(M.shape[0], dtype=M.dtype) elif n < 0: M = inv(M) n *= -1 # short-cuts if n <= 3: if n == 1: return M elif n == 2: return cupy.matmul(M, M) else: return cupy.matmul(cupy.matmul(M, M), M) # binary decomposition to reduce the number of Matrix # multiplications for n > 3. result, Z = None, None for b in cupy.binary_repr(n)[::-1]: Z = M if Z is None else cupy.matmul(Z, Z) if b == '1': result = Z if result is None else cupy.matmul(result, Z) return result
def _travel(self): """ Increases probability of infection based on how many infected people have come to the given cities """ if self.migration_matrix is None: raise ValueError('OD matrix must be set first') migration_matrix = cp.random.poisson(self.migration_matrix) migration_matrix = cp.diag(migration_matrix) - cp.identity( len(migration_matrix)) incoming_infected = cp.matmul( migration_matrix, self._city_infected_counts / self._city_population_sizes) incoming_total = migration_matrix.sum(axis=0) self.city_infection_probs += incoming_infected / (incoming_total + self._city_population_sizes) * \ self._virus.transmission_probability
def _get_phi(self, coeffs): """Returns the linear coefficient matrices for different lags.different Parameters ---------- coeffs : dictionary Dictionary of coefficients for each parent. Returns ------- phi : array-like, shape (tau_max + 1, N, N) Matrices of coefficients for each time lag. """ phi = np.zeros((self.tau_max + 1, self.N, self.N)) phi[0] = np.identity(self.N) for j in list(coeffs): for par in list(coeffs[j]): i, tau = par phi[abs(tau), j, i] = coeffs[j][par] return phi
def _get_psi(self, phi): """Returns the linear causal effect matrices for different lags. Parameters ---------- phi : array-like Coefficient matrices at different lags. Returns ------- psi : array-like, shape (tau_max + 1, N, N) Matrices of causal effects for each time lag. """ psi = np.zeros((self.tau_max + 1, self.N, self.N)) psi[0] = np.identity(self.N) for n in range(1, self.tau_max + 1): psi[n] = np.zeros((self.N, self.N)) for s in range(1, n + 1): psi[n] += np.dot(phi[s], psi[n - s]) return psi
import gzip import cupy as cp import numpy as np import csv import time start = time.time() with gzip.open("mnist/train-labels-idx1-ubyte.gz", "rb") as file: label_data = file.read() magic = int.from_bytes(label_data[0:4], byteorder='big') num_of_data = int.from_bytes(label_data[4:8], byteorder='big') offset = 8 label = [int(s) for s in label_data[offset:]] t_train = cp.identity(10)[label] # too slow # for i in range(0, num_of_data): # t_train [i] = cp.zeros(10,dtype="int8") # t_train [i][label_data[offset+i]] = 1 print("shape of train-labels-idx1-ubyte: {}".format(t_train.shape)) with gzip.open("mnist/train-images-idx3-ubyte.gz", "rb") as file: dataset = file.read() magic = int.from_bytes(dataset[0:4], byteorder='big') num_of_data = int.from_bytes(dataset[4:8], byteorder='big') num_of_rows = int.from_bytes(dataset[8:12], byteorder='big') num_of_cols = int.from_bytes(dataset[12:16], byteorder='big') data_size = num_of_rows * num_of_cols x_train_tmp = [] offset = 16 dataset_tmp = [ int(s) for s in dataset[offset:offset + data_size * num_of_data]
from chainer.training import extensions import PIL import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt # Load the MNIST dataset train, test = chainer.datasets.get_mnist() x_train, t_train = train._datasets x_test, t_test = test._datasets x_train = cp.asarray(x_train) x_test = cp.asarray(x_test) t_train = cp.identity(10)[t_train.astype(int)] t_test = cp.identity(10)[t_test.astype(int)] def cross_entropy_error(y, t): if y.ndim == 1: t = t.reshape(1, t.size) y = y.reshape(1, y.size) if t.size == y.size: t = t.argmax(axis=1) batch_size = y.shape[0] return -cp.sum(cp.log(y[cp.arange(batch_size), t] + 1e-7)) / batch_size
def loss_orth(self): _W = self.W.reshape(self.W.shape[0], -1) return F.sum( (F.matmul(_W, _W.T) - cupy.identity(self.out_channels))**2)
def qr(a, mode='reduced'): """QR decomposition. Decompose a given two-dimensional matrix into ``Q * R``, where ``Q`` is an orthonormal and ``R`` is an upper-triangular matrix. Args: a (cupy.ndarray): The input matrix. mode (str): The mode of decomposition. Currently 'reduced', 'complete', 'r', and 'raw' modes are supported. The default mode is 'reduced', in which matrix ``A = (M, N)`` is decomposed into ``Q``, ``R`` with dimensions ``(M, K)``, ``(K, N)``, where ``K = min(M, N)``. Returns: cupy.ndarray, or tuple of ndarray: Although the type of returned object depends on the mode, it returns a tuple of ``(Q, R)`` by default. For details, please see the document of :func:`numpy.linalg.qr`. .. warning:: This function calls one or more cuSOLVER routine(s) which may yield invalid results if input conditions are not met. To detect these invalid results, you can set the `linalg` configuration to a value that is not `ignore` in :func:`cupyx.errstate` or :func:`cupyx.seterr`. .. seealso:: :func:`numpy.linalg.qr` """ # TODO(Saito): Current implementation only accepts two-dimensional arrays _util._assert_cupy_array(a) _util._assert_rank2(a) if mode not in ('reduced', 'complete', 'r', 'raw'): if mode in ('f', 'full', 'e', 'economic'): msg = 'The deprecated mode \'{}\' is not supported'.format(mode) raise ValueError(msg) else: raise ValueError('Unrecognized mode \'{}\''.format(mode)) # support float32, float64, complex64, and complex128 if a.dtype.char in 'fdFD': dtype = a.dtype.char else: dtype = numpy.promote_types(a.dtype.char, 'f').char m, n = a.shape mn = min(m, n) if mn == 0: if mode == 'reduced': return cupy.empty((m, 0), dtype), cupy.empty((0, n), dtype) elif mode == 'complete': return cupy.identity(m, dtype), cupy.empty((m, n), dtype) elif mode == 'r': return cupy.empty((0, n), dtype) else: # mode == 'raw' # compatibility with numpy.linalg.qr dtype = numpy.promote_types(dtype, 'd') return cupy.empty((n, m), dtype), cupy.empty((0, ), dtype) x = a.transpose().astype(dtype, order='C', copy=True) handle = device.get_cusolver_handle() dev_info = cupy.empty(1, dtype=numpy.int32) if dtype == 'f': geqrf_bufferSize = cusolver.sgeqrf_bufferSize geqrf = cusolver.sgeqrf elif dtype == 'd': geqrf_bufferSize = cusolver.dgeqrf_bufferSize geqrf = cusolver.dgeqrf elif dtype == 'F': geqrf_bufferSize = cusolver.cgeqrf_bufferSize geqrf = cusolver.cgeqrf elif dtype == 'D': geqrf_bufferSize = cusolver.zgeqrf_bufferSize geqrf = cusolver.zgeqrf else: msg = ('dtype must be float32, float64, complex64 or complex128' ' (actual: {})'.format(a.dtype)) raise ValueError(msg) # compute working space of geqrf and solve R buffersize = geqrf_bufferSize(handle, m, n, x.data.ptr, n) workspace = cupy.empty(buffersize, dtype=dtype) tau = cupy.empty(mn, dtype=dtype) geqrf(handle, m, n, x.data.ptr, m, tau.data.ptr, workspace.data.ptr, buffersize, dev_info.data.ptr) cupy.linalg._util._check_cusolver_dev_info_if_synchronization_allowed( geqrf, dev_info) if mode == 'r': r = x[:, :mn].transpose() return _util._triu(r) if mode == 'raw': if a.dtype.char == 'f': # The original numpy.linalg.qr returns float64 in raw mode, # whereas the cusolver returns float32. We agree that the # following code would be inappropriate, however, in this time # we explicitly convert them to float64 for compatibility. return x.astype(numpy.float64), tau.astype(numpy.float64) elif a.dtype.char == 'F': # The same applies to complex64 return x.astype(numpy.complex128), tau.astype(numpy.complex128) return x, tau if mode == 'complete' and m > n: mc = m q = cupy.empty((m, m), dtype) else: mc = mn q = cupy.empty((n, m), dtype) q[:n] = x # compute working space of orgqr and solve Q if dtype == 'f': orgqr_bufferSize = cusolver.sorgqr_bufferSize orgqr = cusolver.sorgqr elif dtype == 'd': orgqr_bufferSize = cusolver.dorgqr_bufferSize orgqr = cusolver.dorgqr elif dtype == 'F': orgqr_bufferSize = cusolver.cungqr_bufferSize orgqr = cusolver.cungqr elif dtype == 'D': orgqr_bufferSize = cusolver.zungqr_bufferSize orgqr = cusolver.zungqr buffersize = orgqr_bufferSize(handle, m, mc, mn, q.data.ptr, m, tau.data.ptr) workspace = cupy.empty(buffersize, dtype=dtype) orgqr(handle, m, mc, mn, q.data.ptr, m, tau.data.ptr, workspace.data.ptr, buffersize, dev_info.data.ptr) cupy.linalg._util._check_cusolver_dev_info_if_synchronization_allowed( orgqr, dev_info) q = q[:mc].transpose() r = x[:, :mc].transpose() return q, _util._triu(r)
def time_identity_3000(self): np.identity(3000)
def time_identity_100(self): np.identity(100)
def calc_J(n): return (cp.identity(n) - 1 / n) / n**.5
def rotate(input, angle, axes=(1, 0), reshape=True, output=None, order=None, mode='constant', cval=0.0, prefilter=True): """Rotate an array. The array is rotated in the plane defined by the two axes given by the ``axes`` parameter using spline interpolation of the requested order. Args: input (cupy.ndarray): The input array. angle (float): The rotation angle in degrees. axes (tuple of 2 ints): The two axes that define the plane of rotation. Default is the first two axes. reshape (bool): If ``reshape`` is True, the output shape is adapted so that the input array is contained completely in the output. Default is True. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation. If it is not given, order 1 is used. It is different from :mod:`scipy.ndimage` and can change in the future. Currently it supports only order 0 and 1. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'`` or ``'opencv'``). Default is ``'constant'``. cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray or None: The rotated input. .. seealso:: :func:`scipy.ndimage.rotate` """ _check_parameter('rotate', order, mode) if mode == 'opencv': mode = '_opencv_edge' axes = list(axes) if axes[0] < 0: axes[0] += input.ndim if axes[1] < 0: axes[1] += input.ndim if axes[0] > axes[1]: axes = axes[1], axes[0] if axes[0] < 0 or input.ndim <= axes[1]: raise IndexError rad = cupy.deg2rad(angle) sin = math.sin(rad) cos = math.cos(rad) matrix = cupy.identity(input.ndim) matrix[axes[0], axes[0]] = cos matrix[axes[0], axes[1]] = sin matrix[axes[1], axes[0]] = -sin matrix[axes[1], axes[1]] = cos iy = input.shape[axes[0]] ix = input.shape[axes[1]] if reshape: mtrx = cupy.array([[cos, sin], [-sin, cos]]) minc = [0, 0] maxc = [0, 0] coor = cupy.dot(mtrx, cupy.array([0, ix])) minc, maxc = _minmax(coor, minc, maxc) coor = cupy.dot(mtrx, cupy.array([iy, 0])) minc, maxc = _minmax(coor, minc, maxc) coor = cupy.dot(mtrx, cupy.array([iy, ix])) minc, maxc = _minmax(coor, minc, maxc) oy = int(maxc[0] - minc[0] + 0.5) ox = int(maxc[1] - minc[1] + 0.5) else: oy = input.shape[axes[0]] ox = input.shape[axes[1]] offset = cupy.zeros(input.ndim) offset[axes[0]] = oy / 2.0 - 0.5 offset[axes[1]] = ox / 2.0 - 0.5 offset = cupy.dot(matrix, offset) tmp = cupy.zeros(input.ndim) tmp[axes[0]] = iy / 2.0 - 0.5 tmp[axes[1]] = ix / 2.0 - 0.5 offset = tmp - offset output_shape = list(input.shape) output_shape[axes[0]] = oy output_shape[axes[1]] = ox return affine_transform(input, matrix, offset, output_shape, output, order, mode, cval, prefilter)
class Renderer: OPEN_FILE_SUCCESS = 0 OPEN_FILE_NOT_EXIST = 1 OPEN_FILE_IO_ERROR = 2 OPEN_FILE_SYNTAX_NOT_SUPPORT = 3 PROJECTION_PERSPECTIVE = 0 PROJECTION_RECTANGULAR = 1 ZOOM_METHOD_IN = 0 ZOOM_METHOD_OUT = 1 im = cp.identity(3, cp.float32) em = cp.zeros((3, 4), cp.float32) render_grid = True render_face = True render_method = PROJECTION_PERSPECTIVE def __init__(self, parent, shader_resolution): self.parent = parent self.shader_resolution = shader_resolution # init camera configs self.im[0, 0] = 200.0 self.im[1, 1] = 200.0 self.im[0, 2] = shader_resolution[0] / 2 self.im[1, 2] = shader_resolution[1] / 2 self.em[2, 3] = -15.0 self.em[0, 0] = 1.0 self.em[1, 1] = 1.0 self.em[2, 2] = 1.0 self.models = [] def open_obj_file(self, filename) -> int: if not os.path.exists(filename): return self.OPEN_FILE_NOT_EXIST try: self.models.append(Model(filename)) except IOError: return self.OPEN_FILE_IO_ERROR except SyntaxError: return self.OPEN_FILE_SYNTAX_NOT_SUPPORT return self.OPEN_FILE_SUCCESS def update_render(self) -> QPixmap: # TODO img = cp.zeros( (self.shader_resolution[1], self.shader_resolution[0], 3), cp.uint8) v_color = cp.array([255, 255, 255], cp.uint8) if self.render_method == self.PROJECTION_PERSPECTIVE: for model in self.models: v_extend = cp.concatenate( (model.v, cp.ones((model.v_size, 1), cp.float32)), axis=1) v_im = cp.transpose( cp.matmul(self.im, cp.matmul(self.em, cp.transpose(v_extend)))) v_im = v_im / v_im[:, 2:] v_im = v_im[:, :2] v_im = v_im.astype(cp.int32) for i in range(len(v_im)): if 0 <= v_im[i, 0] <= self.shader_resolution[0] and 0 <= v_im[ i, 1] <= self.shader_resolution[1]: img[v_im[i, 1], v_im[i, 0], :] = v_color img = cp.asnumpy(img) pixmap = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_RGB888) pixmap = QPixmap(pixmap) return pixmap def zoom(self, method: ZOOM_METHOD_IN, factor=20): assert factor > 0 if (10 + factor) < self.im[0, 0] < 400: if method == self.ZOOM_METHOD_OUT: factor = -factor self.im[0, 0] += factor self.im[1, 1] += factor
def qr(a, mode='reduced'): """QR decomposition. Decompose a given two-dimensional matrix into ``Q * R``, where ``Q`` is an orthonormal and ``R`` is an upper-triangular matrix. Args: a (cupy.ndarray): The input matrix. mode (str): The mode of decomposition. Currently 'reduced', 'complete', 'r', and 'raw' modes are supported. The default mode is 'reduced', in which matrix ``A = (..., M, N)`` is decomposed into ``Q``, ``R`` with dimensions ``(..., M, K)``, ``(..., K, N)``, where ``K = min(M, N)``. Returns: cupy.ndarray, or tuple of ndarray: Although the type of returned object depends on the mode, it returns a tuple of ``(Q, R)`` by default. For details, please see the document of :func:`numpy.linalg.qr`. .. warning:: This function calls one or more cuSOLVER routine(s) which may yield invalid results if input conditions are not met. To detect these invalid results, you can set the `linalg` configuration to a value that is not `ignore` in :func:`cupyx.errstate` or :func:`cupyx.seterr`. .. seealso:: :func:`numpy.linalg.qr` """ _util._assert_cupy_array(a) if mode not in ('reduced', 'complete', 'r', 'raw'): if mode in ('f', 'full', 'e', 'economic'): msg = 'The deprecated mode \'{}\' is not supported'.format(mode) else: msg = 'Unrecognized mode \'{}\''.format(mode) raise ValueError(msg) if a.ndim > 2: return _qr_batched(a, mode) # support float32, float64, complex64, and complex128 dtype, out_dtype = _util.linalg_common_type(a) m, n = a.shape k = min(m, n) if k == 0: if mode == 'reduced': return cupy.empty((m, 0), out_dtype), cupy.empty((0, n), out_dtype) elif mode == 'complete': return cupy.identity(m, out_dtype), cupy.empty((m, n), out_dtype) elif mode == 'r': return cupy.empty((0, n), out_dtype) else: # mode == 'raw' return cupy.empty((n, m), out_dtype), cupy.empty((0,), out_dtype) x = a.transpose().astype(dtype, order='C', copy=True) handle = device.get_cusolver_handle() dev_info = cupy.empty(1, dtype=numpy.int32) if dtype == 'f': geqrf_bufferSize = cusolver.sgeqrf_bufferSize geqrf = cusolver.sgeqrf elif dtype == 'd': geqrf_bufferSize = cusolver.dgeqrf_bufferSize geqrf = cusolver.dgeqrf elif dtype == 'F': geqrf_bufferSize = cusolver.cgeqrf_bufferSize geqrf = cusolver.cgeqrf elif dtype == 'D': geqrf_bufferSize = cusolver.zgeqrf_bufferSize geqrf = cusolver.zgeqrf else: msg = ('dtype must be float32, float64, complex64 or complex128' ' (actual: {})'.format(a.dtype)) raise ValueError(msg) # compute working space of geqrf and solve R buffersize = geqrf_bufferSize(handle, m, n, x.data.ptr, n) workspace = cupy.empty(buffersize, dtype=dtype) tau = cupy.empty(k, dtype=dtype) geqrf(handle, m, n, x.data.ptr, m, tau.data.ptr, workspace.data.ptr, buffersize, dev_info.data.ptr) cupy.linalg._util._check_cusolver_dev_info_if_synchronization_allowed( geqrf, dev_info) if mode == 'r': r = x[:, :k].transpose() return _util._triu(r).astype(out_dtype, copy=False) if mode == 'raw': return ( x.astype(out_dtype, copy=False), tau.astype(out_dtype, copy=False)) if mode == 'complete' and m > n: mc = m q = cupy.empty((m, m), dtype) else: mc = k q = cupy.empty((n, m), dtype) q[:n] = x # compute working space of orgqr and solve Q if dtype == 'f': orgqr_bufferSize = cusolver.sorgqr_bufferSize orgqr = cusolver.sorgqr elif dtype == 'd': orgqr_bufferSize = cusolver.dorgqr_bufferSize orgqr = cusolver.dorgqr elif dtype == 'F': orgqr_bufferSize = cusolver.cungqr_bufferSize orgqr = cusolver.cungqr elif dtype == 'D': orgqr_bufferSize = cusolver.zungqr_bufferSize orgqr = cusolver.zungqr buffersize = orgqr_bufferSize( handle, m, mc, k, q.data.ptr, m, tau.data.ptr) workspace = cupy.empty(buffersize, dtype=dtype) orgqr( handle, m, mc, k, q.data.ptr, m, tau.data.ptr, workspace.data.ptr, buffersize, dev_info.data.ptr) cupy.linalg._util._check_cusolver_dev_info_if_synchronization_allowed( orgqr, dev_info) q = q[:mc].transpose() r = x[:, :mc].transpose() return ( q.astype(out_dtype, copy=False), _util._triu(r).astype(out_dtype, copy=False))