当前位置: 首页>>代码示例>>Python>>正文


Python misc.logsumexp方法代码示例

本文整理汇总了Python中scipy.misc.logsumexp方法的典型用法代码示例。如果您正苦于以下问题:Python misc.logsumexp方法的具体用法?Python misc.logsumexp怎么用?Python misc.logsumexp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scipy.misc的用法示例。


在下文中一共展示了misc.logsumexp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _logistic

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:26,代码来源:markov_switching.py

示例2: test_logsumexp

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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)) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:26,代码来源:test_common.py

示例3: test_logsumexp_b

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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)) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:25,代码来源:test_common.py

示例4: partial_eval

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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 
开发者ID:rky0930,项目名称:yolo_v2,代码行数:21,代码来源:rebar.py

示例5: _alpha_div

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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 
开发者ID:djsutherland,项目名称:skl-groups,代码行数:26,代码来源:_knn.py

示例6: sample_logits

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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 
开发者ID:pbloem,项目名称:language-models,代码行数:21,代码来源:util.py

示例7: n_effective

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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) 
开发者ID:joshspeagle,项目名称:dynesty,代码行数:19,代码来源:dynamicsampler.py

示例8: n_effective

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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) 
开发者ID:joshspeagle,项目名称:dynesty,代码行数:20,代码来源:sampler.py

示例9: _mc_heldout_log_likelihood

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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 
开发者ID:HIPS,项目名称:pgmult,代码行数:23,代码来源:lds.py

示例10: _conditional_psi

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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 
开发者ID:HIPS,项目名称:pgmult,代码行数:21,代码来源:lda.py

示例11: log_z

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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)
            ]),
        ])) 
开发者ID:li012589,项目名称:NeuralRG,代码行数:22,代码来源:exactz.py

示例12: _create_lookup_table

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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() 
开发者ID:lscsoft,项目名称:bilby,代码行数:24,代码来源:likelihood.py

示例13: _evaluate_point_logpdf

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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) 
开发者ID:bfarr,项目名称:kombine,代码行数:26,代码来源:clustered_kde.py

示例14: logpdf

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
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)
        ]) 
开发者ID:probcomp,项目名称:cgpm,代码行数:12,代码来源:parabola.py

示例15: _regime_transition_matrix_tvtp

# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import logsumexp [as 别名]
def _regime_transition_matrix_tvtp(self, params, exog_tvtp=None):
        if exog_tvtp is None:
            exog_tvtp = self.exog_tvtp
        nobs = len(exog_tvtp)

        regime_transition_matrix = np.zeros(
            (self.k_regimes, self.k_regimes, nobs),
            dtype=np.promote_types(np.float64, params.dtype))

        # Compute the predicted values from the regression
        for i in range(self.k_regimes):
            coeffs = params[self.parameters[i, 'regime_transition']]
            regime_transition_matrix[:-1, i, :] = np.dot(
                exog_tvtp,
                np.reshape(coeffs, (self.k_regimes-1, self.k_tvtp)).T).T

        # Perform the logistic transformation
        tmp = np.c_[np.zeros((nobs, self.k_regimes, 1)),
                    regime_transition_matrix[:-1, :, :].T].T
        regime_transition_matrix[:-1, :, :] = np.exp(
            regime_transition_matrix[:-1, :, :] - logsumexp(tmp, axis=0))

        # Compute the last column of the transition matrix
        regime_transition_matrix[-1, :, :] = (
            1 - np.sum(regime_transition_matrix[:-1, :, :], axis=0))

        return regime_transition_matrix 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:29,代码来源:markov_switching.py


注:本文中的scipy.misc.logsumexp方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。