本文整理汇总了Python中keras.backend.argmax方法的典型用法代码示例。如果您正苦于以下问题:Python backend.argmax方法的具体用法?Python backend.argmax怎么用?Python backend.argmax使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.argmax方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: yolo_filter_boxes
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6):
# Compute box scores
box_scores = box_confidence * box_class_probs
# Find the box_classes thanks to the max box_scores, keep track of the corresponding score
box_classes = K.argmax(box_scores, axis=-1)
box_class_scores = K.max(box_scores, axis=-1, keepdims=False)
# Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the
# same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold)
filtering_mask = box_class_scores >= threshold
# Apply the mask to scores, boxes and classes
scores = tf.boolean_mask(box_class_scores, filtering_mask)
boxes = tf.boolean_mask(boxes, filtering_mask)
classes = tf.boolean_mask(box_classes, filtering_mask)
return scores, boxes, classes
示例2: compute_error_matrix
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def compute_error_matrix(y_true, y_pred):
"""Compute Confusion matrix (a.k.a. error matrix).
a predicted
c 0 1 2
t 0 [[ 5, 3, 0],
u 1 [ 2, 3, 1],
a 2 [ 0, 2, 11]]
l
Note true positves are in diagonal
"""
# Find channel axis given backend
if K.image_data_format() == 'channels_last':
ax_chn = 3
else:
ax_chn = 1
classes = y_true.shape[ax_chn]
confusion = get_confusion(K.argmax(y_true, axis=ax_chn).flatten(),
K.argmax(y_pred, axis=ax_chn).flatten(),
classes)
return confusion
示例3: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def call(self, inputs, **kwargs):
if type(inputs) is list: # true label is provided with shape = [None, n_classes], i.e. one-hot code.
assert len(inputs) == 2
inputs, mask = inputs
else: # if no true label, mask by the max length of capsules. Mainly used for prediction
# compute lengths of capsules
x = K.sqrt(K.sum(K.square(inputs), -1))
# generate the mask which is a one-hot code.
# mask.shape=[None, n_classes]=[None, num_capsule]
mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1])
# inputs.shape=[None, num_capsule, dim_capsule]
# mask.shape=[None, num_capsule]
# masked.shape=[None, num_capsule * dim_capsule]
masked = K.batch_flatten(inputs * K.expand_dims(mask, -1))
return masked
示例4: labelembed_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [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
示例5: count
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def count(audio, model, scaler):
# compute STFT
X = np.abs(librosa.stft(audio, n_fft=400, hop_length=160)).T
# apply global (featurewise) standardization to mean1, var0
X = scaler.transform(X)
# cut to input shape length (500 frames x 201 STFT bins)
X = X[:500, :]
# apply l2 normalization
Theta = np.linalg.norm(X, axis=1) + eps
X /= np.mean(Theta)
# add sample dimension
X = X[np.newaxis, ...]
if len(model.input_shape) == 4:
X = X[:, np.newaxis, ...]
ys = model.predict(X, verbose=0)
return np.argmax(ys, axis=1)[0]
示例6: f1_score_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def f1_score_keras(y_true, y_pred):
#convert probas to 0,1
y_ppred = K.zeros_like(y_true)
y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), 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.T.switch(K.T.eq(pred_cnt, 0), 0, y_true_pred/pred_cnt)
#recall for each class
recall = K.T.switch(K.T.eq(gold_cnt, 0), 0, y_true_pred/gold_cnt)
#f1 for each class
f1_class = K.T.switch(K.T.eq(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall))
#return average f1 score over all classes
return K.mean(f1_class)
示例7: f1_score_taskB
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [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
示例8: f1_score_semeval
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def f1_score_semeval(y_true, y_pred):
# convert probas to 0,1
y_ppred = K.zeros_like(y_true)
y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), 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.T.switch(K.T.eq(pred_cnt, 0), 0, y_true_pred / pred_cnt)
# recall for each class
recall = K.T.switch(K.T.eq(gold_cnt, 0), 0, y_true_pred / gold_cnt)
# f1 for each class
f1_class = K.T.switch(K.T.eq(precision + recall, 0), 0, 2 * (precision * recall) / (precision + recall))
#return average f1 score over all classes
return (f1_class[0] + f1_class[2])/2.0
示例9: precision_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [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: f1_score_task3
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def f1_score_task3(y_true, y_pred):
#convert probas to 0,1
y_ppred = K.zeros_like(y_true)
y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), 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.T.switch(K.T.eq(pred_cnt, 0), 0, y_true_pred/pred_cnt)
#recall for each class
recall = K.T.switch(K.T.eq(gold_cnt, 0), 0, y_true_pred/gold_cnt)
#f1 for each class
f1_class = K.T.switch(K.T.eq(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall))
#return average f1 score over all classes
return f1_class[1]
示例11: f1_score_taskB
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [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
示例12: precision_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [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)
示例13: get_sample_weight
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def get_sample_weight(label, whole_set):
if label.ndim < 3: # in case output_size==1
return None
ret = []
for i in label:
ret.append([])
tag = False
for j in i:
cha = whole_set[np.argmax(j)]
weight = 0
if cha == 'empty' and tag == False:
weight = 1 # TODO
tag = True
if cha != 'empty':
weight = 1
ret[-1].append(weight)
ret = np.asarray(ret)
return ret
示例14: _get_accuracy
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def _get_accuracy(y_true, y_pred, mask, sparse_target=False):
"""
:param y_true:
:param y_pred:
:param mask:
:param sparse_target:
:return:
"""
y_pred = K.argmax(y_pred, -1)
if sparse_target:
y_true = K.cast(y_true[:, :, 0], K.dtype(y_pred))
else:
y_true = K.argmax(y_true, -1)
judge = K.cast(K.equal(y_pred, y_true), K.floatx())
if mask is None:
return K.mean(judge)
else:
mask = K.cast(mask, K.floatx())
return K.sum(judge * mask) / K.sum(mask)
示例15: augmented_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import argmax [as 别名]
def augmented_loss(self, y_true, y_pred):
_y_pred = Activation("softmax")(y_pred)
loss = K.categorical_crossentropy(_y_pred, y_true)
# y is (batch x seq x vocab)
y_indexes = K.argmax(y_true, axis=2) # turn one hot to index. (batch x seq)
y_vectors = self.embedding(y_indexes) # lookup the vector (batch x seq x vector_length)
#v_length = self.setting.vector_length
#y_vectors = K.reshape(y_vectors, (-1, v_length))
#y_t = K.map_fn(lambda v: K.dot(self.embedding.embeddings, K.reshape(v, (-1, 1))), y_vectors)
#y_t = K.squeeze(y_t, axis=2) # unknown but necessary operation
#y_t = K.reshape(y_t, (-1, self.sequence_size, self.vocab_size))
# vector x embedding dot products (batch x seq x vocab)
y_t = tf.tensordot(y_vectors, K.transpose(self.embedding.embeddings), 1)
y_t = K.reshape(y_t, (-1, self.sequence_size, self.vocab_size)) # explicitly set shape
y_t = K.softmax(y_t / self.temperature)
_y_pred_t = Activation("softmax")(y_pred / self.temperature)
aug_loss = kullback_leibler_divergence(y_t, _y_pred_t)
loss += (self.gamma * self.temperature) * aug_loss
return loss