本文整理汇总了Python中autograd.numpy.newaxis方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.newaxis方法的具体用法?Python numpy.newaxis怎么用?Python numpy.newaxis使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类autograd.numpy
的用法示例。
在下文中一共展示了numpy.newaxis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: truncate0
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
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)
示例2: bound_by_data
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
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
示例3: gradX_Y
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def gradX_Y(self, X, Y, dim):
"""
Compute the gradient with respect to the dimension dim of X in k(X, Y).
X: nx x d
Y: ny x d
Return a numpy array of size nx x ny.
"""
D2 = util.dist2_matrix(X, Y)
# 1d array of length nx
Xi = X[:, dim]
# 1d array of length ny
Yi = Y[:, dim]
# nx x ny
dim_diff = Xi[:, np.newaxis] - Yi[np.newaxis, :]
b = self.b
c = self.c
Gdim = ( 2.0*b*(c**2 + D2)**(b-1) )*dim_diff
assert Gdim.shape[0] == X.shape[0]
assert Gdim.shape[1] == Y.shape[0]
return Gdim
示例4: pair_gradX_Y
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def pair_gradX_Y(self, X, Y):
"""
Compute the gradient with respect to X in k(X, Y), evaluated at the
specified X and Y.
X: n x d
Y: n x d
Return a numpy array of size n x d
"""
sigma2 = self.sigma2
Kvec = self.pair_eval(X, Y)
# n x d
Diff = X - Y
G = -Kvec[:, np.newaxis]*Diff/sigma2
return G
示例5: gradXY_sum
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def gradXY_sum(self, X, Y):
r"""
Compute \sum_{i=1}^d \frac{\partial^2 k(X, Y)}{\partial x_i \partial y_i}
evaluated at each x_i in X, and y_i in Y.
X: nx x d numpy array.
Y: ny x d numpy array.
Return a nx x ny numpy array of the derivatives.
"""
(n1, d1) = X.shape
(n2, d2) = Y.shape
assert d1==d2, 'Dimensions of the two inputs must be the same'
d = d1
sigma2 = self.sigma2
D2 = np.sum(X**2, 1)[:, np.newaxis] - 2*np.dot(X, Y.T) + np.sum(Y**2, 1)
K = np.exp(old_div(-D2,(2.0*sigma2)))
G = K/sigma2*(d - old_div(D2,sigma2))
return G
示例6: eval
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def eval(self, X, Y):
"""
Evaluate the kernel on data X and Y
X: nx x d where each row represents one point
Y: ny x d
return nx x ny Gram matrix
"""
sumx2 = np.sum(X**2, axis=1)[:, np.newaxis]
sumy2 = np.sum(Y**2, axis=1)[np.newaxis, :]
D2 = sumx2 - 2 * np.dot(X, Y.T) + sumy2
return np.tensordot(
self.wts,
np.exp(
D2[np.newaxis, :, :]
/ (-2 * self.sigma2s[:, np.newaxis, np.newaxis])),
1)
示例7: gradY_X
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def gradY_X(self, X, Y, dim):
"""
Compute the gradient with respect to the dimension dim of Y in k(X, Y).
X: nx x d
Y: ny x d
Return a numpy array of size nx x ny.
"""
gamma = 1/X.shape[1] if self.gamma is None else self.gamma
if self.degree == 1: # optimization, other expression is valid too
out = gamma * X[:, dim, np.newaxis] # nx x 1
return np.repeat(out, Y.shape[0], axis=1)
dot = np.dot(X, Y.T)
return (self.degree * (gamma * dot + self.coef0) ** (self.degree - 1)
* gamma * X[:, dim, np.newaxis])
示例8: ascii_table
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def ascii_table(self, tablefmt="pipe"):
"""
Return an ASCII string representation of the table.
tablefmt: "plain", "fancy_grid", "grid", "simple" might be useful.
"""
methods = self.methods
xvalues = self.xvalues
plot_matrix = self.plot_matrix
import tabulate
# https://pypi.python.org/pypi/tabulate
aug_table = np.hstack((np.array(methods)[:, np.newaxis], plot_matrix))
return tabulate.tabulate(aug_table, xvalues, tablefmt=tablefmt)
# end of class PlotValues
示例9: image
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def image(self, image):
"""Updates the coefficients if the image is changed"""
if len(image.shape) == 2:
self._image = image[np.newaxis, :, :]
else:
self._image = image
if self._direct == True:
self._coeffs = self.direct_transform()
else:
self._coeffs = self.transform()
示例10: coefficients
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def coefficients(self, coeffs):
"""Updates the image if the coefficients are changed"""
if len(np.shape(coeffs)) == 3:
coeffs = coeffs[np.newaxis, :, :, :]
self._coeffs = coeffs
rec = []
for star in self._coeffs:
rec.append(iuwt(star))
self._image = np.array(rec)
示例11: transform
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def transform(self):
""" Performs the wavelet transform of an image by convolution with the seed wavelet
Seed wavelets are the transform of a dirac in starlets when computed for a given shape,
the seed is cached to be reused for images with the same shape.
The transform is applied to `self._image`
Returns
-------
starlet: numpy ndarray
the starlet transform of the Starlet object's image
"""
try:
#Check if the starlet seed exists
seed_fft = Cache.check('Starlet', tuple(self._starlet_shape))
except KeyError:
# make a starlet seed
self.seed = mk_starlet(self._starlet_shape)
# Take its fft
seed_fft = fft.Fourier(self.seed)
seed_fft.fft(self._starlet_shape[-2:], (-2,-1))
# Cache the fft
Cache.set('Starlet', tuple(self._starlet_shape), seed_fft)
coefficients = []
for im in self._image:
coefficients.append(fft.convolve(seed_fft, fft.Fourier(im[np.newaxis, :, :]), axes = (-2,-1)).image)
return np.array(coefficients)
示例12: kernel
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [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)
示例13: weighted_post
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def weighted_post(th0, Sig0inv, Siginv, x, w):
Sigp = np.linalg.inv(Sig0inv + w.sum()*Siginv)
mup = np.dot(Sigp, np.dot(Sig0inv,th0) + np.dot(Siginv, (w[:, np.newaxis]*x).sum(axis=0)))
return mup, Sigp
示例14: ll_m2_exact
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def ll_m2_exact(muw, Sigw, Siginv, x):
L = np.linalg.cholesky(Siginv)
Rho = np.dot(np.dot(L.T, Sigw), L)
crho = 2*(Rho**2).sum() + (np.diag(Rho)*np.diag(Rho)[:, np.newaxis]).sum()
mu = np.dot(L.T, (x - muw).T).T
musq = (mu**2).sum(axis=1)
return 0.25*(crho + musq*musq[:, np.newaxis] + np.diag(Rho).sum()*(musq + musq[:,np.newaxis]) + 4*np.dot(np.dot(mu, Rho), mu.T))
#Var[Log N(x;, mu, Sig)] under mu ~ N(muw, Sigw)
示例15: ll_m2_exact_diag
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import newaxis [as 别名]
def ll_m2_exact_diag(muw, Sigw, Siginv, x):
L = np.linalg.cholesky(Siginv)
Rho = np.dot(np.dot(L.T, Sigw), L)
crho = 2*(Rho**2).sum() + (np.diag(Rho)*np.diag(Rho)[:, np.newaxis]).sum()
mu = np.dot(L.T, (x - muw).T).T
musq = (mu**2).sum(axis=1)
return 0.25*(crho + musq**2 + 2*np.diag(Rho).sum()*musq + 4*(np.dot(mu, Rho)*mu).sum(axis=1))