Python autograd.numpy.transpose() Examples
The following are 22
code examples of autograd.numpy.transpose().
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: standard_models.py From pyhawkes with MIT License | 6 votes |
def G(self): full_W = np.array([node.w for node in self.nodes]) WB = full_W[:,1:].reshape((self.K,self.K, self.B)) # Weight matrix is summed over impulse response functions WT = WB.sum(axis=2) # Impulse response weights are normalized weights GT = WB / WT[:,:,None] # Then we transpose so that the impuolse matrix is (outgoing x incoming x basis) G = np.transpose(GT, [1,0,2]) # TODO: Decide if this is still necessary for k1 in range(self.K): for k2 in range(self.K): if G[k1,k2,:].sum() < 1e-2: G[k1,k2,:] = 1.0/self.B return G
Example #2
Source File: compute_sfs.py From momi2 with GNU General Public License v3.0 | 6 votes |
def _process_event(self, event): e_type = self.demo._event_type(event) if e_type == 'leaf': self._process_leaf_likelihood(event) elif e_type == 'merge_subpops': self._process_merge_subpops_likelihood(event) elif e_type == 'merge_clusters': self._process_merge_clusters_likelihood(event) elif e_type == 'pulse': self._process_pulse_likelihood(event) else: raise Exception("Unrecognized event type.") for newpop in self.demo._parent_pops(event): lik = self._get_likelihoods(newpop) lik.make_last_axis(newpop) n = lik.get_last_axis_n() if n > 0: lik.add_last_axis_sfs(self.demo._truncated_sfs(newpop)) if event != self.demo._event_root: lik.matmul_last_axis( np.transpose(moran_transition( self.demo._scaled_time(newpop), n)))
Example #3
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 #4
Source File: configurations.py From momi2 with GNU General Public License v3.0 | 6 votes |
def build_config_list(sampled_pops, counts, sampled_n=None, ascertainment_pop=None): """ if sampled_n is not None, counts is the derived allele counts if sampled_n is None, counts has an extra trailing axis: counts[...,0] is ancestral allele count, counts[...,1] is derived allele count """ if sampled_n is not None: sampled_n = np.array(sampled_n, dtype=int) counts1 = np.array(counts, dtype=int, ndmin=2) counts0 = sampled_n - counts1 counts = np.array([counts0, counts1], dtype=int) counts = np.transpose(counts, axes=[1, 2, 0]) counts = np.array(counts, ndmin=3, dtype=int) assert counts.shape[1:] == (len(sampled_pops), 2) counts.setflags(write=False) return ConfigList(sampled_pops, counts, sampled_n, ascertainment_pop)
Example #5
Source File: configurations.py From momi2 with GNU General Public License v3.0 | 5 votes |
def _vecs_and_idxs(self, folded): augmented_configs = self._augmented_configs(folded) augmented_idxs = self._augmented_idxs(folded) # construct the vecs vecs = [np.zeros((len(augmented_configs), n + 1)) for n in self.sampled_n] for i in range(len(vecs)): n = self.sampled_n[i] derived = np.einsum( "i,j->ji", np.ones(len(augmented_configs)), np.arange(n + 1)) curr = scipy.stats.hypergeom.pmf( k=augmented_configs[:, i, 1], M=n, n=derived, N=augmented_configs[:, i].sum(1) ) assert not np.any(np.isnan(curr)) vecs[i] = np.transpose(curr) # copy augmented_idxs to make it safe return vecs, dict(augmented_idxs) # def _config_str_iter(self): # for c in self.value: # yield _config2hashable(c)
Example #6
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 #7
Source File: linalg.py From autograd with MIT License | 5 votes |
def _vjp_sylvester(argnums, ans, args, _): a, b, q = args def vjp(g): vjps = [] q_vjp = solve_sylvester(anp.transpose(a), anp.transpose(b), g) if 0 in argnums: vjps.append(-anp.dot(q_vjp, anp.transpose(ans))) if 1 in argnums: vjps.append(-anp.dot(anp.transpose(ans), q_vjp)) if 2 in argnums: vjps.append(q_vjp) return tuple(vjps) return vjp
Example #8
Source File: linalg.py From autograd with MIT License | 5 votes |
def grad_solve_triangular(ans, a, b, trans=0, lower=False, **kwargs): tri = anp.tril if (lower ^ (_flip(a, trans) == 'N')) else anp.triu transpose = lambda x: x if _flip(a, trans) != 'N' else x.T al2d = lambda x: x if x.ndim > 1 else x[...,None] def vjp(g): v = al2d(solve_triangular(a, g, trans=_flip(a, trans), lower=lower)) return -transpose(tri(anp.dot(v, al2d(ans).T))) return vjp
Example #9
Source File: linalg.py From autograd with MIT License | 5 votes |
def _vjp_sqrtm(ans, A, disp=True, blocksize=64): assert disp, "sqrtm vjp not implemented for disp=False" ans_transp = anp.transpose(ans) def vjp(g): return anp.real(solve_sylvester(ans_transp, ans_transp, g)) return vjp
Example #10
Source File: signal.py From autograd with MIT License | 5 votes |
def grad_convolve(argnum, ans, A, B, axes=None, dot_axes=[(),()], mode='full'): assert mode in ['valid', 'full'], "Grad for mode {0} not yet implemented".format(mode) axes, shapes = parse_axes(A.shape, B.shape, axes, dot_axes, mode) if argnum == 0: X, Y = A, B _X_, _Y_ = 'A', 'B' ignore_Y = 'ignore_B' elif argnum == 1: X, Y = B, A _X_, _Y_ = 'B', 'A' ignore_Y = 'ignore_A' else: raise NotImplementedError("Can't take grad of convolve w.r.t. arg {0}".format(argnum)) if mode == 'full': new_mode = 'valid' else: if any([x_size > y_size for x_size, y_size in zip(shapes[_X_]['conv'], shapes[_Y_]['conv'])]): new_mode = 'full' else: new_mode = 'valid' def vjp(g): result = convolve(g, Y[flipped_idxs(Y.ndim, axes[_Y_]['conv'])], axes = [axes['out']['conv'], axes[_Y_]['conv']], dot_axes = [axes['out'][ignore_Y], axes[_Y_]['ignore']], mode = new_mode) new_order = npo.argsort(axes[_X_]['ignore'] + axes[_X_]['dot'] + axes[_X_]['conv']) return np.transpose(result, new_order) return vjp
Example #11
Source File: signal.py From autograd with MIT License | 5 votes |
def convolve(A, B, axes=None, dot_axes=[(),()], mode='full'): assert mode in ['valid', 'full'], "Mode {0} not yet implemented".format(mode) if axes is None: axes = [list(range(A.ndim)), list(range(A.ndim))] wrong_order = any([B.shape[ax_B] < A.shape[ax_A] for ax_A, ax_B in zip(*axes)]) if wrong_order: if mode=='valid' and not all([B.shape[ax_B] <= A.shape[ax_A] for ax_A, ax_B in zip(*axes)]): raise Exception("One array must be larger than the other along all convolved dimensions") elif mode != 'full' or B.size <= A.size: # Tie breaker i1 = B.ndim - len(dot_axes[1]) - len(axes[1]) # B ignore i2 = i1 + A.ndim - len(dot_axes[0]) - len(axes[0]) # A ignore i3 = i2 + len(axes[0]) ignore_B = list(range(i1)) ignore_A = list(range(i1, i2)) conv = list(range(i2, i3)) return convolve(B, A, axes=axes[::-1], dot_axes=dot_axes[::-1], mode=mode).transpose(ignore_A + ignore_B + conv) if mode == 'full': B = pad_to_full(B, A, axes[::-1]) B_view_shape = list(B.shape) B_view_strides = list(B.strides) flipped_idxs = [slice(None)] * A.ndim for ax_A, ax_B in zip(*axes): B_view_shape.append(abs(B.shape[ax_B] - A.shape[ax_A]) + 1) B_view_strides.append(B.strides[ax_B]) B_view_shape[ax_B] = A.shape[ax_A] flipped_idxs[ax_A] = slice(None, None, -1) B_view = as_strided(B, B_view_shape, B_view_strides) A_view = A[tuple(flipped_idxs)] all_axes = [list(axes[i]) + list(dot_axes[i]) for i in [0, 1]] return einsum_tensordot(A_view, B_view, all_axes)
Example #12
Source File: geometry.py From AeroSandbox with MIT License | 5 votes |
def add_control_surface(self, deflection=0., hinge_point=0.75): # Returns a version of the airfoil with a control surface added at a given point. # Inputs: # # deflection: the deflection angle, in degrees. Downwards-positive. # # hinge_point: the location of the hinge, as a fraction of chord. # Make the rotation matrix for the given angle. sintheta = np.sin(np.radians(-deflection)) costheta = np.cos(np.radians(-deflection)) rotation_matrix = np.array( [[costheta, -sintheta], [sintheta, costheta]] ) # Find the hinge point hinge_point = np.array( (hinge_point, self.get_camber_at_chord_fraction(hinge_point))) # Make hinge_point a vector. # Split the airfoil into the sections before and after the hinge split_index = np.where(self.mcl_coordinates[:, 0] > hinge_point[0])[0][0] mcl_coordinates_before = self.mcl_coordinates[:split_index, :] mcl_coordinates_after = self.mcl_coordinates[split_index:, :] upper_minus_mcl_before = self.upper_minus_mcl[:split_index, :] upper_minus_mcl_after = self.upper_minus_mcl[split_index:, :] # Rotate the mean camber line (MCL) and "upper minus mcl" new_mcl_coordinates_after = np.transpose( rotation_matrix @ np.transpose(mcl_coordinates_after - hinge_point)) + hinge_point new_upper_minus_mcl_after = np.transpose(rotation_matrix @ np.transpose(upper_minus_mcl_after)) # Do blending # Assemble airfoil new_mcl_coordinates = np.vstack((mcl_coordinates_before, new_mcl_coordinates_after)) new_upper_minus_mcl = np.vstack((upper_minus_mcl_before, new_upper_minus_mcl_after)) upper_coordinates = np.flipud(new_mcl_coordinates + new_upper_minus_mcl) lower_coordinates = new_mcl_coordinates - new_upper_minus_mcl coordinates = np.vstack((upper_coordinates, lower_coordinates[1:, :])) new_airfoil = Airfoil(name=self.name + " flapped", coordinates=coordinates, repanel=False) return new_airfoil # TODO fix self-intersecting airfoils at high deflections
Example #13
Source File: test_einsum2.py From momi2 with GNU General Public License v3.0 | 5 votes |
def test_grad(): p = .05 def fun0(B, Bdims): return einsum2.einsum2(np.exp(B**2), Bdims, np.transpose(B), Bdims[::-1], []) def fun1(B, Bdims): if Bdims: Bdims = list(range(len(Bdims))) return np.einsum(np.exp(B**2), Bdims, np.transpose(B), Bdims[::-1], []) grad0 = autograd.grad(fun0) grad1 = autograd.grad(fun1) B, Bdims = random_tensor(p) assert np.allclose(grad0(B, Bdims), grad1(B, Bdims))
Example #14
Source File: einsum2.py From momi2 with GNU General Public License v3.0 | 5 votes |
def _transpose(in_arr, in_sublist, out_sublist): if set(in_sublist) != set(out_sublist): raise ValueError("Input and output subscripts don't match") for sublist in (in_sublist, out_sublist): if len(set(sublist)) != len(sublist): raise NotImplementedError("Repeated subscripts not implemented") in_idxs = {k:v for v,k in enumerate(in_sublist)} return np.transpose(in_arr, axes=[in_idxs[s] for s in out_sublist])
Example #15
Source File: compute_sfs.py From momi2 with GNU General Public License v3.0 | 5 votes |
def make_last_axis(self, pop): axis = self.pop_axis(pop) perm = [i for i in range(self.n_axes) if i != axis] + [axis] self.liks = np.transpose(self.liks, perm) self.pop_labels = [p for p in self.pop_labels if p != pop] + [pop] assert len(self.pop_labels) + 1 == len(self.liks.shape)
Example #16
Source File: util.py From momi2 with GNU General Public License v3.0 | 5 votes |
def check_psd(X, **tol_kwargs): X = check_symmetric(X) d, U = np.linalg.eigh(X) d = truncate0(d, **tol_kwargs) ret = np.dot(U, np.dot(np.diag(d), np.transpose(U))) #assert np.allclose(ret, X) return np.array(ret, ndmin=2)
Example #17
Source File: util.py From momi2 with GNU General Public License v3.0 | 5 votes |
def check_symmetric(X): Xt = np.transpose(X) assert np.allclose(X, Xt) return 0.5 * (X + Xt)
Example #18
Source File: standard_models.py From pyhawkes with MIT License | 5 votes |
def W(self): full_W = np.array([node.w for node in self.nodes]) WB = full_W[:,1:].reshape((self.K,self.K, self.B)) # Weight matrix is summed over impulse response functions WT = WB.sum(axis=2) # Then we transpose so that the weight matrix is (outgoing x incoming) W = WT.T return W
Example #19
Source File: confidence_region.py From momi2 with GNU General Public License v3.0 | 4 votes |
def _long_score_cov(params, seg_sites, demo_func, **kwargs): if "mut_rate" in kwargs: raise NotImplementedError( "Currently only implemented for multinomial composite likelihood") params = np.array(params) configs = seg_sites.sfs.configs #_,snp_counts = seg_sites.sfs._idxs_counts(None) snp_counts = seg_sites.sfs._total_freqs weights = snp_counts / float(np.sum(snp_counts)) def snp_log_probs(x): ret = np.log(expected_sfs( demo_func(*x), configs, normalized=True, **kwargs)) return ret - np.sum(weights * ret) # subtract off mean # g_out = sum(autocov(einsum("ij,ik->ikj",jacobian(idx_series), jacobian(idx_series)))) # computed in roundabout way, in case jacobian is slow for many snps # autocovariance is truncated at sqrt(len(idx_series)), to avoid # statistical/numerical issues def g_out_antihess(y): lp = snp_log_probs(y) ret = 0.0 for l in seg_sites._get_likelihood_sequences(lp): L = len(l) lc = make_constant(l) fft = np.fft.fft(l) # (assumes l is REAL) assert np.all(np.imag(l) == 0.0) fft_rev = np.conj(fft) * np.exp(2 * np.pi * 1j * np.arange(L) / float(L)) curr = 0.5 * (fft * fft_rev - fft * make_constant(fft_rev) - make_constant(fft) * fft_rev) curr = np.fft.ifft(curr)[(L - 1)::-1] # make real assert np.allclose(np.imag(curr / L), 0.0) curr = np.real(curr) curr = curr[0] + 2.0 * np.sum(curr[1:int(np.sqrt(L))]) ret = ret + curr return ret g_out = autograd.hessian(g_out_antihess)(params) g_out = 0.5 * (g_out + np.transpose(g_out)) return g_out
Example #20
Source File: parametric_GP.py From ParametricGP with MIT License | 4 votes |
def likelihood(self, hyp): M = self.M Z = self.Z m = self.m S = self.S X_batch = self.X_batch y_batch = self.y_batch jitter = self.jitter jitter_cov = self.jitter_cov N = X_batch.shape[0] logsigma_n = hyp[-1] sigma_n = np.exp(logsigma_n) # Compute K_u_inv K_u = kernel(Z, Z, hyp[:-1]) # K_u_inv = np.linalg.solve(K_u + np.eye(M)*jitter_cov, np.eye(M)) L = np.linalg.cholesky(K_u + np.eye(M)*jitter_cov) K_u_inv = np.linalg.solve(L.T, np.linalg.solve(L,np.eye(M))) self.K_u_inv = K_u_inv # Compute mu psi = kernel(Z, X_batch, hyp[:-1]) K_u_inv_m = np.matmul(K_u_inv,m) MU = np.matmul(psi.T,K_u_inv_m) # Compute cov Alpha = np.matmul(K_u_inv,psi) COV = kernel(X_batch, X_batch, hyp[:-1]) - np.matmul(psi.T, np.matmul(K_u_inv,psi)) + \ np.matmul(Alpha.T, np.matmul(S,Alpha)) COV_inv = np.linalg.solve(COV + np.eye(N)*sigma_n + np.eye(N)*jitter, np.eye(N)) # L = np.linalg.cholesky(COV + np.eye(N)*sigma_n + np.eye(N)*jitter) # COV_inv = np.linalg.solve(np.transpose(L), np.linalg.solve(L,np.eye(N))) # Compute cov(Z, X) cov_ZX = np.matmul(S,Alpha) # Update m and S alpha = np.matmul(COV_inv, cov_ZX.T) m = m + np.matmul(cov_ZX, np.matmul(COV_inv, y_batch-MU)) S = S - np.matmul(cov_ZX, alpha) self.m = m self.S = S # Compute NLML K_u_inv_m = np.matmul(K_u_inv,m) NLML = 0.5*np.matmul(m.T,K_u_inv_m) + np.sum(np.log(np.diag(L))) + 0.5*np.log(2.*np.pi)*M return NLML[0,0]
Example #21
Source File: demography.py From momi2 with GNU General Public License v3.0 | 4 votes |
def admixture_operator(n_node, p): # axis0=n_from_parent, axis1=der_from_parent, axis2=der_in_parent der_in_parent = np.tile(np.arange(n_node + 1), (n_node + 1, n_node + 1, 1)) n_from_parent = np.transpose(der_in_parent, [2, 0, 1]) der_from_parent = np.transpose(der_in_parent, [0, 2, 1]) anc_in_parent = n_node - der_in_parent anc_from_parent = n_from_parent - der_from_parent x = comb(der_in_parent, der_from_parent) * comb( anc_in_parent, anc_from_parent) / comb(n_node, n_from_parent) # rearrange so axis0=1, axis1=der_in_parent, axis2=der_from_parent, axis3=n_from_parent x = np.transpose(x) x = np.reshape(x, [1] + list(x.shape)) n = np.arange(n_node+1) B = comb(n_node, n) # the two arrays to convolve_sum_axes x1 = (x * B * ((1-p)**n) * (p**(n[::-1]))) x2 = x[:, :, :, ::-1] # reduce array size; approximate low probability events with 0 mu = n_node * (1-p) sigma = np.sqrt(n_node * p * (1-p)) n_sd = 4 lower = np.max((0, np.floor(mu - n_sd*sigma))) upper = np.min((n_node, np.ceil(mu + n_sd*sigma))) + 1 lower, upper = int(lower), int(upper) ##x1 = x1[:,:,:upper,lower:upper] ##x2 = x2[:,:,:(n_node-lower+1),lower:upper] ret = convolve_sum_axes(x1, x2) # axis0=der_in_parent1, axis1=der_in_parent2, axis2=der_in_child ret = np.reshape(ret, ret.shape[1:]) if ret.shape[2] < (n_node+1): ret = np.pad(ret, [(0,0),(0,0),(0,n_node+1-ret.shape[2])], "constant") return ret[:, :, :(n_node+1)] #@memoize #def _der_in_admixture_node(n_node): # ''' # returns 4d-array, [n_from_parent1, der_in_child, der_in_parent1, der_in_parent2]. # Used by Demography._admixture_prob_helper # ''' # # axis0=n_from_parent, axis1=der_from_parent, axis2=der_in_parent # der_in_parent = np.tile(np.arange(n_node + 1), (n_node + 1, n_node + 1, 1)) # n_from_parent = np.transpose(der_in_parent, [2, 0, 1]) # der_from_parent = np.transpose(der_in_parent, [0, 2, 1]) # # anc_in_parent = n_node - der_in_parent # anc_from_parent = n_from_parent - der_from_parent # # x = scipy.special.comb(der_in_parent, der_from_parent) * scipy.special.comb( # anc_in_parent, anc_from_parent) / scipy.special.comb(n_node, n_from_parent) # # ret, labels = convolve_axes( # x, x[::-1, ...], [[c for c in 'ijk'], [c for c in 'ilm']], ['j', 'l'], 'n') # return np.einsum('%s->inkm' % ''.join(labels), ret[..., :(n_node + 1)])
Example #22
Source File: compute_sfs.py From momi2 with GNU General Public License v3.0 | 4 votes |
def _process_pulse_likelihood(self, event): parent_pops = self.demo._parent_pops(event) child_pops_events = self.demo._child_pops(event) assert len(child_pops_events) == 2 child_pops, child_events = list(zip(*list(child_pops_events.items()))) recipient, non_recipient, donor, non_donor = self.demo._pulse_nodes(event) assert set(parent_pops) == set([donor, non_donor]) assert set(child_pops) == set([recipient, non_recipient]) if len(set(child_events)) == 2: ## more memory-efficient to do split then join admixture_probs, admixture_idxs = self.demo._admixture_prob(recipient) admixture_probs_dims = [recipient, non_donor, donor] assert set(admixture_probs_dims) == set(admixture_idxs) admixture_probs = np.transpose( admixture_probs, [admixture_idxs.index(i) for i in admixture_probs_dims]) recipient_lik = self._get_likelihoods(recipient) donor_lik = self._get_likelihoods(non_recipient) assert donor_lik is not recipient_lik recipient_lik.make_last_axis(recipient) donor_lik.make_last_axis(non_recipient) recipient_lik.admix_trailing_pop(admixture_probs, donor) self._merge_pops(donor, [donor, non_recipient]) self._rename_pop(recipient, non_donor) else: # in this case, (typically) more memory-efficient to multiply likelihood by transition 4-tensor # (if only 2 populations, and much fewer SFS entries than samples, it may be more efficient to replace -ep with -es,-ej) child_event, = set(child_events) lik = self._get_likelihoods(recipient) assert lik is self._get_likelihoods(non_recipient) pulse_probs, pulse_idxs = self.demo._pulse_prob(event) pulse_probs_dims = [recipient, non_recipient, non_donor, donor] assert set(pulse_probs_dims) == set(pulse_idxs) pulse_probs = np.transpose(pulse_probs, [ pulse_idxs.index(i) for i in pulse_probs_dims]) lik.make_last_axis(recipient) lik.make_last_axis(non_recipient) lik.matmul_last_axis(pulse_probs, axes=2) lik.rename_pop(recipient, non_donor) lik.rename_pop(non_recipient, donor)