本文整理汇总了Python中numpy.reciprocal方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.reciprocal方法的具体用法?Python numpy.reciprocal怎么用?Python numpy.reciprocal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.reciprocal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_rho
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def update_rho(self, rho_new):
"""
Update set-size parameter rho
"""
if rho_new <= 0:
raise ValueError("rho must be positive")
# Update rho
self.work.settings.rho = np.minimum(np.maximum(rho_new,
RHO_MIN), RHO_MAX)
# Update rho_vec and rho_inv_vec
ineq_ind = np.where(self.work.constr_type == 0)
eq_ind = np.where(self.work.constr_type == 1)
self.work.rho_vec[ineq_ind] = self.work.settings.rho
self.work.rho_vec[eq_ind] = RHO_EQ_OVER_RHO_INEQ * self.work.settings.rho
self.work.rho_inv_vec = np.reciprocal(self.work.rho_vec)
# Factorize KKT
self.work.linsys_solver = linsys_solver(self.work)
示例2: ComputeMassMatrixInfo
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def ComputeMassMatrixInfo(self, M, formulation, fem_solver):
"""Computes the inverse of lumped mass matrix and so on
"""
invM = None
if formulation.fields == "electro_mechanics":
if fem_solver.mass_type == "lumped":
M = M.ravel()
invM = np.zeros_like(M)
invM[self.mechanical_dofs] = np.reciprocal(M[self.mechanical_dofs])
M_mech = M[self.mechanical_dofs]
else:
M_mech = M[self.mechanical_dofs,:][:,self.mechanical_dofs]
else:
if fem_solver.mass_type == "lumped":
M = M.ravel()
M_mech = M
invM = np.reciprocal(M)
else:
M_mech = M
return M_mech, invM
示例3: forward
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def forward(self, inputs):
self.retain_inputs((0, 1, 2, 4))
x, gamma, mean, var, gy = inputs
expander = self.expander
xp = backend.get_array_module(x)
if self.inv_std is None or self.inv_var is None:
self.inv_var = xp.reciprocal(var + self.eps)
self.inv_std = xp.sqrt(self.inv_var, dtype=self.inv_var.dtype)
self.gamma_over_std = gamma * self.inv_std
x_hat = _x_hat(x, mean[expander], self.inv_std[expander])
gx = self.gamma_over_std[expander] * gy
gbeta = gy.sum(axis=self.axis, dtype=gamma.dtype)
ggamma = (x_hat * gy).sum(axis=self.axis)
gmean = -self.gamma_over_std * gbeta
gvar = - 0.5 * self.inv_var * (
gamma * ggamma).astype(var.dtype, copy=False)
gx = gx.astype(dtype=x.dtype)
self.retain_outputs((0, 1, 2, 3, 4))
return gx, ggamma, gbeta, gmean, gvar
示例4: predict_proba
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def predict_proba(X, classifier):
"""Probability estimation for OvR logistic regression.
Positive class probabilities are computed as
1. / (1. + np.exp(-classifier.decision_function(X)));
multiclass is handled by normalizing that over all classes.
"""
prob = np.dot(X, classifier['coef_'].T) + classifier['intercept_']
prob = prob.ravel() if prob.shape[1] == 1 else prob
prob *= -1
np.exp(prob, prob)
prob += 1
np.reciprocal(prob, prob)
if prob.ndim == 1:
return np.vstack([1 - prob, prob]).T
else:
# OvR normalization, like LibLinear's predict_probability
prob /= prob.sum(axis=1).reshape((prob.shape[0], -1))
return prob
示例5: vector_normalize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def vector_normalize(v):
"""
Normalise (Euclidean) the last axis of a numpy array
:param v: numpy vector array, any dimension
:return: array normalized, 0 vectors will be np.nan
.. versionadded:: 9.3.1
"""
if v.ndim < 2:
return np.array((1.,))
vs = v.shape
v = v.reshape((-1, v.shape[-1]))
mag = np.linalg.norm(v, axis=1)
mag[mag == 0.] = np.nan
return (v.T * np.reciprocal(mag)).T.reshape(vs)
示例6: l1_inverse
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def l1_inverse(depth1,depth2):
"""
Computes the l1 errors between inverses of two depth maps.
Takes preprocessed depths (no nans, infs and non-positive values)
depth1: one depth map
depth2: another depth map
Returns:
L1(log)
"""
assert(np.all(np.isfinite(depth1) & np.isfinite(depth2) & (depth1 > 0) & (depth2 > 0)))
diff = np.reciprocal(depth1) - np.reciprocal(depth2)
num_pixels = float(diff.size)
if num_pixels == 0:
return np.nan
else:
return np.sum(np.absolute(diff)) / num_pixels
示例7: compute_cooccurrence_constraint
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def compute_cooccurrence_constraint(self, nodes):
"""
Co-occurrence constraint as described in the paper.
Parameters
----------
nodes: np.array
Nodes whose features are considered for change
Returns
-------
np.array [len(nodes), D], dtype bool
Binary matrix of dimension len(nodes) x D. A 1 in entry n,d indicates that
we are allowed to add feature d to the features of node n.
"""
words_graph = self.cooc_matrix.copy()
D = self.X_obs.shape[1]
words_graph.setdiag(0)
words_graph = (words_graph > 0)
word_degrees = np.sum(words_graph, axis=0).A1
inv_word_degrees = np.reciprocal(word_degrees.astype(float) + 1e-8)
sd = np.zeros([self.N])
for n in range(self.N):
n_idx = self.X_obs[n, :].nonzero()[1]
sd[n] = np.sum(inv_word_degrees[n_idx.tolist()])
scores_matrix = sp.lil_matrix((self.N, D))
for n in nodes:
common_words = words_graph.multiply(self.X_obs[n])
idegs = inv_word_degrees[common_words.nonzero()[1]]
nnz = common_words.nonzero()[0]
scores = np.array([idegs[nnz == ix].sum() for ix in range(D)])
scores_matrix[n] = scores
self.cooc_constraint = sp.csr_matrix(scores_matrix - 0.5 * sd[:, None] > 0)
示例8: test_blocked
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def test_blocked(self):
# test alignments offsets for simd instructions
# alignments for vz + 2 * (vs - 1) + 1
for dt, sz in [(np.float32, 11), (np.float64, 7), (np.int32, 11)]:
for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt,
type='binary',
max_size=sz):
exp1 = np.ones_like(inp1)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg)
assert_almost_equal(np.add(inp1, 2), exp1 + 2, err_msg=msg)
assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg)
np.add(inp1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
inp2[...] += np.arange(inp2.size, dtype=dt) + 1
assert_almost_equal(np.square(inp2),
np.multiply(inp2, inp2), err_msg=msg)
# skip true divide for ints
if dt != np.int32 or (sys.version_info.major < 3 and not sys.py3kwarning):
assert_almost_equal(np.reciprocal(inp2),
np.divide(1, inp2), err_msg=msg)
inp1[...] = np.ones_like(inp1)
np.add(inp1, 2, out=out)
assert_almost_equal(out, exp1 + 2, err_msg=msg)
inp2[...] = np.ones_like(inp2)
np.add(2, inp2, out=out)
assert_almost_equal(out, exp1 + 2, err_msg=msg)
示例9: reciprocal
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def reciprocal(self):
return self.power(-1)
##############################################
示例10: period
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def period(self):
r""" Return the period :math:`T = \frac{1}{f}`. """
return self.reciprocal()
##############################################
示例11: test_reciprocal
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def test_reciprocal(input_data):
expected_output = np.reciprocal(input_data)
node = onnx.helper.make_node('Reciprocal', inputs=['x'], outputs=['y'])
ng_results = run_node(node, [input_data])
assert np.allclose(ng_results, [expected_output])
示例12: test_blocked
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def test_blocked(self):
# test alignments offsets for simd instructions
# alignments for vz + 2 * (vs - 1) + 1
for dt, sz in [(np.float32, 11), (np.float64, 7)]:
for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt,
type='binary',
max_size=sz):
exp1 = np.ones_like(inp1)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg)
assert_almost_equal(np.add(inp1, 1), exp1 + 1, err_msg=msg)
assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg)
np.add(inp1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
inp2[...] += np.arange(inp2.size, dtype=dt) + 1
assert_almost_equal(np.square(inp2),
np.multiply(inp2, inp2), err_msg=msg)
assert_almost_equal(np.reciprocal(inp2),
np.divide(1, inp2), err_msg=msg)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
np.add(inp1, 1, out=out)
assert_almost_equal(out, exp1 + 1, err_msg=msg)
np.add(1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
示例13: __call__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def __call__(self, image, mask):
mean = np.reshape(np.array(self.mean, dtype=image.dtype), [1, 1, self.channel])
std = np.reshape(np.array(self.std, dtype=image.dtype), [1, 1, self.channel])
denominator = np.reciprocal(std, dtype=image.dtype)
new_image = (image - mean) * denominator
new_mask = mask
return new_image, new_mask
示例14: compute_cooccurrence_constraint
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def compute_cooccurrence_constraint(self, nodes):
"""
Co-occurrence constraint as described in the paper.
Parameters
----------
nodes: np.array
Nodes whose features are considered for change
Returns
-------
np.array [len(nodes), D], dtype bool
Binary matrix of dimension len(nodes) x D. A 1 in entry n,d indicates that
we are allowed to add feature d to the features of node n.
"""
words_graph = self.cooc_matrix.copy()
D = self.modified_features.shape[1]
words_graph.setdiag(0)
words_graph = (words_graph > 0)
word_degrees = np.sum(words_graph, axis=0).A1
inv_word_degrees = np.reciprocal(word_degrees.astype(float) + 1e-8)
sd = np.zeros([self.nnodes])
for n in range(self.nnodes):
n_idx = self.modified_features[n, :].nonzero()[1]
sd[n] = np.sum(inv_word_degrees[n_idx.tolist()])
scores_matrix = sp.lil_matrix((self.nnodes, D))
for n in nodes:
common_words = words_graph.multiply(self.modified_features[n])
idegs = inv_word_degrees[common_words.nonzero()[1]]
nnz = common_words.nonzero()[0]
scores = np.array([idegs[nnz == ix].sum() for ix in range(D)])
scores_matrix[n] = scores
self.cooc_constraint = sp.csr_matrix(scores_matrix - 0.5 * sd[:, None] > 0)
示例15: _batch_normalization
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import reciprocal [as 别名]
def _batch_normalization(self, x, mean, variance, bias, scale,
variance_epsilon):
inv = np.reciprocal(np.sqrt(variance + variance_epsilon))
if scale is not None:
inv *= scale
return x * inv + (bias - mean * inv if bias is not None else -mean * inv)