本文整理汇总了Python中keras.backend.clip方法的典型用法代码示例。如果您正苦于以下问题:Python backend.clip方法的具体用法?Python backend.clip怎么用?Python backend.clip使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.clip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: symbolic_fgs
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def symbolic_fgs(x, grad, eps=0.3, clipping=True):
"""
FGSM attack.
"""
# signed gradient
normed_grad = K.sign(grad)
# Multiply by constant epsilon
scaled_grad = eps * normed_grad
# Add perturbation to original example to obtain adversarial example
adv_x = K.stop_gradient(x + scaled_grad)
if clipping:
adv_x = K.clip(adv_x, 0, 1)
return adv_x
示例2: symbolic_fg
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def symbolic_fg(x, grad, eps=0.3, clipping=True):
"""
FG attack
"""
# Unit vector in direction of gradient
reduc_ind = list(xrange(1, len(x.get_shape())))
normed_grad = grad / tf.sqrt(tf.reduce_sum(tf.square(grad),
reduction_indices=reduc_ind,
keep_dims=True))
# Multiply by constant epsilon
scaled_grad = eps * normed_grad
# Add perturbation to original example to obtain adversarial example
adv_x = K.stop_gradient(x + scaled_grad)
if clipping:
adv_x = K.clip(adv_x, 0, 1)
return adv_x
示例3: iter_fgs
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def iter_fgs(model, x, y, steps, alpha, eps, clipping=True):
"""
I-FGSM attack.
"""
adv_x = x
# iteratively apply the FGSM with small step size
for i in range(steps):
logits = model(adv_x)
grad = gen_grad(adv_x, logits, y)
adv_x = symbolic_fgs(adv_x, grad, alpha, True)
r = adv_x - x
r = K.clip(r, -eps, eps)
adv_x = x+r
if clipping:
adv_x = K.clip(adv_x, 0, 1)
return adv_x
示例4: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None,), dtype='int32')
y = K.placeholder(shape=(None,), dtype='float32')
prediction = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(prediction * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# 상태가 입력, 큐함수가 출력인 인공신경망 생성
示例5: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None, ), dtype='int32')
y = K.placeholder(shape=(None, ), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
示例6: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None,), dtype='int32')
y = K.placeholder(shape=(None,), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
示例7: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None, ), dtype='int32')
y = K.placeholder(shape=(None, ), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
# dueling network's Q Value is sum of advantages and state value
示例8: loss_function
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def loss_function(self, y_true, y_pred):
y, u, a, b = _keras_split(y_true, y_pred)
if self.kind == 'discrete':
loglikelihoods = loglik_discrete(y, u, a, b)
elif self.kind == 'continuous':
loglikelihoods = loglik_continuous(y, u, a, b)
if self.clip_prob is not None:
loglikelihoods = K.clip(loglikelihoods,
log(self.clip_prob), log(1 - self.clip_prob))
if self.reduce_loss:
loss = -1.0 * K.mean(loglikelihoods, axis=-1)
else:
loss = -loglikelihoods
return loss
# For backwards-compatibility
示例9: angle_error
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def angle_error(gt, pred):
"""
Average angular error computed by cosine difference
:param gt: list of ground truth label
:param pred: list of predicted label
:return: Average angular error
"""
vec_gt = angles2vector(gt)
vec_pred = angles2vector(pred)
x = K.np.multiply(vec_gt[:, 0], vec_pred[:, 0])
y = K.np.multiply(vec_gt[:, 1], vec_pred[:, 1])
z = K.np.multiply(vec_gt[:, 2], vec_pred[:, 2])
dif = K.np.sum([x, y, z], axis=0) / (tf.norm(vec_gt, axis=1) * tf.norm(vec_pred, axis=1))
clipped_dif = K.clip(dif, np.float(-1.0), np.float(1.0))
loss = (tf.acos(clipped_dif) * 180) / np.pi
return K.mean(loss, axis=-1)
示例10: numpy_angle_error
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def numpy_angle_error(gt, pred):
"""
Numpy version of angle_error. Average angular error computed by cosine difference
:param gt: list of ground truth label
:param pred: list of predicted label
:return: Average angular error
"""
vec_gt = numpy_angles2vector(gt)
vec_pred = numpy_angles2vector(pred)
x = np.multiply(vec_gt[:, 0], vec_pred[:, 0])
y = np.multiply(vec_gt[:, 1], vec_pred[:, 1])
z = np.multiply(vec_gt[:, 2], vec_pred[:, 2])
dif = np.sum([x, y, z], axis=0) / (np.linalg.norm(vec_gt, axis=1) * np.linalg.norm(vec_pred, axis=1))
clipped_dif = np.clip(dif, np.float(-1.0), np.float(1.0))
loss = (np.arccos(clipped_dif) * 180) / np.pi
return np.mean(loss, axis=-1)
示例11: crossentropy_reed_wrap
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def crossentropy_reed_wrap(_beta):
def crossentropy_reed_core(y_true, y_pred):
"""
This loss function is proposed in:
Reed et al. "Training Deep Neural Networks on Noisy Labels with Bootstrapping", 2014
:param y_true:
:param y_pred:
:return:
"""
# hyper param
print(_beta)
y_pred = K.clip(y_pred, K.epsilon(), 1)
# (1) dynamically update the targets based on the current state of the model: bootstrapped target tensor
# use predicted class proba directly to generate regression targets
y_true_update = _beta * y_true + (1 - _beta) * y_pred
# (2) compute loss as always
_loss = -K.sum(y_true_update * K.log(y_pred), axis=-1)
return _loss
return crossentropy_reed_core
示例12: ssim
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def ssim(x, y):
c1 = 0.01 ** 2
c2 = 0.03 ** 2
mu_x = K.mean(x, axis=-1)
mu_y = K.mean(y, axis=-1)
sigma_x = K.mean(x ** 2, axis=-1) - mu_x ** 2
sigma_y = K.mean(y ** 2, axis=-1) - mu_y ** 2
sigma_xy = K.mean(x * y, axis=-1) - mu_x * mu_y
ssim_n = (2 * mu_x * mu_y + c1) * (2 * sigma_xy + c2)
ssim_d = (mu_x ** 2 + mu_y ** 2 + c1) * (sigma_x + sigma_y + c2)
ssim_out = ssim_n / ssim_d
return K.clip((1 - ssim_out) / 2, 0, 1)
示例13: softmax_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def softmax_loss(y_true, y_pred):
"""Compute cross entropy loss aka softmax 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
softmax_loss: Softmax loss, tensor of shape (?, num_boxes).
"""
eps = K.epsilon()
y_pred = K.clip(y_pred, eps, 1. - eps)
softmax_loss = -tf.reduce_sum(y_true * tf.log(y_pred), axis=-1)
return softmax_loss
示例14: focal_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [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
示例15: f1
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clip [as 别名]
def f1(y_true, y_pred):
def precision(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def recall(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
precision = precision(y_true, y_pred)
recall = recall(y_true, y_pred)
return 2 * ((precision * recall) / (precision + recall + K.epsilon()))
# Inception_ResNet_V2 model define