本文整理汇总了Python中autograd.numpy.exp方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.exp方法的具体用法?Python numpy.exp怎么用?Python numpy.exp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类autograd.numpy
的用法示例。
在下文中一共展示了numpy.exp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def __init__(self, n_var=2, n_constr=2, **kwargs):
super().__init__(n_var, n_constr, **kwargs)
a, b = anp.zeros(n_constr + 1), anp.zeros(n_constr + 1)
a[0], b[0] = 1, 1
delta = 1 / (n_constr + 1)
alpha = delta
for j in range(n_constr):
beta = a[j] * anp.exp(-b[j] * alpha)
a[j + 1] = (a[j] + beta) / 2
b[j + 1] = - 1 / alpha * anp.log(beta / a[j + 1])
alpha += delta
self.a = a[1:]
self.b = b[1:]
示例2: simple_admixture_3pop
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def simple_admixture_3pop(x=None):
if x is None:
x = np.random.normal(size=7)
t = np.cumsum(np.exp(x[:5]))
p = 1.0 / (1.0 + np.exp(x[5:]))
model = momi.DemographicModel(1., .25)
model.add_leaf("b")
model.add_leaf("a")
model.add_leaf("c")
model.move_lineages("a", "c", t[1], p=1.-p[1])
model.move_lineages("a", "d", t[0], p=1.-p[0])
model.move_lineages("c", "d", t[2])
model.move_lineages("d", "b", t[3])
model.move_lineages("a", "b", t[4])
return model
示例3: _compute_loss
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def _compute_loss(self):
"""Compute and store loss value for the given batch of examples."""
if self._loss_computed:
return
self._compute_distances()
# NLL loss from the NIPS paper.
exp_negative_distances = np.exp(-self.euclidean_dists) # (1 + neg_size, batch_size)
# Remove the value for the true edge (u,v) from the partition function
Z = exp_negative_distances[1:].sum(axis=0) # (batch_size)
self.exp_negative_distances = exp_negative_distances # (1 + neg_size, batch_size)
self.Z = Z # (batch_size)
self.pos_loss = self.euclidean_dists[0].sum()
self.neg_loss = np.log(self.Z).sum()
self.loss = self.pos_loss + self.neg_loss # scalar
self._loss_computed = True
示例4: _nll_loss_fn
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def _nll_loss_fn(poincare_dists):
"""
Parameters
----------
poincare_dists : numpy.array
All distances d(u,v) and d(u,v'), where v' is negative. Shape (1 + negative_size).
Returns
----------
log-likelihood loss function from the NIPS paper, Eq (6).
"""
exp_negative_distances = grad_np.exp(-poincare_dists)
# Remove the value for the true edge (u,v) from the partition function
# return -grad_np.log(exp_negative_distances[0] / (- exp_negative_distances[0] + exp_negative_distances.sum()))
return poincare_dists[0] + grad_np.log(exp_negative_distances[1:].sum())
示例5: softplus
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def softplus(x):
""" Numerically stable transform from real line to positive reals
Returns np.log(1.0 + np.exp(x))
Autograd friendly and fully vectorized
@param x: array of values in (-\infty, +\infty)
@return ans : array of values in (0, +\infty), same size as x
"""
if not isinstance(x, float):
mask1 = x > 0
mask0 = np.logical_not(mask1)
out = np.zeros_like(x)
out[mask0] = np.log1p(np.exp(x[mask0]))
out[mask1] = x[mask1] + np.log1p(np.exp(-x[mask1]))
return out
if x > 0:
return x + np.log1p(np.exp(-x))
else:
return np.log1p(np.exp(x))
示例6: callback
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def callback(params, t, g):
print("Iteration {} lower bound {}".format(t, -objective(params, t)))
plt.cla()
target_distribution = lambda x: np.exp(log_density(x, t))
var_distribution = lambda x: np.exp(variational_log_density(params, x))
plot_isocontours(ax, target_distribution)
plot_isocontours(ax, var_distribution, cmap=plt.cm.bone)
ax.set_autoscale_on(False)
rs = npr.RandomState(0)
samples = variational_sampler(params, num_plotting_samples, rs)
plt.plot(samples[:, 0], samples[:, 1], 'x')
plt.draw()
plt.pause(1.0/30.0)
示例7: black_box_variational_inference
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def black_box_variational_inference(logprob, D, num_samples):
"""Implements http://arxiv.org/abs/1401.0118, and uses the
local reparameterization trick from http://arxiv.org/abs/1506.02557"""
def unpack_params(params):
# Variational dist is a diagonal Gaussian.
mean, log_std = params[:D], params[D:]
return mean, log_std
def gaussian_entropy(log_std):
return 0.5 * D * (1.0 + np.log(2*np.pi)) + np.sum(log_std)
rs = npr.RandomState(0)
def variational_objective(params, t):
"""Provides a stochastic estimate of the variational lower bound."""
mean, log_std = unpack_params(params)
samples = rs.randn(num_samples, D) * np.exp(log_std) + mean
lower_bound = gaussian_entropy(log_std) + np.mean(logprob(samples, t))
return -lower_bound
gradient = grad(variational_objective)
return variational_objective, gradient, unpack_params
示例8: _evaluate
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def _evaluate(self, x, out, *args, **kwargs):
part1 = -1. * self.a * anp.exp(-1. * self.b * anp.sqrt((1. / self.n_var) * anp.sum(x * x, axis=1)))
part2 = -1. * anp.exp((1. / self.n_var) * anp.sum(anp.cos(self.c * x), axis=1))
out["F"] = part1 + part2 + self.a + anp.exp(1)
示例9: _evaluate
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def _evaluate(self, x, out, *args, **kwargs):
out["F"] = 1 - anp.exp(-x ** 2) * anp.sin(2 * anp.pi * x) ** 2
示例10: _evaluate
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def _evaluate(self, x, out, *args, **kwargs):
l = []
for i in range(2):
l.append(-10 * anp.exp(-0.2 * anp.sqrt(anp.square(x[:, i]) + anp.square(x[:, i + 1]))))
f1 = anp.sum(anp.column_stack(l), axis=1)
f2 = anp.sum(anp.power(anp.abs(x), 0.8) + 5 * anp.sin(anp.power(x, 3)), axis=1)
out["F"] = anp.column_stack([f1, f2])
示例11: _evaluate
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def _evaluate(self, x, out, *args, **kwargs):
f1 = 1 - anp.exp(-4 * x[:, 0]) * anp.power(anp.sin(6 * anp.pi * x[:, 0]), 6)
g = 1 + 9.0 * anp.power(anp.sum(x[:, 1:], axis=1) / (self.n_var - 1.0), 0.25)
f2 = g * (1 - anp.power(f1 / g, 2))
out["F"] = anp.column_stack([f1, f2])
示例12: gaussian
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def gaussian(y, x, sigma=1, integrate=True, bbox=None):
"""Circular Gaussian Function
Parameters
----------
y: float
Vertical coordinate of the center
x: float
Horizontal coordinate of the center
sigma: float
Standard deviation of the gaussian
integrate: bool
Whether pixel integration is performed
bbox: Box
Bounding box over which to evaluate the function
Returns
-------
result: array
A 2D circular gaussian sampled at the coordinates `(y_i, x_j)`
for all i and j in `shape`.
"""
Y = np.arange(bbox.shape[1]) + bbox.origin[1]
X = np.arange(bbox.shape[2]) + bbox.origin[2]
def f(X):
if not integrate:
return np.exp(-(X ** 2) / (2 * sigma ** 2))
else:
sqrt2 = np.sqrt(2)
return (
np.sqrt(np.pi / 2)
* sigma
* (
scipy.special.erf((0.5 - X) / (sqrt2 * sigma))
+ scipy.special.erf((2 * X + 1) / (2 * sqrt2 * sigma))
)
)
return (f(Y - y)[:, None] * f(X - x)[None, :])[None, :, :]
示例13: _shift_morph
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def _shift_morph(self, shift, morph):
if shift is not None:
X = fft.Fourier(morph)
X_fft = X.fft(self.fft_shape, (0, 1))
# Apply shift in Fourier
result_fft = (
X_fft
* np.exp(self.shifter_y[:, None] * shift[0])
* np.exp(self.shifter_x[None, :] * shift[1])
)
X = fft.Fourier.from_fft(result_fft, self.fft_shape, X.shape, [0, 1])
return np.real(X.image)
return morph
示例14: kernel
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def kernel(X, Xp, hyp):
output_scale = np.exp(hyp[0])
lengthscales = np.sqrt(np.exp(hyp[1:]))
X = X/lengthscales
Xp = Xp/lengthscales
X_SumSquare = np.sum(np.square(X),axis=1);
Xp_SumSquare = np.sum(np.square(Xp),axis=1);
mul = np.dot(X,Xp.T);
dists = X_SumSquare[:,np.newaxis]+Xp_SumSquare-2.0*mul
return output_scale * np.exp(-0.5 * dists)
示例15: heston_log_st_mgf
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import exp [as 别名]
def heston_log_st_mgf(u, t, r, q, S0, V0, theta, k, sigma, rho):
dt = np.sqrt((sigma ** 2) * (u - u ** 2) + (k - rho * sigma * u) ** 2)
beta = k - u * rho * sigma
g = (beta - dt) / (beta + dt)
D_t = (beta - dt) / (sigma ** 2) * ((1 - np.exp(-dt * t)) / (1 - g * np.exp(-dt * t)))
C_t = u * (r - q) * t + k * theta / (sigma ** 2) * (
(beta - dt) * t - 2 * np.log((1 - g * np.exp(-dt * t)) / (1 - g)))
return np.exp(C_t + D_t * V0 + u * np.log(S0))