Python autograd.numpy.all() Examples
The following are 30
code examples of autograd.numpy.all().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
autograd.numpy
, or try the search function
.
Example #1
Source File: test_correctness.py From pymoo with Apache License 2.0 | 6 votes |
def test_problems(self): for entry in problems: name, params = entry print("Testing: " + name) X, F, CV = load(name) if F is None: print("Warning: No correctness check for %s" % name) continue problem = get_problem(name, *params) _F, _G, _CV, _dF, _dG = problem.evaluate(X, return_values_of=["F", "G", "CV", "dF", "dG"]) if problem.n_obj == 1: F = F[:, None] self.assertTrue(anp.all(anp.abs(_F - F) < 0.00001)) if problem.n_constr > 0: self.assertTrue(anp.all(anp.abs(_CV[:, 0] - CV) < 0.0001))
Example #2
Source File: util.py From kernel-gof with MIT License | 6 votes |
def fit_gaussian_draw(X, J, seed=28, reg=1e-7, eig_pow=1.0): """ Fit a multivariate normal to the data X (n x d) and draw J points from the fit. - reg: regularizer to use with the covariance matrix - eig_pow: raise eigenvalues of the covariance matrix to this power to construct a new covariance matrix before drawing samples. Useful to shrink the spread of the variance. """ with NumpySeedContext(seed=seed): d = X.shape[1] mean_x = np.mean(X, 0) cov_x = np.cov(X.T) if d==1: cov_x = np.array([[cov_x]]) [evals, evecs] = np.linalg.eig(cov_x) evals = np.maximum(0, np.real(evals)) assert np.all(np.isfinite(evals)) evecs = np.real(evecs) shrunk_cov = evecs.dot(np.diag(evals**eig_pow)).dot(evecs.T) + reg*np.eye(d) V = np.random.multivariate_normal(mean_x, shrunk_cov, J) return V
Example #3
Source File: util.py From kernel-gof with MIT License | 6 votes |
def bound_by_data(Z, Data): """ Determine lower and upper bound for each dimension from the Data, and project Z so that all points in Z live in the bounds. Z: m x d Data: n x d Return a projected Z of size m x d. """ n, d = Z.shape Low = np.min(Data, 0) Up = np.max(Data, 0) LowMat = np.repeat(Low[np.newaxis, :], n, axis=0) UpMat = np.repeat(Up[np.newaxis, :], n, axis=0) Z = np.maximum(LowMat, Z) Z = np.minimum(UpMat, Z) return Z
Example #4
Source File: configurations.py From momi2 with GNU General Public License v3.0 | 6 votes |
def build_full_config_list(sampled_pops, sampled_n, ascertainment_pop=None): sampled_n = np.array(sampled_n) if ascertainment_pop is None: ascertainment_pop = [True] * len(sampled_pops) ascertainment_pop = np.array(ascertainment_pop) ranges = [list(range(n + 1)) for n in sampled_n] config_list = [] for x in it.product(*ranges): x = np.array(x, dtype=int) if not (np.all(x[ascertainment_pop] == 0) or np.all( x[ascertainment_pop] == sampled_n[ascertainment_pop])): config_list.append(x) return build_config_list( sampled_pops, np.array(config_list, dtype=int), sampled_n, ascertainment_pop=ascertainment_pop)
Example #5
Source File: sfs.py From momi2 with GNU General Public License v3.0 | 6 votes |
def _get_subsample_counts(configs, n): subconfigs, weights = [], [] for pop_comb in it.combinations_with_replacement(configs.sampled_pops, n): subsample_n = co.Counter(pop_comb) subsample_n = np.array([subsample_n[pop] for pop in configs.sampled_pops], dtype=int) if np.any(subsample_n > configs.sampled_n): continue for sfs_entry in it.product(*(range(sub_n + 1) for sub_n in subsample_n)): sfs_entry = np.array(sfs_entry, dtype=int) if np.all(sfs_entry == 0) or np.all(sfs_entry == subsample_n): # monomorphic continue sfs_entry = np.transpose([subsample_n - sfs_entry, sfs_entry]) cnt_vec = configs.subsample_probs(sfs_entry) if not np.all(cnt_vec == 0): subconfigs.append(sfs_entry) weights.append(cnt_vec) return np.array(subconfigs), np.array(weights)
Example #6
Source File: compute_sfs.py From momi2 with GNU General Public License v3.0 | 6 votes |
def _expected_sfs(demography, configs, folded, error_matrices): if np.any(configs.sampled_n != demography.sampled_n) or np.any(configs.sampled_pops != demography.sampled_pops): raise ValueError( "configs and demography must have same sampled_n, sampled_pops. Use Demography.copy() or ConfigList.copy() to make a copy with different sampled_n.") vecs, idxs = configs._vecs_and_idxs(folded) if error_matrices is not None: vecs = _apply_error_matrices(vecs, error_matrices) vals = expected_sfs_tensor_prod(vecs, demography) sfs = vals[idxs['idx_2_row']] if folded: sfs = sfs + vals[idxs['folded_2_row']] denom = vals[idxs['denom_idx']] for i in (0, 1): denom = denom - vals[idxs[("corrections_2_denom", i)]] #assert np.all(np.logical_or(vals >= 0.0, np.isclose(vals, 0.0))) return sfs, denom
Example #7
Source File: util.py From momi2 with GNU General Public License v3.0 | 6 votes |
def truncate0(x, axis=None, strict=False, tol=1e-13): '''make sure everything in x is non-negative''' # the maximum along axis maxes = np.maximum(np.amax(x, axis=axis), 1e-300) # the negative part of minimum along axis mins = np.maximum(-np.amin(x, axis=axis), 0.0) # assert the negative numbers are small (relative to maxes) assert np.all(mins <= tol * maxes) if axis is not None: idx = [slice(None)] * x.ndim idx[axis] = np.newaxis mins = mins[idx] maxes = maxes[idx] if strict: # set everything below the tolerance to 0 return set0(x, x < tol * maxes) else: # set everything of same magnitude as most negative number, to 0 return set0(x, x < 2 * mins)
Example #8
Source File: tm.py From autohmm with BSD 2-Clause "Simplified" License | 6 votes |
def _set_transmat(self, transmat_val): if transmat_val is None: transmat = np.tile(1.0 / self.n_components, (self.n_components, self.n_components)) else: transmat_val[np.isnan(transmat_val)] = 0.0 normalize(transmat_val, axis=1) if (np.asarray(transmat_val).shape == (self.n_components, self.n_components)): transmat = np.copy(transmat_val) elif transmat_val.shape[0] == self.n_unique: transmat = self._ntied_transmat(transmat_val) else: raise ValueError("cannot match shape of transmat") if not np.all(np.allclose(np.sum(transmat, axis=1), 1.0)): raise ValueError('Rows of transmat must sum to 1.0') self._log_transmat = np.log(np.asarray(transmat).copy()) underflow_idx = np.isnan(self._log_transmat) self._log_transmat[underflow_idx] = NEGINF
Example #9
Source File: goftest.py From kernel-gof with MIT License | 5 votes |
def ustat_h1_mean_variance(fea_tensor, return_variance=True, use_unbiased=True): """ Compute the mean and variance of the asymptotic normal distribution under H1 of the test statistic. fea_tensor: feature tensor obtained from feature_tensor() return_variance: If false, avoid computing and returning the variance. use_unbiased: If True, use the unbiased version of the mean. Can be negative. Return the mean [and the variance] """ Xi = fea_tensor n, d, J = Xi.shape #print 'Xi' #print Xi #assert np.all(util.is_real_num(Xi)) assert n > 1, 'Need n > 1 to compute the mean of the statistic.' # n x d*J # Tau = Xi.reshape(n, d*J) Tau = np.reshape(Xi, [n, d*J]) if use_unbiased: t1 = np.sum(np.mean(Tau, 0)**2)*(old_div(n,float(n-1))) t2 = old_div(np.sum(np.mean(Tau**2, 0)),float(n-1)) # stat is the mean stat = t1 - t2 else: stat = np.sum(np.mean(Tau, 0)**2) if not return_variance: return stat # compute the variance # mu: d*J vector mu = np.mean(Tau, 0) variance = 4*np.mean(np.dot(Tau, mu)**2) - 4*np.sum(mu**2)**2 return stat, variance
Example #10
Source File: goftest.py From kernel-gof with MIT License | 5 votes |
def feature_tensor(self, X): """ Compute the feature tensor which is n x d x J. The feature tensor can be used to compute the statistic, and the covariance matrix for simulating from the null distribution. X: n x d data numpy array return an n x d x J numpy array """ k = self.k J = self.V.shape[0] n, d = X.shape # n x d matrix of gradients grad_logp = self.p.grad_log(X) #assert np.all(util.is_real_num(grad_logp)) # n x J matrix #print 'V' #print self.V K = k.eval(X, self.V) #assert np.all(util.is_real_num(K)) list_grads = np.array([np.reshape(k.gradX_y(X, v), (1, n, d)) for v in self.V]) stack0 = np.concatenate(list_grads, axis=0) #a numpy array G of size n x d x J such that G[:, :, J] # is the derivative of k(X, V_j) with respect to X. dKdV = np.transpose(stack0, (1, 2, 0)) # n x d x J tensor grad_logp_K = util.outer_rows(grad_logp, K) #print 'grad_logp' #print grad_logp.dtype #print grad_logp #print 'K' #print K Xi = old_div((grad_logp_K + dKdV),np.sqrt(d*J)) #Xi = (grad_logp_K + dKdV) return Xi
Example #11
Source File: test_kernel.py From kernel-gof with MIT License | 5 votes |
def test_basic(self): """ Nothing special. Just test basic things. """ # sample n = 10 d = 3 with util.NumpySeedContext(seed=29): X = np.random.randn(n, d)*3 k = kernel.KGauss(sigma2=1) K = k.eval(X, X) self.assertEqual(K.shape, (n, n)) self.assertTrue(np.all(K >= 0-1e-6)) self.assertTrue(np.all(K <= 1+1e-6), 'K not bounded by 1')
Example #12
Source File: plot.py From kernel-gof with MIT License | 5 votes |
def box_meshgrid(func, xbound, ybound, nx=50, ny=50): """ Form a meshed grid (to be used with a contour plot) on a box specified by xbound, ybound. Evaluate the grid with [func]: (n x 2) -> n. - xbound: a tuple (xmin, xmax) - ybound: a tuple (ymin, ymax) - nx: number of points to evluate in the x direction return XX, YY, ZZ where XX is a 2D nd-array of size nx x ny """ # form a test location grid to try minx, maxx = xbound miny, maxy = ybound loc0_cands = np.linspace(minx, maxx, nx) loc1_cands = np.linspace(miny, maxy, ny) lloc0, lloc1 = np.meshgrid(loc0_cands, loc1_cands) # nd1 x nd0 x 2 loc3d = np.dstack((lloc0, lloc1)) # #candidates x 2 all_loc2s = np.reshape(loc3d, (-1, 2) ) # evaluate the function func_grid = func(all_loc2s) func_grid = np.reshape(func_grid, (ny, nx)) assert lloc0.shape[0] == ny assert lloc0.shape[1] == nx assert np.all(lloc0.shape == lloc1.shape) return lloc0, lloc1, func_grid
Example #13
Source File: goftest.py From kernel-gof with MIT License | 5 votes |
def compute_stat(self, dat, return_ustat_gram=False): """ Compute the V statistic as in Section 2.2 of Chwialkowski et al., 2016. return_ustat_gram: If True, then return the n x n matrix used to compute the statistic (by taking the mean of all the elements) """ X = dat.data() n, d = X.shape k = self.k # n x d matrix of gradients grad_logp = self.p.grad_log(X) # n x n gram_glogp = grad_logp.dot(grad_logp.T) # n x n K = k.eval(X, X) B = np.zeros((n, n)) C = np.zeros((n, n)) for i in range(d): grad_logp_i = grad_logp[:, i] B += k.gradX_Y(X, X, i)*grad_logp_i C += (k.gradY_X(X, X, i).T * grad_logp_i).T H = K*gram_glogp + B + C + k.gradXY_sum(X, X) # V-statistic stat = n*np.mean(H) if return_ustat_gram: return stat, H else: return stat #print 't1: {0}'.format(t1) #print 't2: {0}'.format(t2) #print 't3: {0}'.format(t3) #print 't4: {0}'.format(t4) # end KernelSteinTest
Example #14
Source File: data.py From kernel-gof with MIT License | 5 votes |
def __add__(self, data2): """ Merge the current Data with another one. Create a new Data and create a new copy for all internal variables. """ copy = self.clone() copy2 = data2.clone() nX = np.vstack((copy.X, copy2.X)) return Data(nX) ### end Data class
Example #15
Source File: data.py From kernel-gof with MIT License | 5 votes |
def __init__(self, X): """ :param X: n x d numpy array for dataset X """ self.X = X if not np.all(np.isfinite(X)): print('X:') print(util.fullprint(X)) raise ValueError('Not all elements in X are finite.')
Example #16
Source File: util.py From kernel-gof with MIT License | 5 votes |
def standardize(X): mx = np.mean(X, 0) stdx = np.std(X, axis=0) # Assume standard deviations are not 0 Zx = old_div((X-mx),stdx) assert np.all(np.isfinite(Zx)) return Zx
Example #17
Source File: test_problems_wfg.py From pymoo with Apache License 2.0 | 5 votes |
def test_problems(self): for n_obj, n_var, k in [(2, 6, 4), (3, 6, 4), (10, 20, 18)]: problems = [ get_problem("wfg1", n_var, n_obj, k), get_problem("wfg2", n_var, n_obj, k), get_problem("wfg3", n_var, n_obj, k), get_problem("wfg4", n_var, n_obj, k), get_problem("wfg5", n_var, n_obj, k), get_problem("wfg6", n_var, n_obj, k), get_problem("wfg7", n_var, n_obj, k), get_problem("wfg8", n_var, n_obj, k), get_problem("wfg9", n_var, n_obj, k) ] for problem in problems: name = str(problem.__class__.__name__) print("Testing: " + name + "-" + str(n_obj)) X, F, CV = load(name, n_obj) # other = from_optproblems(problem) # F = np.row_stack([other.objective_function(x) for x in X]) if F is None: print("Warning: No correctness check for %s" % name) continue _F, _G, _CV = problem.evaluate(X, return_values_of=["F", "G", "CV"]) if problem.n_obj == 1: F = F[:, None] self.assertTrue(anp.all(anp.abs(_F - F) < 0.00001)) if problem.n_constr > 0: self.assertTrue(anp.all(anp.abs(_CV[:, 0] - CV) < 0.0001))
Example #18
Source File: test_binary_ops.py From autograd with MIT License | 5 votes |
def test_comparison_grads(): compare_funs = [lambda x, y : np.sum(x < x) + 0.0, lambda x, y : np.sum(x <= y) + 0.0, lambda x, y : np.sum(x > y) + 0.0, lambda x, y : np.sum(x >= y) + 0.0, lambda x, y : np.sum(x == y) + 0.0, lambda x, y : np.sum(x != y) + 0.0] with warnings.catch_warnings(record=True) as w: for arg1, arg2 in arg_pairs(): zeros = (arg1 + arg2) * 0 # get correct shape for fun in compare_funs: assert np.all(grad(fun)(arg1, arg2) == zeros) assert np.all(grad(fun, argnum=1)(arg1, arg2) == zeros)
Example #19
Source File: test_misc.py From autograd with MIT License | 5 votes |
def test_flatten_complex(): val = 1 + 1j flat, unflatten = flatten(val) assert np.all(val == unflatten(flat))
Example #20
Source File: test_misc.py From autograd with MIT License | 5 votes |
def unflatten_tracing(): val = [npr.randn(4), [npr.randn(3,4), 2.5], (), (2.0, [1.0, npr.randn(2)])] vect, unflatten = flatten(val) def f(vect): return unflatten(vect) flatten2, _ = make_vjp(f)(vect) assert np.all(vect == flatten2(val))
Example #21
Source File: test_misc.py From autograd with MIT License | 5 votes |
def test_flatten_dict(): val = {'k': npr.random((4, 4)), 'k2': npr.random((3, 3)), 'k3': 3.0, 'k4': [1.0, 4.0, 7.0, 9.0]} vect, unflatten = flatten(val) val_recovered = unflatten(vect) vect_2, _ = flatten(val_recovered) assert np.all(vect == vect_2)
Example #22
Source File: test_misc.py From autograd with MIT License | 5 votes |
def test_flatten_empty(): val = (npr.randn(4), [npr.randn(3,4), 2.5], (), (2.0, [1.0, npr.randn(2)])) vect, unflatten = flatten(val) val_recovered = unflatten(vect) vect_2, _ = flatten(val_recovered) assert np.all(vect == vect_2)
Example #23
Source File: test_misc.py From autograd with MIT License | 5 votes |
def test_flatten(): r = np.random.randn x = (1.0, r(2,3), [r(1,4), {'x': 2.0, 'y': r(4,2)}]) x_flat, unflatten = flatten(x) assert x_flat.shape == (20,) assert x_flat[0] == 1.0 assert np.all(x_flat == flatten(unflatten(x_flat))[0]) y = (1.0, 2.0, [3.0, {'x': 2.0, 'y': 4.0}]) y_flat, unflatten = flatten(y) assert y_flat.shape == (5,) assert y == unflatten(y_flat)
Example #24
Source File: math_functions.py From momi2 with GNU General Public License v3.0 | 5 votes |
def _apply_error_matrices(vecs, error_matrices): if not all([np.allclose(np.sum(err, axis=0), 1.0) for err in error_matrices]): raise Exception("Columns of error matrix should sum to 1") return [np.dot(v, err) for v, err in zip(vecs, error_matrices)] # inverse of a PSD matrix
Example #25
Source File: math_functions.py From momi2 with GNU General Public License v3.0 | 5 votes |
def symmetric_matrix(arr, n): if len(arr) != n * (n + 1) / 2: raise Exception("Array must have dimensions n*(n+1)/2") ret = np.zeros((n, n)) idx = np.triu_indices(n) ret[idx] = arr ret[tuple(reversed(idx))] = arr assert np.all(ret == ret.T) return ret
Example #26
Source File: math_functions.py From momi2 with GNU General Public License v3.0 | 5 votes |
def expm1d(x, eps=1e-6): x = np.array(x) abs_x = np.abs(x) if x.shape: # FIXME: don't require abs_x to be increasing assert np.all(abs_x[1:] >= abs_x[:-1]) small = abs_x < eps big = ~small return np.concatenate([expm1d_taylor(x[small]), expm1d_naive(x[big])]) elif abs_x < eps: return expm1d_taylor(x) else: return expm1d_naive(x)
Example #27
Source File: math_functions.py From momi2 with GNU General Public License v3.0 | 5 votes |
def transformed_expi(x): abs_x = np.abs(x) ser = abs_x < 1. / 45. nser = np.logical_not(ser) # ret = np.zeros(x.shape) # ret[ser], ret[nser] = transformed_expi_series(x[ser]), transformed_expi_naive(x[nser]))) # return ret # We use np.concatenate to combine. # would be better to use ret[ser] and ret[nser] as commented out above # but array assignment not yet supported by autograd assert np.all(abs_x[:-1] >= abs_x[1:]) return np.concatenate((transformed_expi_naive(x[nser]), transformed_expi_series(x[ser])))
Example #28
Source File: configurations.py From momi2 with GNU General Public License v3.0 | 5 votes |
def __eq__(self, other): conf_arr = self.value try: return np.all(conf_arr == other.value) except AttributeError: return False
Example #29
Source File: configurations.py From momi2 with GNU General Public License v3.0 | 5 votes |
def __init__(self, sampled_pops, conf_arr, sampled_n=None, ascertainment_pop=None): """Use build_config_list() instead of calling this constructor directly""" # If sampled_n=None, ConfigList.sampled_n will be the max number of # observed individuals/alleles per population. self.sampled_pops = tuple(sampled_pops) self.value = conf_arr if ascertainment_pop is None: ascertainment_pop = [True] * len(sampled_pops) self.ascertainment_pop = np.array(ascertainment_pop) self.ascertainment_pop.setflags(write=False) if all(not a for a in self.ascertainment_pop): raise ValueError( "At least one of the populations must be used for " "ascertainment of polymorphic sites") max_n = np.max(np.sum(self.value, axis=2), axis=0) if sampled_n is None: sampled_n = max_n sampled_n = np.array(sampled_n) if np.any(sampled_n < max_n): raise ValueError("config greater than sampled_n") self.sampled_n = sampled_n if not np.sum(sampled_n[self.ascertainment_pop]) >= 2: raise ValueError("The total sample size of the ascertainment " "populations must be >= 2") config_sampled_n = np.sum(self.value, axis=2) self.has_missing_data = np.any(config_sampled_n != self.sampled_n) if np.any(np.sum(self.value[:, self.ascertainment_pop, :], axis=1) == 0): raise ValueError("Monomorphic sites not allowed. In addition, all" " sites must be polymorphic when restricted to" " the ascertainment populations")
Example #30
Source File: sfs.py From momi2 with GNU General Public License v3.0 | 5 votes |
def combine_loci(self): # return copy with all loci combined return self.from_matrix(self.freqs_matrix.sum(axis=1), self.configs, self.folded, self._length)