本文整理汇总了Python中keras.backend.equal方法的典型用法代码示例。如果您正苦于以下问题:Python backend.equal方法的具体用法?Python backend.equal怎么用?Python backend.equal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.equal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rpn_class_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def rpn_class_loss_graph(rpn_match, rpn_class_logits):
"""RPN anchor classifier loss.
rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive,
-1=negative, 0=neutral anchor.
rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG.
"""
# Squeeze last dim to simplify
rpn_match = tf.squeeze(rpn_match, -1)
# Get anchor classes. Convert the -1/+1 match to 0/1 values.
anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32)
# Positive and Negative anchors contribute to the loss,
# but neutral anchors (match value = 0) don't.
indices = tf.where(K.not_equal(rpn_match, 0))
# Pick rows that contribute to the loss and filter out the rest.
rpn_class_logits = tf.gather_nd(rpn_class_logits, indices)
anchor_class = tf.gather_nd(anchor_class, indices)
# Cross entropy loss
loss = K.sparse_categorical_crossentropy(target=anchor_class,
output=rpn_class_logits,
from_logits=True)
loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0))
return loss
示例2: rpn_class_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def rpn_class_loss_graph(rpn_match, rpn_class_logits):
"""RPN anchor classifier loss.
rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive,
-1=negative, 0=neutral anchor.
rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG.
"""
# Squeeze last dim to simplify
rpn_match = tf.squeeze(rpn_match, -1)
# Get anchor classes. Convert the -1/+1 match to 0/1 values.
anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32)
# Positive and Negative anchors contribute to the loss,
# but neutral anchors (match value = 0) don't.
indices = tf.where(K.not_equal(rpn_match, 0))
# Pick rows that contribute to the loss and filter out the rest.
rpn_class_logits = tf.gather_nd(rpn_class_logits, indices)
anchor_class = tf.gather_nd(anchor_class, indices)
# Cross entropy loss
loss = K.sparse_categorical_crossentropy(target=anchor_class,
output=rpn_class_logits,
from_logits=True)
loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0))
return loss
示例3: rpn_class_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def rpn_class_loss_graph(rpn_match, rpn_class_logits):
"""RPN anchor classifier loss.
rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive,
-1=negative, 0=neutral anchor.
rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG.
"""
# Squeeze last dim to simplify
rpn_match = tf.squeeze(rpn_match, -1)
# Get anchor classes. Convert the -1/+1 match to 0/1 values.
anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32)
# Positive and Negative anchors contribute to the loss,
# but neutral anchors (match value = 0) don't.
indices = tf.where(K.not_equal(rpn_match, 0))
# Pick rows that contribute to the loss and filter out the rest.
rpn_class_logits = tf.gather_nd(rpn_class_logits, indices)
anchor_class = tf.gather_nd(anchor_class, indices)
# Crossentropy loss
loss = K.sparse_categorical_crossentropy(target=anchor_class,
output=rpn_class_logits,
from_logits=True)
loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0))
return loss
示例4: _rpn_loss_regr
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def _rpn_loss_regr(y_true, y_pred):
"""
smooth L1 loss
y_ture [1][HXWX10][3] (class,regr)
y_pred [1][HXWX10][2] (reger)
"""
sigma = 9.0
cls = y_true[0, :, 0]
regr = y_true[0, :, 1:3]
regr_keep = tf.where(K.equal(cls, 1))[:, 0]
regr_true = tf.gather(regr, regr_keep)
regr_pred = tf.gather(y_pred[0], regr_keep)
diff = tf.abs(regr_true - regr_pred)
less_one = tf.cast(tf.less(diff, 1.0 / sigma), 'float32')
loss = less_one * 0.5 * diff ** 2 * sigma + tf.abs(1 - less_one) * (diff - 0.5 / sigma)
loss = K.sum(loss, axis=1)
return K.switch(tf.size(loss) > 0, K.mean(loss), K.constant(0.0))
示例5: labelembed_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def labelembed_loss(out1, out2, tar, targets, tau = 2., alpha = 0.9, beta = 0.5, num_classes = 100):
out2_prob = K.softmax(out2)
tau2_prob = K.stop_gradient(K.softmax(out2 / tau))
soft_tar = K.stop_gradient(K.softmax(tar))
L_o1_y = K.sparse_categorical_crossentropy(output = K.softmax(out1), target = targets)
pred = K.argmax(out2, axis = -1)
mask = K.stop_gradient(K.cast(K.equal(pred, K.cast(targets, 'int64')), K.floatx()))
L_o1_emb = -cross_entropy(out1, soft_tar) # pylint: disable=invalid-unary-operand-type
L_o2_y = K.sparse_categorical_crossentropy(output = out2_prob, target = targets)
L_emb_o2 = -cross_entropy(tar, tau2_prob) * mask * (K.cast(K.shape(mask)[0], K.floatx())/(K.sum(mask)+1e-8)) # pylint: disable=invalid-unary-operand-type
L_re = K.relu(K.sum(out2_prob * K.one_hot(K.cast(targets, 'int64'), num_classes), axis = -1) - alpha)
return beta * L_o1_y + (1-beta) * L_o1_emb + L_o2_y + L_emb_o2 + L_re
示例6: f1_score_taskB
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def f1_score_taskB(y_true, y_pred):
# convert probas to 0,1
y_pred_ones = K.zeros_like(y_true)
y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1
# where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true * y_pred_ones, axis=0)
# for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
# for each class: how many are true members of said class
gold_cnt = K.sum(y_true, axis=0)
# precision for each class
precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred / pred_cnt)
# recall for each class
recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred / gold_cnt)
# f1 for each class
f1_class = K.switch(K.equal(precision + recall, 0), 0, 2 * (precision * recall) / (precision + recall))
# return average f1 score over all classes
return f1_class
示例7: precision_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def precision_keras(y_true, y_pred):
# convert probas to 0,1
y_pred_ones = K.zeros_like(y_true)
y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1
# where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true * y_pred_ones, axis=0)
# for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
# precision for each class
precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred / pred_cnt)
# return average f1 score over all classes
return K.mean(precision)
示例8: f1_score_taskB
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def f1_score_taskB(y_true, y_pred):
#convert probas to 0,1
y_pred_ones = K.zeros_like(y_true)
y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1
#where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true*y_pred_ones, axis=0)
#for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
#for each class: how many are true members of said class
gold_cnt = K.sum(y_true, axis=0)
#precision for each class
precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred/pred_cnt)
#recall for each class
recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred/gold_cnt)
#f1 for each class
f1_class = K.switch(K.equal(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall))
#return average f1 score over all classes
return f1_class
示例9: precision_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def precision_keras(y_true, y_pred):
#convert probas to 0,1
y_pred_ones = K.zeros_like(y_true)
y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1
#where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true*y_pred_ones, axis=0)
#for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
#precision for each class
precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred/pred_cnt)
#return average f1 score over all classes
return K.mean(precision)
示例10: rpn_class_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def rpn_class_loss_graph(rpn_match, rpn_class_logits):
'''RPN anchor classifier loss.
rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive,
-1=negative, 0=neutral anchor.
rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG.
'''
# Squeeze last dim to simplify
rpn_match = tf.squeeze(rpn_match, -1)
# Get anchor classes. Convert the -1/+1 match to 0/1 values.
anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32)
# Positive and Negative anchors contribute to the loss,
# but neutral anchors (match value = 0) don't.
indices = tf.where(K.not_equal(rpn_match, 0))
# Pick rows that contribute to the loss and filter out the rest.
rpn_class_logits = tf.gather_nd(rpn_class_logits, indices)
anchor_class = tf.gather_nd(anchor_class, indices)
# Cross entropy loss
loss = K.sparse_categorical_crossentropy(target=anchor_class,
output=rpn_class_logits,
from_logits=True)
loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0))
return loss
示例11: focal_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [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
示例12: customPooling
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def customPooling(x):
target = x[1]
inputs = x[0]
maskVal = 0
#getting the mask by observing the model's inputs
mask = K.equal(inputs, maskVal)
mask = K.all(mask, axis=-1, keepdims=True)
#inverting the mask for getting the valid steps for each sample
mask = 1 - K.cast(mask, K.floatx())
#summing the valid steps for each sample
stepsPerSample = K.sum(mask, axis=1, keepdims=False)
#applying the mask to the target (to make sure you are summing zeros below)
target = target * mask
#calculating the mean of the steps (using our sum of valid steps as averager)
means = K.sum(target, axis=1, keepdims=False) / stepsPerSample
return means
示例13: rpn_class_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def rpn_class_loss_graph(rpn_match, rpn_class_logits):
"""RPN anchor classifier loss.
rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive,
-1=negative, 0=neutral anchor.
rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for BG/FG.
"""
# Squeeze last dim to simplify
rpn_match = tf.squeeze(rpn_match, -1)
# Get anchor classes. Convert the -1/+1 match to 0/1 values.
anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32)
# Positive and Negative anchors contribute to the loss,
# but neutral anchors (match value = 0) don't.
indices = tf.where(K.not_equal(rpn_match, 0))
# Pick rows that contribute to the loss and filter out the rest.
rpn_class_logits = tf.gather_nd(rpn_class_logits, indices)
anchor_class = tf.gather_nd(anchor_class, indices)
# Cross entropy loss
loss = K.sparse_categorical_crossentropy(target=anchor_class,
output=rpn_class_logits,
from_logits=True)
loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0))
return loss
示例14: softmax_activation
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def softmax_activation(self, mem):
"""Softmax activation."""
# spiking_samples = k.less_equal(k.random_uniform([self.config.getint(
# 'simulation', 'batch_size'), 1]), 300 * self.dt / 1000.)
# spiking_neurons = k.T.repeat(spiking_samples, 10, axis=1)
# activ = k.T.nnet.softmax(mem)
# max_activ = k.max(activ, axis=1, keepdims=True)
# output_spikes = k.equal(activ, max_activ).astype(k.floatx())
# output_spikes = k.T.set_subtensor(output_spikes[k.equal(
# spiking_neurons, 0).nonzero()], 0.)
# new_and_reset_mem = k.T.set_subtensor(mem[spiking_neurons.nonzero()],
# 0.)
# self.add_update([(self.mem, new_and_reset_mem)])
# return output_spikes
return k.T.mul(k.less_equal(k.random_uniform(mem.shape),
k.softmax(mem)), self.v_thresh)
示例15: rpn_bbox_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import equal [as 别名]
def rpn_bbox_loss_graph(config, target_bbox, rpn_match, rpn_bbox):
"""Return the RPN bounding box loss graph.
config: the model config object.
target_bbox: [batch, max positive anchors, (dy, dx, log(dh), log(dw))].
Uses 0 padding to fill in unsed bbox deltas.
rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive,
-1=negative, 0=neutral anchor.
rpn_bbox: [batch, anchors, (dy, dx, log(dh), log(dw))]
"""
# Positive anchors contribute to the loss, but negative and
# neutral anchors (match value of 0 or -1) don't.
rpn_match = K.squeeze(rpn_match, -1)
indices = tf.where(K.equal(rpn_match, 1))
# Pick bbox deltas that contribute to the loss
rpn_bbox = tf.gather_nd(rpn_bbox, indices)
# Trim target bounding box deltas to the same length as rpn_bbox.
batch_counts = K.sum(K.cast(K.equal(rpn_match, 1), tf.int32), axis=1)
target_bbox = batch_pack_graph(target_bbox, batch_counts,
config.IMAGES_PER_GPU)
loss = smooth_l1_loss(target_bbox, rpn_bbox)
loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0))
return loss