本文整理汇总了Python中numpy.power方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.power方法的具体用法?Python numpy.power怎么用?Python numpy.power使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.power方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cost
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def cost(params, Y, R, num_features):
Y = np.matrix(Y) # (1682, 943)
R = np.matrix(R) # (1682, 943)
num_movies = Y.shape[0]
num_users = Y.shape[1]
# reshape the parameter array into parameter matrices
X = np.matrix(np.reshape(params[:num_movies * num_features], (num_movies, num_features))) # (1682, 10)
Theta = np.matrix(np.reshape(params[num_movies * num_features:], (num_users, num_features))) # (943, 10)
# initializations
J = 0
# compute the cost
error = np.multiply((X * Theta.T) - Y, R) # (1682, 943)
squared_error = np.power(error, 2) # (1682, 943)
J = (1. / 2) * np.sum(squared_error)
return J
示例2: prepare_poly_data
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def prepare_poly_data(*args, power):
"""
args: keep feeding in X, Xval, or Xtest
will return in the same order
"""
def prepare(x):
# expand feature
df = poly_features(x, power=power)
# normalization
ndarr = normalize_feature(df).as_matrix()
# add intercept term
return np.insert(ndarr, 0, np.ones(ndarr.shape[0]), axis=1)
return [prepare(x) for x in args]
示例3: spectrogram2wav
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def spectrogram2wav(mag):
'''# Generate wave file from spectrogram'''
# transpose
mag = mag.T
# de-noramlize
mag = (np.clip(mag, 0, 1) * hp.max_db) - hp.max_db + hp.ref_db
# to amplitude
mag = np.power(10.0, mag * 0.05)
# wav reconstruction
wav = griffin_lim(mag)
# de-preemphasis
wav = signal.lfilter([1], [1, -hp.preemphasis], wav)
# trim
wav, _ = librosa.effects.trim(wav)
return wav.astype(np.float32)
示例4: test_return_l2_regularizer_weights
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def test_return_l2_regularizer_weights(self):
conv_hyperparams_text_proto = """
regularizer {
l2_regularizer {
weight: 0.42
}
}
initializer {
truncated_normal_initializer {
}
}
"""
conv_hyperparams_proto = hyperparams_pb2.Hyperparams()
text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto)
scope = hyperparams_builder.build(conv_hyperparams_proto, is_training=True)
conv_scope_arguments = scope.values()[0]
regularizer = conv_scope_arguments['weights_regularizer']
weights = np.array([1., -1, 4., 2.])
with self.test_session() as sess:
result = sess.run(regularizer(tf.constant(weights)))
self.assertAllClose(np.power(weights, 2).sum() / 2.0 * 0.42, result)
示例5: speech_aug_scheduler
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def speech_aug_scheduler(step, s_r, s_i, s_f, peak_lr):
# Starting from 0, ramp-up to set LR and converge to 0.01*LR, w/ exp. decay
final_lr_ratio = 0.01
exp_decay_lambda = -np.log10(final_lr_ratio)/(s_f-s_i) # Approx. w/ 10-based
cur_step = step+1
if cur_step<s_r:
# Ramp-up
return peak_lr*float(cur_step)/s_r
elif cur_step<s_i:
# Hold
return peak_lr
elif cur_step<=s_f:
# Decay
return peak_lr*np.power(10,-exp_decay_lambda*(cur_step-s_i))
else:
# Converge
return peak_lr*final_lr_ratio
示例6: zipf_distribution
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def zipf_distribution(nbr_symbols, alpha):
"""Helper function: Create a Zipf distribution.
Args:
nbr_symbols: number of symbols to use in the distribution.
alpha: float, Zipf's Law Distribution parameter. Default = 1.5.
Usually for modelling natural text distribution is in
the range [1.1-1.6].
Returns:
distr_map: list of float, Zipf's distribution over nbr_symbols.
"""
tmp = np.power(np.arange(1, nbr_symbols + 1), -alpha)
zeta = np.r_[0.0, np.cumsum(tmp)]
return [x / zeta[-1] for x in zeta]
示例7: class_balanced_weight
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def class_balanced_weight(beta, samples_per_class):
assert 0 <= beta < 1, 'Wrong rang of beta {}'.format(beta)
if not isinstance(samples_per_class, np.ndarray):
if isinstance(samples_per_class, (list, tuple)):
samples_per_class = np.array(samples_per_class)
elif torch.is_tensor(samples_per_class):
samples_per_class = samples_per_class.numpy()
else:
raise NotImplementedError(
'Type of samples_per_class should be {}, {} or {} but got {}'.format(
(list, tuple), np.ndarray, torch.Tensor, type(samples_per_class)))
assert isinstance(samples_per_class, np.ndarray) \
and isinstance(beta, numbers.Number)
balanced_matrix = (1 - beta) / (1 - np.power(beta, samples_per_class))
return torch.Tensor(balanced_matrix)
示例8: get_poly_cc
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def get_poly_cc(n, k, t):
""" This is a helper function to get the coeffitient of coefficient for n-th
order polynomial with k-th derivative at time t.
"""
assert (n > 0 and k >= 0), "order and derivative must be positive."
cc = np.ones(n)
D = np.linspace(0, n-1, n)
for i in range(n):
for j in range(k):
cc[i] = cc[i] * D[i]
D[i] = D[i] - 1
if D[i] == -1:
D[i] = 0
for i, c in enumerate(cc):
cc[i] = c * np.power(t, D[i])
return cc
# Minimum Snap Trajectory
示例9: test_get_poly_cc_t
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def test_get_poly_cc_t(self):
cc = trajGen3D.get_poly_cc(4, 0, 1)
expected = [1, 1, 1, 1]
np.testing.assert_array_equal(cc, expected)
cc = trajGen3D.get_poly_cc(8, 0, 2)
expected = [1, 2, 4, 8, 16, 32, 64, 128]
np.testing.assert_array_equal(cc, expected)
cc = trajGen3D.get_poly_cc(8, 1, 1)
expected = np.linspace(0, 7, 8)
np.testing.assert_array_equal(cc, expected)
t = 2
cc = trajGen3D.get_poly_cc(8, 1, t)
expected = [0, 1, 2*t, 3*np.power(t,2), 4*np.power(t,3), 5*np.power(t,4), 6*np.power(t,5), 7*np.power(t,6)]
np.testing.assert_array_equal(cc, expected)
示例10: normalize_adj
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def normalize_adj(A, is_sym=True, exponent=0.5):
"""
Normalize adjacency matrix
is_sym=True: D^{-1/2} A D^{-1/2}
is_sym=False: D^{-1} A
"""
rowsum = np.array(A.sum(1))
if is_sym:
r_inv = np.power(rowsum, -exponent).flatten()
else:
r_inv = np.power(rowsum, -1.0).flatten()
r_inv[np.isinf(r_inv)] = 0.
if sp.isspmatrix(A):
r_mat_inv = sp.diags(r_inv.squeeze())
else:
r_mat_inv = np.diag(r_inv)
if is_sym:
return r_mat_inv.dot(A).dot(r_mat_inv)
else:
return r_mat_inv.dot(A)
示例11: impulseamp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def impulseamp(self,tnow):
"""
Apply impulsive wave to the system
Args:
tnow: float
Current time in A.U.
Returns:
amp: float
Amplitude of field at time
ison: bool
On whether field is on or off
"""
amp = self.fieldamp*np.sin(self.fieldfreq*tnow)*\
(1.0/math.sqrt(2.0*3.1415*self.tau*self.tau))*\
np.exp(-1.0*np.power(tnow-self.tOn,2.0)/(2.0*self.tau*self.tau))
ison = False
if (np.abs(amp)>self.field_tol):
ison = True
return amp,ison
示例12: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
super(AverageHeadAttention, self).__init__()
self.n_head = n_head
self.d_k = d_k
self.d_v = d_v
self.w_qs = nn.Linear(d_model, n_head * d_k)
self.w_ks = nn.Linear(d_model, n_head * d_k)
self.w_vs = nn.Linear(d_model, n_head * d_v)
nn.init.normal_(self.w_qs.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_k)))
nn.init.normal_(self.w_ks.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_k)))
nn.init.normal_(self.w_vs.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_v)))
self.attention = ScaledDotProductAttention(temperature=np.power(d_k, 0.5))
self.layer_norm = nn.LayerNorm(d_model)
self.fc = nn.Linear(d_v, d_model)
nn.init.xavier_normal_(self.fc.weight)
self.dropout = nn.Dropout(dropout)
示例13: get_sinusoid_encoding_table
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def get_sinusoid_encoding_table(n_position, d_hid, padding_idx=None):
''' Sinusoid position encoding table '''
def cal_angle(position, hid_idx):
return position / np.power(10000, 2 * (hid_idx // 2) / d_hid)
def get_posi_angle_vec(position):
return [cal_angle(position, hid_j) for hid_j in range(d_hid)]
sinusoid_table = np.array([get_posi_angle_vec(pos_i) for pos_i in range(n_position)])
sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2]) # dim 2i
sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2]) # dim 2i+1
if padding_idx is not None:
# zero vector for padding dimension
sinusoid_table[padding_idx] = 0.
return torch.FloatTensor(sinusoid_table)
示例14: scotts_factor
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def scotts_factor(self):
return power(self.n, -1./(self.d+4))
示例15: imcv2_recolor
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import power [as 别名]
def imcv2_recolor(im, a = .1):
t = [np.random.uniform()]
t += [np.random.uniform()]
t += [np.random.uniform()]
t = np.array(t) * 2. - 1.
# random amplify each channel
im = im * (1 + t * a)
mx = 255. * (1 + a)
up = np.random.uniform() * 2 - 1
# im = np.power(im/mx, 1. + up * .5)
im = cv2.pow(im/mx, 1. + up * .5)
return np.array(im * 255., np.uint8)