Python scipy.misc.logsumexp() Examples
The following are 30
code examples of scipy.misc.logsumexp().
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
scipy.misc
, or try the search function
.
Example #1
Source File: rebar.py From object_detection_with_tensorflow with MIT License | 6 votes |
def partial_eval(self, X, n_samples=5): if n_samples < 1000: res, iwae = self.sess.run( (self.lHat, self.iwae), feed_dict={self.x: X, self.n_samples: n_samples}) res = [iwae] + res else: # special case to handle OOM assert n_samples % 100 == 0, "When using large # of samples, it must be divisble by 100" res = [] for i in xrange(int(n_samples/100)): logF, = self.sess.run( (self.logF,), feed_dict={self.x: X, self.n_samples: 100}) res.append(logsumexp(logF, axis=1)) res = [np.mean(logsumexp(res, axis=0) - np.log(n_samples))] return res # Random samplers
Example #2
Source File: markov_switching.py From vnpy_crypto with MIT License | 6 votes |
def _logistic(x): """ Note that this is not a vectorized function """ x = np.array(x) # np.exp(x) / (1 + np.exp(x)) if x.ndim == 0: y = np.reshape(x, (1, 1, 1)) # np.exp(x[i]) / (1 + np.sum(np.exp(x[:]))) elif x.ndim == 1: y = np.reshape(x, (len(x), 1, 1)) # np.exp(x[i,t]) / (1 + np.sum(np.exp(x[:,t]))) elif x.ndim == 2: y = np.reshape(x, (x.shape[0], 1, x.shape[1])) # np.exp(x[i,j,t]) / (1 + np.sum(np.exp(x[:,j,t]))) elif x.ndim == 3: y = x else: raise NotImplementedError tmp = np.c_[np.zeros((y.shape[-1], y.shape[1], 1)), y.T].T evaluated = np.reshape(np.exp(y - logsumexp(tmp, axis=0)), x.shape) return evaluated
Example #3
Source File: test_common.py From Computable with MIT License | 6 votes |
def test_logsumexp_b(): a = np.arange(200) b = np.arange(200, 0, -1) desired = np.log(np.sum(b*np.exp(a))) assert_almost_equal(logsumexp(a, b=b), desired) a = [1000, 1000] b = [1.2, 1.2] desired = 1000 + np.log(2 * 1.2) assert_almost_equal(logsumexp(a, b=b), desired) x = np.array([1e-40] * 100000) b = np.linspace(1, 1000, 1e5) logx = np.log(x) X = np.vstack((x, x)) logX = np.vstack((logx, logx)) B = np.vstack((b, b)) assert_array_almost_equal(np.exp(logsumexp(logX, b=B)), (B * X).sum()) assert_array_almost_equal(np.exp(logsumexp(logX, b=B, axis=0)), (B * X).sum(axis=0)) assert_array_almost_equal(np.exp(logsumexp(logX, b=B, axis=1)), (B * X).sum(axis=1))
Example #4
Source File: test_common.py From Computable with MIT License | 6 votes |
def test_logsumexp(): """Test whether logsumexp() function correctly handles large inputs.""" a = np.arange(200) desired = np.log(np.sum(np.exp(a))) assert_almost_equal(logsumexp(a), desired) # Now test with large numbers b = [1000, 1000] desired = 1000.0 + np.log(2.0) assert_almost_equal(logsumexp(b), desired) n = 1000 b = np.ones(n) * 10000 desired = 10000.0 + np.log(n) assert_almost_equal(logsumexp(b), desired) x = np.array([1e-40] * 1000000) logx = np.log(x) X = np.vstack([x, x]) logX = np.vstack([logx, logx]) assert_array_almost_equal(np.exp(logsumexp(logX)), X.sum()) assert_array_almost_equal(np.exp(logsumexp(logX, axis=0)), X.sum(axis=0)) assert_array_almost_equal(np.exp(logsumexp(logX, axis=1)), X.sum(axis=1))
Example #5
Source File: likelihood.py From bilby with MIT License | 6 votes |
def _create_lookup_table(self): """ Make the lookup table """ logger.info('Building lookup table for distance marginalisation.') self._dist_margd_loglikelihood_array = np.zeros((400, 800)) for ii, optimal_snr_squared_ref in enumerate(self._optimal_snr_squared_ref_array): optimal_snr_squared_array = ( optimal_snr_squared_ref * self._ref_dist ** 2. / self._distance_array ** 2) for jj, d_inner_h_ref in enumerate(self._d_inner_h_ref_array): d_inner_h_array = ( d_inner_h_ref * self._ref_dist / self._distance_array) if self.phase_marginalization: d_inner_h_array =\ self._bessel_function_interped(abs(d_inner_h_array)) self._dist_margd_loglikelihood_array[ii][jj] = \ logsumexp(d_inner_h_array - optimal_snr_squared_array / 2, b=self.distance_prior_array * self._delta_distance) log_norm = logsumexp(0. / self._distance_array, b=self.distance_prior_array * self._delta_distance) self._dist_margd_loglikelihood_array -= log_norm self.cache_lookup_table()
Example #6
Source File: clustered_kde.py From kombine with MIT License | 6 votes |
def _evaluate_point_logpdf(args): """ Evaluate the Gaussian KDE at a given point `p`. This lives outside the KDE method to allow for parallelization using :mod:`multipocessing`. Since :func:`map` only allows single-argument functions, the following arguments to be packed into a single tuple. :param p: The point to evaluate the KDE at. :param data: The `(N, ndim)`-shaped array of data used to construct the KDE. :param cho_factor: A Cholesky decomposition of the kernel covariance matrix. """ point, data, cho_factor = args # Use Cholesky decomposition to avoid direct inversion of covariance matrix diff = data - point tdiff = la.cho_solve(cho_factor, diff.T, check_finite=False).T diff *= tdiff # Work in the log to avoid large numbers return logsumexp(-np.sum(diff, axis=1)/2)
Example #7
Source File: rebar.py From yolo_v2 with Apache License 2.0 | 6 votes |
def partial_eval(self, X, n_samples=5): if n_samples < 1000: res, iwae = self.sess.run( (self.lHat, self.iwae), feed_dict={self.x: X, self.n_samples: n_samples}) res = [iwae] + res else: # special case to handle OOM assert n_samples % 100 == 0, "When using large # of samples, it must be divisble by 100" res = [] for i in xrange(int(n_samples/100)): logF, = self.sess.run( (self.logF,), feed_dict={self.x: X, self.n_samples: 100}) res.append(logsumexp(logF, axis=1)) res = [np.mean(logsumexp(res, axis=0) - np.log(n_samples))] return res # Random samplers
Example #8
Source File: rebar.py From Gun-Detector with Apache License 2.0 | 6 votes |
def partial_eval(self, X, n_samples=5): if n_samples < 1000: res, iwae = self.sess.run( (self.lHat, self.iwae), feed_dict={self.x: X, self.n_samples: n_samples}) res = [iwae] + res else: # special case to handle OOM assert n_samples % 100 == 0, "When using large # of samples, it must be divisble by 100" res = [] for i in xrange(int(n_samples/100)): logF, = self.sess.run( (self.logF,), feed_dict={self.x: X, self.n_samples: 100}) res.append(logsumexp(logF, axis=1)) res = [np.mean(logsumexp(res, axis=0) - np.log(n_samples))] return res # Random samplers
Example #9
Source File: _knn.py From skl-groups with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _alpha_div(omas, Bs, dim, num_q, rhos, nus, clamp=True): N = rhos.shape[0] # the actual main estimate: # rho^(- dim * est alpha) nu^(- dim * est beta) # = (rho / nu) ^ (dim * (1 - alpha)) # do some reshaping trickery to get broadcasting right estimates = np.log(rhos)[:, np.newaxis, :] estimates -= np.log(nus)[:, np.newaxis, :] estimates = estimates * (dim * omas.reshape(1, -1, 1)) estimates = logsumexp(estimates, axis=0) # turn the sum into a mean, multiply by Bs estimates += np.log(Bs / N) # factors based on the sizes: # 1 / [ (n-1)^(est alpha) * m^(est beta) ] = ((n-1) / m) ^ (1 - alpha) estimates += omas * np.log((N - 1) / num_q) np.exp(estimates, out=estimates) if clamp: np.maximum(estimates, 0, out=estimates) return estimates
Example #10
Source File: rebar.py From models with Apache License 2.0 | 6 votes |
def partial_eval(self, X, n_samples=5): if n_samples < 1000: res, iwae = self.sess.run( (self.lHat, self.iwae), feed_dict={self.x: X, self.n_samples: n_samples}) res = [iwae] + res else: # special case to handle OOM assert n_samples % 100 == 0, "When using large # of samples, it must be divisble by 100" res = [] for i in xrange(int(n_samples/100)): logF, = self.sess.run( (self.logF,), feed_dict={self.x: X, self.n_samples: 100}) res.append(logsumexp(logF, axis=1)) res = [np.mean(logsumexp(res, axis=0) - np.log(n_samples))] return res # Random samplers
Example #11
Source File: util.py From language-models with MIT License | 6 votes |
def sample_logits(preds, temperature=1.0): """ Sample an index from a logit vector. :param preds: :param temperature: :return: """ preds = np.asarray(preds).astype('float64') if temperature == 0.0: return np.argmax(preds) preds = preds / temperature preds = preds - logsumexp(preds) choice = np.random.choice(len(preds), 1, p=np.exp(preds)) return choice
Example #12
Source File: markov_switching.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def _logistic(x): """ Note that this is not a vectorized function """ x = np.array(x) # np.exp(x) / (1 + np.exp(x)) if x.ndim == 0: y = np.reshape(x, (1, 1, 1)) # np.exp(x[i]) / (1 + np.sum(np.exp(x[:]))) elif x.ndim == 1: y = np.reshape(x, (len(x), 1, 1)) # np.exp(x[i,t]) / (1 + np.sum(np.exp(x[:,t]))) elif x.ndim == 2: y = np.reshape(x, (x.shape[0], 1, x.shape[1])) # np.exp(x[i,j,t]) / (1 + np.sum(np.exp(x[:,j,t]))) elif x.ndim == 3: y = x else: raise NotImplementedError tmp = np.c_[np.zeros((y.shape[-1], y.shape[1], 1)), y.T].T evaluated = np.reshape(np.exp(y - logsumexp(tmp, axis=0)), x.shape) return evaluated
Example #13
Source File: dynamicsampler.py From dynesty with MIT License | 6 votes |
def n_effective(self): """ Estimate the effective number of posterior samples using the Kish Effective Sample Size (ESS) where `ESS = sum(wts)^2 / sum(wts^2)`. Note that this is `len(wts)` when `wts` are uniform and `1` if there is only one non-zero element in `wts`. """ if len(self.saved_logwt) == 0: # If there are no saved weights, return 0. return 0 else: # Otherwise, compute Kish ESS. logwts = np.array(self.saved_logwt) logneff = logsumexp(logwts) * 2 - logsumexp(logwts * 2) return np.exp(logneff)
Example #14
Source File: sampler.py From dynesty with MIT License | 6 votes |
def n_effective(self): """ Estimate the effective number of posterior samples using the Kish Effective Sample Size (ESS) where `ESS = sum(wts)^2 / sum(wts^2)`. Note that this is `len(wts)` when `wts` are uniform and `1` if there is only one non-zero element in `wts`. """ if (len(self.saved_logwt) == 0) or (np.max(self.saved_logwt) > 0.01 * np.nan_to_num(-np.inf)): # If there are no saved weights, or its -inf return 0. return 0 else: # Otherwise, compute Kish ESS. logwts = np.array(self.saved_logwt) logneff = logsumexp(logwts) * 2 - logsumexp(logwts * 2) return np.exp(logneff)
Example #15
Source File: lds.py From pgmult with MIT License | 6 votes |
def _mc_heldout_log_likelihood(self, X, M=100): # Estimate the held out likelihood using Monte Carlo T, K = X.shape assert K == self.K lls = np.zeros(M) for m in range(M): # Sample latent states from the prior data = self.generate(T=T, keep=False) data["x"] = X lls[m] = self.emission_distn.log_likelihood(data) # Compute the average hll = logsumexp(lls) - np.log(M) # Use bootstrap to compute error bars samples = np.random.choice(lls, size=(100, M), replace=True) hll_samples = logsumexp(samples, axis=1) - np.log(M) std_hll = hll_samples.std() return hll, std_hll
Example #16
Source File: lda.py From pgmult with MIT License | 6 votes |
def _conditional_psi(self, d, t, Lmbda, N, c): nott = np.arange(self.T) != t psi = self.psi[d] omega = self.omega[d] mu = self.theta_prior.mu zetat = logsumexp(psi[nott]) mut_marg = mu[t] - 1./Lmbda[t,t] * Lmbda[t,nott].dot(psi[nott] - mu[nott]) sigmat_marg = 1./Lmbda[t,t] sigmat_cond = 1./(omega[t] + 1./sigmat_marg) # kappa is the mean dot precision, i.e. the sufficient statistic of a Gaussian # therefore we can sum over datapoints kappa = (c[t] - N/2.0).sum() mut_cond = sigmat_cond * (kappa + mut_marg / sigmat_marg + omega[t]*zetat) return mut_cond, sigmat_cond
Example #17
Source File: rebar.py From hands-detection with MIT License | 6 votes |
def partial_eval(self, X, n_samples=5): if n_samples < 1000: res, iwae = self.sess.run( (self.lHat, self.iwae), feed_dict={self.x: X, self.n_samples: n_samples}) res = [iwae] + res else: # special case to handle OOM assert n_samples % 100 == 0, "When using large # of samples, it must be divisble by 100" res = [] for i in xrange(int(n_samples/100)): logF, = self.sess.run( (self.logF,), feed_dict={self.x: X, self.n_samples: 100}) res.append(logsumexp(logF, axis=1)) res = [np.mean(logsumexp(res, axis=0) - np.log(n_samples))] return res # Random samplers
Example #18
Source File: rebar.py From object_detection_kitti with Apache License 2.0 | 6 votes |
def partial_eval(self, X, n_samples=5): if n_samples < 1000: res, iwae = self.sess.run( (self.lHat, self.iwae), feed_dict={self.x: X, self.n_samples: n_samples}) res = [iwae] + res else: # special case to handle OOM assert n_samples % 100 == 0, "When using large # of samples, it must be divisble by 100" res = [] for i in xrange(int(n_samples/100)): logF, = self.sess.run( (self.logF,), feed_dict={self.x: X, self.n_samples: 100}) res.append(logsumexp(logF, axis=1)) res = [np.mean(logsumexp(res, axis=0) - np.log(n_samples))] return res # Random samplers
Example #19
Source File: exactz.py From NeuralRG with Apache License 2.0 | 6 votes |
def log_z(n, j, beta): return ( -log(2) + 1 / 2 * n**2 * log(2 * sinh(2 * h(j, beta))) + logsumexp([ fsum([ log(2 * cosh(n / 2 * gamma(n, j, beta, 2 * r))) for r in range(n) ]), fsum([ log(2 * sinh(n / 2 * gamma(n, j, beta, 2 * r))) for r in range(n) ]), fsum([ log(2 * cosh(n / 2 * gamma(n, j, beta, 2 * r + 1))) for r in range(n) ]), fsum([ log(2 * sinh(n / 2 * gamma(n, j, beta, 2 * r + 1))) for r in range(n) ]), ]))
Example #20
Source File: rebar.py From g-tensorflow-models with Apache License 2.0 | 6 votes |
def partial_eval(self, X, n_samples=5): if n_samples < 1000: res, iwae = self.sess.run( (self.lHat, self.iwae), feed_dict={self.x: X, self.n_samples: n_samples}) res = [iwae] + res else: # special case to handle OOM assert n_samples % 100 == 0, "When using large # of samples, it must be divisble by 100" res = [] for i in xrange(int(n_samples/100)): logF, = self.sess.run( (self.logF,), feed_dict={self.x: X, self.n_samples: 100}) res.append(logsumexp(logF, axis=1)) res = [np.mean(logsumexp(res, axis=0) - np.log(n_samples))] return res # Random samplers
Example #21
Source File: rebar.py From multilabel-image-classification-tensorflow with MIT License | 6 votes |
def partial_eval(self, X, n_samples=5): if n_samples < 1000: res, iwae = self.sess.run( (self.lHat, self.iwae), feed_dict={self.x: X, self.n_samples: n_samples}) res = [iwae] + res else: # special case to handle OOM assert n_samples % 100 == 0, "When using large # of samples, it must be divisble by 100" res = [] for i in xrange(int(n_samples/100)): logF, = self.sess.run( (self.logF,), feed_dict={self.x: X, self.n_samples: 100}) res.append(logsumexp(logF, axis=1)) res = [np.mean(logsumexp(res, axis=0) - np.log(n_samples))] return res # Random samplers
Example #22
Source File: HVAE_2level.py From vae_vampprior with MIT License | 5 votes |
def calculate_likelihood(self, X, dir, mode='test', S=5000, MB=500): # set auxiliary variables for number of training and test sets N_test = X.size(0) # init list likelihood_test = [] if S <= MB: R = 1 else: R = S / MB S = MB for j in range(N_test): if j % 100 == 0: print('{:.2f}%'.format(j / (1. * N_test) * 100)) # Take x* x_single = X[j].unsqueeze(0) a = [] for r in range(0, int(R)): # Repeat it for all training points x = x_single.expand(S, x_single.size(1)) a_tmp, _, _ = self.calculate_loss(x) a.append( -a_tmp.cpu().data.numpy() ) # calculate max a = np.asarray(a) a = np.reshape(a, (a.shape[0] * a.shape[1], 1)) likelihood_x = logsumexp( a ) likelihood_test.append(likelihood_x - np.log(len(a))) likelihood_test = np.array(likelihood_test) plot_histogram(-likelihood_test, dir, mode) return -np.mean(likelihood_test)
Example #23
Source File: lda.py From pgmult with MIT License | 5 votes |
def _conditional_omega(self, d, t): nott = np.arange(self.T) != t psi = self.psi[d] zetat = logsumexp(psi[nott]) return psi[t] - zetat
Example #24
Source File: utils.py From sgnmt with Apache License 2.0 | 5 votes |
def log_sum_log_semiring(vals): """Uses the ``logsumexp`` function in scipy to calculate the log of the sum of a set of log values. Args: vals (set): List or set of numerical values """ return logsumexp(numpy.asarray([val for val in vals]))
Example #25
Source File: parabola.py From cgpm with Apache License 2.0 | 5 votes |
def logpdf(self, rowid, targets, constraints=None, inputs=None): assert targets.keys() == self.outputs assert inputs.keys() == self.inputs assert not constraints x = inputs[self.inputs[0]] y = targets[self.outputs[0]] return logsumexp([ np.log(.5)+self.uniform.logpdf(y-x**2), np.log(.5)+self.uniform.logpdf(-y-x**2) ])
Example #26
Source File: ctm.py From pgmult with MIT License | 5 votes |
def logma(v): def logavg(v): return logsumexp(v) - np.log(len(v)) return np.array([logavg(v[n//2:n]) for n in range(2,len(v))])
Example #27
Source File: lds.py From pgmult with MIT License | 5 votes |
def predictive_log_likelihood(self, X_pred, data_index=0, M=100): """ Hacky way of computing the predictive log likelihood :param X_pred: :param data_index: :param M: :return: """ Tpred = X_pred.shape[0] data = self.data_list[data_index] conditional_mean = self.emission_distn.conditional_mean(data) conditional_cov = self.emission_distn.conditional_cov(data, flat=True) lls = [] z_preds = data["states"].predict_states( conditional_mean, conditional_cov, Tpred=Tpred, Npred=M) for m in range(M): ll_pred = self.emission_distn.log_likelihood( {"z": z_preds[...,m], "x": X_pred}) lls.append(ll_pred) # Compute the average hll = logsumexp(lls) - np.log(M) # Use bootstrap to compute error bars samples = np.random.choice(lls, size=(100, M), replace=True) hll_samples = logsumexp(samples, axis=1) - np.log(M) std_hll = hll_samples.std() return hll, std_hll
Example #28
Source File: utils.py From pgmult with MIT License | 5 votes |
def log_polya_gamma_density(x, b, c, trunc=1000): if np.isscalar(x): xx = np.array([[x]]) else: assert x.ndim == 1 xx = x[:,None] logf = np.zeros(xx.size) logf += b * np.log(np.cosh(c/2.)) logf += (b-1) * np.log(2) - gammaln(b) # Compute the terms in the summation ns = np.arange(trunc)[None,:].astype(np.float) terms = np.zeros_like(ns, dtype=np.float) terms += gammaln(ns+b) - gammaln(ns+1) terms += np.log(2*ns+b) - 0.5 * np.log(2*np.pi) # Compute the terms that depend on x terms = terms - 3./2*np.log(xx) terms += -(2*ns+b)**2 / (8*xx) terms += -c**2/2. * xx # logf += logsumexp(terms, axis=1) maxlogf = np.amax(terms, axis=1)[:,None] logf += np.log(np.exp(terms - maxlogf).dot((-1.0)**ns.T)).ravel() \ + maxlogf.ravel() # terms2 = terms.reshape((xx.shape[0], -1, 2)) # df = -np.diff(np.exp(terms2 - terms2.max(2)[...,None]),axis=2) # terms2 = np.log(df+0j) + terms2.max(2)[...,None] # logf += logsumexp(terms2.reshape((xx.shape[0], -1)), axis=1) # plt.figure() # plt.plot(xx, logf) return logf
Example #29
Source File: test_common.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_logsumexp(): # make sure logsumexp can be imported from either scipy.misc or # scipy.special with suppress_warnings() as sup: sup.filter(DeprecationWarning, "`logsumexp` is deprecated") assert_allclose(logsumexp([0, 1]), sc_logsumexp([0, 1]), atol=1e-16)
Example #30
Source File: infotheo.py From vnpy_crypto with MIT License | 5 votes |
def logsumexp(a, axis=None): """ Compute the log of the sum of exponentials log(e^{a_1}+...e^{a_n}) of a Avoids numerical overflow. Parameters ---------- a : array-like The vector to exponentiate and sum axis : int, optional The axis along which to apply the operation. Defaults is None. Returns ------- sum(log(exp(a))) Notes ----- This function was taken from the mailing list http://mail.scipy.org/pipermail/scipy-user/2009-October/022931.html This should be superceded by the ufunc when it is finished. """ if axis is None: # Use the scipy.maxentropy version. return sp_logsumexp(a) a = np.asarray(a) shp = list(a.shape) shp[axis] = 1 a_max = a.max(axis=axis) s = np.log(np.exp(a - a_max.reshape(shp)).sum(axis=axis)) lse = a_max + s return lse