本文整理汇总了Python中keras.backend.pow方法的典型用法代码示例。如果您正苦于以下问题:Python backend.pow方法的具体用法?Python backend.pow怎么用?Python backend.pow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.pow方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def call(self, x, mask=None):
if K.image_dim_ordering == "th":
_, f, r, c = self.shape
else:
_, r, c, f = self.shape
squared = K.square(x)
pooled = K.pool2d(squared, (self.n, self.n), strides=(1, 1),
padding="same", pool_mode="avg")
if K.image_dim_ordering == "th":
summed = K.sum(pooled, axis=1, keepdims=True)
averaged = self.alpha * K.repeat_elements(summed, f, axis=1)
else:
summed = K.sum(pooled, axis=3, keepdims=True)
averaged = self.alpha * K.repeat_elements(summed, f, axis=3)
denom = K.pow(self.k + averaged, self.beta)
return x / denom
示例2: sharpen
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def sharpen(_weight_t, scalar_gama_t):
'''
The convolution operation in convolutional shift can cause leakage or
dispersion of weights over time if the shift weighting is no sharp.
For example, if shift of -1, 0 and 1 are given weights of 0.1, 0.8,
and 0.1, the rotation will transform a weighting focused at single
point into one slightly blurred over three points. To combat this,
each head emits one further scalar \gama >= 1 whose effect is sharpen
the final weighting as follows:
$$w_{i}^{(t)} = \frac{(\hat{w}_{i}^{(t)})^{\gama}}
{\sum_{j}\hat{w}_{j}^{(t)})^{\gama}}$$
:param _weight_t: the weight vector which denotes a memory address.
:param scalar_gama_t: the scalar for sharpen.
:return: the sharpened weight.
'''
weight_t = K.pow(_weight_t, scalar_gama_t)
return weight_t / K.sum(weight_t)
示例3: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [K.update_add(self.iterations, 1)]
t = K.cast(self.iterations, K.floatx()) + 1
lr_t = self.learning_rate * (K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t)))
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
self.weights = [self.iterations] + ms + vs
for p, g, m, v in zip(params, grads, ms, vs):
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
p_t = lr_t * m_t / (K.sqrt(v_t) + self.epsilon)
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
self.updates.append(K.update_sub(p, p_t))
return self.updates
示例4: focal_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def focal_loss(y_true, y_pred, gamma=2, alpha=0.25):
"""Compute focal loss.
# Arguments
y_true: Ground truth targets,
tensor of shape (?, num_boxes, num_classes).
y_pred: Predicted logits,
tensor of shape (?, num_boxes, num_classes).
# Returns
focal_loss: Focal loss, tensor of shape (?, num_boxes).
# References
https://arxiv.org/abs/1708.02002
"""
#y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
eps = K.epsilon()
y_pred = K.clip(y_pred, eps, 1. - eps)
pt = tf.where(tf.equal(y_true, 1), y_pred, 1 - y_pred)
focal_loss = -tf.reduce_sum(alpha * K.pow(1. - pt, gamma) * K.log(pt), axis=-1)
return focal_loss
示例5: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def call(self, x):
if (self.size is None) or (self.mode == 'sum'):
self.size = int(x.shape[-1])
batch_size, seq_len = K.shape(x)[0], K.shape(x)[1]
position_j = 1. / K.pow(10000.,
2 * K.arange(self.size / 2, dtype='float32'
) / self.size)
position_j = K.expand_dims(position_j, 0)
# K.arange不支持变长,只好用这种方法生成
position_i = K.cumsum(K.ones_like(x[:, :, 0]), 1) - 1
position_i = K.expand_dims(position_i, 2)
position_ij = K.dot(position_i, position_j)
position_ij = K.concatenate(
[K.cos(position_ij), K.sin(position_ij)], 2)
if self.mode == 'sum':
return position_ij + x
elif self.mode == 'concat':
return K.concatenate([position_ij, x], 2)
示例6: full_affinity
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def full_affinity(X, scale):
'''
Calculates the symmetrized full Gaussian affinity matrix, scaled
by a provided scale
X: input dataset of size n
scale: provided scale
returns: n x n affinity matrix
'''
sigma = K.variable(scale)
Dx = squared_distance(X)
sigma_squared = K.pow(sigma, 2)
sigma_squared = K.expand_dims(sigma_squared, -1)
Dx_scaled = Dx / (2 * sigma_squared)
W = K.exp(-Dx_scaled)
return W
示例7: make_smooth_ndcg_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def make_smooth_ndcg_loss(y_true, y_pred):
y_true, y_pred = tensorify(y_true), tensorify(y_pred)
n_objects = K.max(y_true) + 1.0
y_true_f = K.cast(y_true, "float32")
relevance = n_objects - y_true_f - 1.0
log_term = K.log(relevance + 2.0) / K.log(2.0)
exp_relevance = K.pow(2.0, relevance) - 1.0
gains = exp_relevance / log_term
# Calculate ideal dcg:
idcg = K.sum(gains, axis=-1)
# Calculate smoothed dcg:
exped = K.exp(y_pred)
exped = exped / K.sum(exped, axis=-1, keepdims=True)
# toppred, toppred_ind = tf.nn.top_k(gains * exped, k)
return 1 - K.sum(exped * gains, axis=-1) / idcg
示例8: focal_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def focal_loss(gamma=2., alpha=.25):
def focal_loss_fixed(y_true, y_pred):
pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
return -K.mean(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) - K.mean((1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0))
return focal_loss_fixed
示例9: __call__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def __call__(self):
def calc_loss(y_true, y_pred):
loss = K.pow(K.abs(y_true - y_pred) + 1e-8, self.gamma)
return loss
return calc_loss
示例10: gelu
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def gelu(x):
return 0.5 * x * (1 + K.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * K.pow(x, 3))))
# https://stackoverflow.com/a/42194662/2796084
示例11: loglik_discrete
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def loglik_discrete(y, u, a, b, epsilon=K.epsilon()):
hazard0 = K.pow((y + epsilon) / a, b)
hazard1 = K.pow((y + 1.0) / a, b)
loglikelihoods = u * \
K.log(K.exp(hazard1 - hazard0) - (1.0 - epsilon)) - hazard1
return loglikelihoods
示例12: loglik_continuous
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def loglik_continuous(y, u, a, b, epsilon=K.epsilon()):
ya = (y + epsilon) / a
loglikelihoods = u * (K.log(b) + b * K.log(ya)) - K.pow(ya, b)
return loglikelihoods
示例13: loglik_continuous_conditional_correction
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def loglik_continuous_conditional_correction(y, u, a, b, epsilon=K.epsilon()):
"""Integrated conditional excess loss.
Explanation TODO
"""
ya = (y + epsilon) / a
loglikelihoods = y * \
(u * (K.log(b) + b * K.log(ya)) - (b / (b + 1.)) * K.pow(ya, b))
return loglikelihoods
示例14: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [(self.iterations, self.iterations + 1)]
t = self.iterations + 1
lr_t = self.lr * K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))
ms = [K.variable(np.zeros(K.get_value(p).shape)) for p in params]
vs = [K.variable(np.zeros(K.get_value(p).shape)) for p in params]
gs = [K.variable(np.zeros(K.get_value(p).shape)) for p in params]
self.weights = ms + vs
for p, g, m, v, gg in zip(params, grads, ms, vs, gs):
flag = K.equal(self.iterations % self.accum_iters, 0)
flag = K.cast(flag, dtype='float32')
gg_t = (1 - flag) * (gg + g)
m_t = (self.beta_1 * m) + (1. - self.beta_1) * (gg + flag * g) / self.accum_iters
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square((gg + flag * g) / self.accum_iters)
p_t = p - flag * lr_t * m_t / (K.sqrt(v_t) + self.epsilon)
self.updates.append((m, flag * m_t + (1 - flag) * m))
self.updates.append((v, flag * v_t + (1 - flag) * v))
self.updates.append((gg, gg_t))
new_p = p_t
# apply constraints
if getattr(p, 'constraint', None) is not None:
c = p.constraints(new_p)
new_p = c(new_p)
self.updates.append((p, new_p))
return self.updates
示例15: gelu_tanh
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pow [as 别名]
def gelu_tanh(x):
"""基于Tanh近似计算的gelu函数
"""
cdf = 0.5 * (1.0 + K.tanh(
(np.sqrt(2 / np.pi) * (x + 0.044715 * K.pow(x, 3)))))
return x * cdf