Ejemplo n.º 1
0
def single_class_accuracy(y_true, y_pred):
    class_id_true = K.argmax(y_true, axis=-1)
    class_id_preds = K.argmax(y_pred, axis=-1)
    # Replace class_id_preds with class_id_true for recall here
    accuracy_mask = K.cast(K.equal(class_id_preds, 1), 'int32')
    class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask
    class_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1)
    return class_acc
Ejemplo n.º 2
0
def masked_accuracy(y_true, y_pred):
    max_args = argmax(y_true)
    mask = cast(not_equal(max_args, zeros_like(max_args)), dtype='float32')
    points = switch(
        mask,
        cast(equal(argmax(y_true, -1), argmax(y_pred, -1)), dtype='float32'),
        zeros_like(mask, dtype=floatx()))
    return sum(points) / cast(sum(mask), dtype='float32')
Ejemplo n.º 3
0
 def masked_acc(y_true, y_pred):
     mask = tf.cast(tf.not_equal(tf.reduce_sum(y_true, -1), 0), 'float32')
     eq = K.cast(
         K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)),
         'float32')
     eq = tf.reduce_sum(eq * mask, -1) / tf.reduce_sum(mask, -1)
     eq = K.mean(eq)
     return eq
Ejemplo n.º 4
0
 def haraka_post_corrections(self, in_out):
     """
     Change any obviously wrong haraka marks according to the character and its context.
     :param in_out: input layer and prediction layers outputs.
     :return: corrected predictions.
     """
     inputs, pred_haraka, pred_shadda = in_out
     if not self.rules_enabled:
         return pred_haraka
     char_index = K.argmax(inputs[:, -1], axis=-1)
     # Force the correct haraka on some letters
     forced_diac_chars = {CHAR2INDEX['إ']: 3}
     for f_diac_char, f_diac in forced_diac_chars.items():
         mask = K.reshape(K.cast(K.not_equal(char_index, f_diac_char), 'float32'), (-1, 1))
         pred_haraka = mask * pred_haraka + (1 - mask) * K.one_hot(f_diac, K.int_shape(pred_haraka)[-1])
     # Force the correct haraka before some letters
     f_prev_diac_chars = {CHAR2INDEX['ى']: 1, CHAR2INDEX['ة']: 1}
     prev_char_index = K.argmax(inputs[:, -2], axis=-1)
     for fd_char, f_diac in f_prev_diac_chars.items():
         mask = K.cast(K.not_equal(char_index[1:], fd_char), 'float32')
         mask = K.reshape(K.concatenate([mask, K.ones((1,))], axis=0), (-1, 1))
         pred_haraka = pred_haraka * mask + (1 - mask) * K.one_hot(f_diac, K.int_shape(pred_haraka)[-1])
     # Allow only Fatha, Fathatan, or nothing before ا if it is in the end of the word
     mask = K.reshape(K.concatenate([K.clip(
         K.cast(K.not_equal(char_index[1:-1], CHAR2INDEX['ا']), 'float32') +
         K.cast(K.not_equal(char_index[2:], CHAR2INDEX[' ']), 'float32'), 0, 1), K.ones((2,))], axis=0), (-1, 1))
     pred_haraka = mask * pred_haraka + (1 - mask) * K.constant([1, 1, 0, 0, 0, 1, 0, 0], shape=(1, 8)) * pred_haraka
     # Force Fatha before ا if it is not in the end of the word
     mask = K.reshape(K.concatenate([K.clip(
         K.cast(K.not_equal(char_index[1:-1], CHAR2INDEX['ا']), 'float32') +
         K.cast(K.equal(char_index[2:], CHAR2INDEX[' ']), 'float32'), 0, 1), K.ones((2,))], axis=0), (-1, 1))
     pred_haraka = mask * pred_haraka + (1 - mask) * K.one_hot(1, K.int_shape(pred_haraka)[-1])
     # Force no sukun and tanween at the beginning of the word
     mask = K.reshape(
         K.concatenate([K.zeros((1,)), K.cast(K.not_equal(prev_char_index[1:], CHAR2INDEX[' ']), 'float32')],
                       axis=0), (-1, 1))
     pred_haraka = mask * pred_haraka + (1 - mask) * K.constant([1, 1, 1, 1, 0, 0, 0, 0], shape=(1, 8)) * pred_haraka
     # Allow tanween only at the end of the word
     mask = K.reshape(K.concatenate([K.cast(K.not_equal(char_index[1:], CHAR2INDEX[' ']), 'float32'), K.zeros((1,))],
                                    axis=0), (-1, 1))
     pred_haraka = mask * K.constant([1, 1, 1, 1, 1, 0, 0, 0], shape=(1, 8)) * pred_haraka + (1 - mask) * pred_haraka
     # Prohibit Fathatan on most letters
     mask = K.reshape(K.concatenate([K.clip(
         K.cast(K.not_equal(char_index[1:], CHAR2INDEX[' ']), 'float32') +
         K.cast(K.not_equal(char_index[:-1], CHAR2INDEX['ء']), 'float32'), 0, 1), K.ones((1,))], axis=0), (-1, 1))
     mask *= K.reshape(K.cast(K.not_equal(char_index, CHAR2INDEX['ة']), 'float32'), (-1, 1))
     mask *= K.reshape(K.concatenate([K.clip(
         K.cast(K.not_equal(char_index[1:-1], CHAR2INDEX['ا']), 'float32') +
         K.cast(K.not_equal(char_index[2:], CHAR2INDEX[' ']), 'float32'), 0, 1), K.ones((2,))], axis=0), (-1, 1))
     pred_haraka = mask * K.constant([1, 1, 1, 1, 1, 0, 1, 1], shape=(1, 8)) * pred_haraka + (1 - mask) * pred_haraka
     # Drop haraka from the forbidden characters
     forbidden_chars = [CHAR2INDEX[' '], CHAR2INDEX['0'], CHAR2INDEX['آ'], CHAR2INDEX['ى'], CHAR2INDEX['ا']]
     mask = K.cast(K.not_equal(char_index, forbidden_chars[0]), 'float32')
     for forbidden_char in forbidden_chars[1:]:
         mask *= K.cast(K.not_equal(char_index, forbidden_char), 'float32')
     mask = K.reshape(mask, (-1, 1))
     pred_haraka = mask * pred_haraka + (1 - mask) * K.one_hot(0, K.int_shape(pred_haraka)[-1])
     return pred_haraka
Ejemplo n.º 5
0
def permute_ypred_for_matching(y_true, y_pred):

    # Permutation for the predictions and targets
    # Applied before the detection loss is computed

    alpha = y_true[:, :, 0]
    x = y_true[:, :, 1]
    p = y_pred[:, :, 1]
    nb_pred = args.numbertimestamps

    D = K.abs(
        tf.tile(K.expand_dims(x, axis=-1), (1, 1, nb_pred)) -
        tf.tile(K.expand_dims(p, axis=-2), (1, nb_pred, 1)))
    D1 = 1 - D
    Permut = 0 * D

    alpha_filter = tf.tile(K.expand_dims(alpha, -1), (1, 1, nb_pred))

    v_filter = alpha_filter
    h_filter = 0 * v_filter + 1
    D2 = v_filter * D1

    for i in range(nb_pred):
        D2 = v_filter * D2
        D2 = h_filter * D2
        A = tf.one_hot(K.argmax(D2, axis=-1), nb_pred)
        B = v_filter * A * D2
        C = tf.transpose(tf.one_hot(K.argmax(B, axis=-2), nb_pred),
                         perm=[0, 2, 1])
        E = v_filter * A * C
        Permut = Permut + E
        v_filter = (1 - K.sum(Permut, axis=-1)) * alpha
        v_filter = tf.tile(K.expand_dims(v_filter, -1), (1, 1, nb_pred))
        h_filter = 1 - K.sum(Permut, axis=-2)
        h_filter = tf.tile(K.expand_dims(h_filter, -2), (1, nb_pred, 1))

    v_filter = 1 - alpha_filter
    D2 = v_filter * D1
    D2 = h_filter * D2

    for i in range(nb_pred):
        D2 = v_filter * D2
        D2 = h_filter * D2
        A = tf.one_hot(K.argmax(D2, axis=-1), nb_pred)
        B = v_filter * A * D2
        C = tf.transpose(tf.one_hot(K.argmax(B, axis=-2), nb_pred),
                         perm=[0, 2, 1])
        E = v_filter * A * C
        Permut = Permut + E
        v_filter = (1 - K.sum(Permut, axis=-1)) * (1 - alpha)
        v_filter = tf.tile(K.expand_dims(v_filter, -1), (1, 1, nb_pred))
        h_filter = 1 - K.sum(Permut, axis=-2)
        h_filter = tf.tile(K.expand_dims(h_filter, -2), (1, nb_pred, 1))

    permutation = K.argmax(Permut, axis=-1)
    permuted = tf.gather(y_pred, permutation, batch_dims=1)

    return permuted
Ejemplo n.º 6
0
    def dice(self, y_true, y_pred):
        """
        compute dice for given Tensors

        """
        if self.crop_indices is not None:
            y_true = utils.batch_gather(y_true, self.crop_indices)
            y_pred = utils.batch_gather(y_pred, self.crop_indices)

        if self.input_type == 'prob':
            # We assume that y_true is probabilistic, but just in case:
            y_true /= K.sum(y_true, axis=-1, keepdims=True)
            y_true = K.clip(y_true, K.epsilon(), 1)

            # make sure pred is a probability
            y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
            y_pred = K.clip(y_pred, K.epsilon(), 1)

        # Prepare the volumes to operate on
        # If we're doing 'hard' Dice, then we will prepare one-hot-based matrices of size
        # [batch_size, nb_voxels, nb_labels], where for each voxel in each batch entry,
        # the entries are either 0 or 1
        if self.dice_type == 'hard':

            # if given predicted probability, transform to "hard max""
            if self.input_type == 'prob':
                if self.approx_hard_max:
                    y_pred_op = _hard_max(y_pred, axis=-1)
                    y_true_op = _hard_max(y_true, axis=-1)
                else:
                    y_pred_op = _label_to_one_hot(K.argmax(y_pred, axis=-1), self.nb_labels)
                    y_true_op = _label_to_one_hot(K.argmax(y_true, axis=-1), self.nb_labels)

            # if given predicted label, transform to one hot notation
            else:
                assert self.input_type == 'max_label'
                y_pred_op = _label_to_one_hot(y_pred, self.nb_labels)
                y_true_op = _label_to_one_hot(y_true, self.nb_labels)

        # If we're doing soft Dice, require prob output, and the data already is as we need it
        # [batch_size, nb_voxels, nb_labels]
        else:
            assert self.input_type == 'prob', "cannot do soft dice with max_label input"
            y_pred_op = y_pred
            y_true_op = y_true

        # reshape to [batch_size, nb_voxels, nb_labels]
        batch_size = K.shape(y_true)[0]
        y_pred_op = K.reshape(y_pred_op, [batch_size, -1, K.shape(y_true)[-1]])
        y_true_op = K.reshape(y_true_op, [batch_size, -1, K.shape(y_true)[-1]])

        # compute dice for each entry in batch.
        # dice will now be [batch_size, nb_labels]
        top = 2 * K.sum(y_true_op * y_pred_op, 1)
        bottom = K.sum(K.square(y_true_op), 1) + K.sum(K.square(y_pred_op), 1)
        # make sure we have no 0s on the bottom. K.epsilon()
        bottom = K.maximum(bottom, self.area_reg)
        return top / bottom
Ejemplo n.º 7
0
    def update_state(self, y_true, y_pred, sample_weight=None):

        y_true = K.argmax(y_true, axis=-1)
        y_pred = K.argmax(y_pred, axis=-1)
        y_true = K.flatten(y_true)

        true_poss = K.sum(K.cast((K.equal(y_true, y_pred)), dtype=tf.float32))

        self.cat_true_positives.assign_add(true_poss)
Ejemplo n.º 8
0
 def accuracy(self, y_true, y_pred):  # 训练过程中显示逐帧准确率的函数,排除了mask的影响
     mask = 1 - y_true[:, :, -1] if self.ignore_last_label else None
     y_true, y_pred = y_true[:, :, :self.num_labels], y_pred[:, :, :self.num_labels]
     isequal = K.equal(K.argmax(y_true, 2), K.argmax(y_pred, 2))
     isequal = K.cast(isequal, 'float32')
     if mask == None:
         return K.mean(isequal)
     else:
         return K.sum(isequal * mask) / K.sum(mask)
Ejemplo n.º 9
0
def target_first(y_true, y_pred):
    #for use with find_scale
    if type(y_true) == type(np.array([])):
        return np.argmax(y_true, axis=-1) == np.argmax(y_pred, axis=-1)
    else:
        return K.mean(
            K.cast(
                K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)),
                K.floatx()))
Ejemplo n.º 10
0
    def custom_accuracy(y_true, y_pred):
        y_true_class = K.argmax(y_true, axis=-1)
        y_pred_class = K.argmax(y_pred, axis=-1)

        ignore_mask = K.cast(K.not_equal(y_pred_class, to_ignore), 'int32')
        matches = K.cast(K.equal(y_true_class, y_pred_class),
                         'int32') * ignore_mask
        accuracy = K.sum(matches) / K.maximum(K.sum(ignore_mask), 1)
        return accuracy
Ejemplo n.º 11
0
def iou(y_true, y_pred, label: int):
    y_true = K.cast(K.equal(K.argmax(y_true), label), K.floatx())
    y_pred = K.cast(K.equal(K.argmax(y_pred), label), K.floatx())

    intersection = K.sum(y_true * y_pred)

    union = K.sum(y_true) + K.sum(y_pred) - intersection

    return K.switch(K.equal(union, 0), 1.0, intersection / union)
Ejemplo n.º 12
0
    def acc(y_true, y_pred):
        targ = K.argmax(y_true, axis=-1)
        pred = K.argmax(y_pred, axis=-1)
        correct = K.cast(K.equal(targ, pred), dtype='float32')

        Pmax = K.cast(K.max(y_pred, axis=-1), dtype='float32')
        Pmax = Pmax * correct
        mask = K.cast(K.greater(Pmax, custom_acc), dtype='float32')

        return K.mean(mask)
Ejemplo n.º 13
0
 def prec(y_true, y_pred):
     class_id_true = K.argmax(y_true, axis=-1)
     class_id_pred = K.argmax(y_pred, axis=-1)
     precision_mask = K.cast(K.equal(class_id_pred, interesting_class_id),
                             'int32')
     class_prec_tensor = K.cast(K.equal(class_id_true, class_id_pred),
                                'int32') * precision_mask
     class_prec = K.cast(K.sum(class_prec_tensor), 'float32') / K.cast(
         K.maximum(K.sum(precision_mask), 1), 'float32')
     return class_prec
Ejemplo n.º 14
0
def masked_categorical_accuracy(y_true, y_pred, mask_id):
    true_ids = bk.argmax(y_true, axis=-1)
    pred_ids = bk.argmax(y_pred, axis=-1)
    maskBool = bk.not_equal(true_ids, mask_id)
    maskInt64 = bk.cast(maskBool, 'int64')
    maskFloatX = bk.cast(maskBool, bk.floatx())

    count = bk.sum(maskFloatX)
    equals = bk.equal(true_ids * maskInt64, pred_ids * maskInt64)
    sum = bk.sum(bk.cast(equals, bk.floatx()) * maskFloatX)
    return sum / count
Ejemplo n.º 15
0
def weighted_acc(y_true, y_pred):
    y_true_digit = K.argmax(y_true, axis=-1)
    y_pred_digit = K.argmax(y_pred, axis=-1)
    mask = tf.subtract(K.constant(1.0, dtype=tf.float32), y_pred[:, :, :, -1])
    true_mat = K.cast(K.equal(y_true_digit, y_pred_digit), K.floatx())

    tp = K.sum(K.minimum(true_mat, mask))
    fp = K.sum(K.minimum(1 - true_mat, mask))
    fn = K.sum(K.minimum(1 - true_mat, mask))

    return tp / (tp + fp + fn + 1e-12)
Ejemplo n.º 16
0
def acc(y_true, y_pred):
  # both are of shape ( _, Ty, DEOCDER_VOCAB_SIZE )
  targ = K.argmax(y_true, axis=-1)
  pred = K.argmax(y_pred, axis=-1)
  correct = K.cast(  K.equal(targ,pred), dtype='float32') #cast bool tensor to float

  # 0 is padding, don't include those- mask is tensor representing non-pad value
  mask = K.cast(K.greater(targ, 0), dtype='float32') #cast bool-tensor to float 
  n_correct = K.sum(mask * correct) #
  n_total = K.sum(mask)
  return n_correct / n_total
Ejemplo n.º 17
0
def comboLoss(true, pred):
    atrue = K.argmax(true, axis=-1)
    apred = K.argmax(pred, axis=-1)
    maskas = tf.where((tf.gather(mas, atrue) == tf.gather(mas, apred)) &
                      (tf.gather(mna, atrue) != tf.gather(mna, apred)))
    maskna = tf.where((tf.gather(mna, atrue) == tf.gather(mna, apred)) &
                      (tf.gather(mas, atrue) != tf.gather(mas, apred)))
    loss = categorical_crossentropy(true, pred)
    tf.tensor_scatter_nd_update(loss, maskas, tf.gather(loss, maskas) * 2. / 3.)
    tf.tensor_scatter_nd_update(loss, maskna, tf.gather(loss, maskna) * 6. / 7.)
    return .25 * loss * (1. - K.exp(-loss)) ** 2.
Ejemplo n.º 18
0
    def wrapper(y_true, y_pred):
        if (usesoftmax):
            y_pred = tf.keras.activations.softmax(y_pred)
        y_pred = backend.clip(y_pred, _EPSILON, 1.0 - _EPSILON)
        """ here all calculations will be based on the class greater than 0, except accuracy"""
        avgIOU = 0.0

        for i in range(batch_size):
            numUnion = 1.0
            recall = 0.0
            numClass = 0.0
            IOU = 0.0
            mask = backend.argmax(y_true[i], -1)
            pred = backend.argmax(y_pred[i], -1)

            for c in np.arange(1, num_classes, 1):
                msk_equal = backend.cast(backend.equal(mask, c),
                                         dtype='float32')

                masks_sum = backend.sum(msk_equal)

                predictions_sum = backend.sum(
                    backend.cast(backend.equal(pred, c), 'float32'))

                numTrue = backend.sum(
                    backend.cast(backend.equal(pred, c), 'float32') *
                    backend.cast(backend.equal(mask, c), 'float32'))
                unionSize = masks_sum + predictions_sum - numTrue
                maskhaslabel = backend.greater(masks_sum, 0)
                predhaslabel = backend.greater(predictions_sum, 0)

                predormaskexistlabel = backend.any(backend.stack(
                    [maskhaslabel, predhaslabel], axis=0),
                                                   axis=0)

                IOU = backend.switch(predormaskexistlabel,
                                     lambda: IOU + numTrue / unionSize,
                                     lambda: IOU)
                numUnion = backend.switch(predormaskexistlabel,
                                          lambda: numUnion + 1,
                                          lambda: numUnion)
                recall = backend.switch(maskhaslabel,
                                        lambda: recall + numTrue / masks_sum,
                                        lambda: recall)
                numClass = backend.switch(maskhaslabel, lambda: numClass + 1,
                                          lambda: numClass)
            IOU = IOU / numUnion
            avgIOU = avgIOU + IOU
        avgIOU = avgIOU / batch_size
        iou_loss = 1.0 - avgIOU
        # print(np.shape(y_true), np.shape(y_pred))
        main_loss = backend.mean(weighted_loss_fn(y_true, y_pred))
        # dice_loss = soft_dice_loss(y_true, y_pred)
        return main_loss + 0.1 * iou_loss
Ejemplo n.º 19
0
  def iou(y_true, y_pred):

    y_pred_softmax = K.argmax(y_pred)
    y_pred_softmax = y_pred_softmax == channel_id
    y_pred_softmax = K.cast(y_pred_softmax, K.floatx())

    y_true_softmax = K.argmax(y_true)
    y_true_softmax = y_true_softmax == channel_id
    y_true_softmax = K.cast(y_true_softmax, K.floatx())

    return iou_coef_softmax(y_true_softmax, y_pred_softmax, smooth)
Ejemplo n.º 20
0
 def acc(y_true, y_pred):
     #y_true = tf.Print(y_true, ['y_true', tf.shape(y_true), y_true], summarize=32)
     #y_pred = tf.Print(y_pred, ['y_pred', tf.shape(y_pred), y_pred], summarize=32)
     corr = K.cast(
         K.equal(K.cast(K.argmax(y_true, axis=-1), 'int32'),
                 K.cast(K.argmax(y_pred, axis=-1), 'int32')),
         'float32')
     #corr = tf.Print(corr, ['y_corr', tf.shape(corr), corr], summarize=32)
     mean_corr = K.mean(corr)
     #mean_corr = tf.Print(mean_corr, ['mean_corr', tf.shape(mean_corr), mean_corr], summarize=32)
     return mean_corr
Ejemplo n.º 21
0
def comboAcc(true, pred):
    atrue = K.argmax(true, axis=-1)
    apred = K.argmax(pred, axis=-1)
    maskas = (tf.gather(mas, atrue) == tf.gather(mas, apred)) & (
        tf.gather(mna, atrue) != tf.gather(mna, apred))
    maskna = (tf.gather(mna, atrue) == tf.gather(mna, apred)) & (
        tf.gather(mas, atrue) != tf.gather(mas, apred))
    acc = K.mean(tf.cast(atrue == apred, tf.float32))
    acc += K.mean(tf.cast(maskas, tf.float32)) / 3.
    acc += K.mean(tf.cast(maskna, tf.float32)) / 7.
    return acc
Ejemplo n.º 22
0
def calculate_new_loss_variable(y_true, y_pred):
    y = K.argmax(
        y_true,
        axis=1)  # Array of all the indexes of max values for true values
    y_hat = K.argmax(
        y_pred,
        axis=1)  # Array of all the indexes of max values for predictions

    tmp = K.cast(K.sum(K.abs(y - y_hat)), dtype='float32')
    loss = tmp * 0.001  # [0, 0.506]
    return loss
Ejemplo n.º 23
0
def get_2_max(y):
    best = K.argmax(y, axis=-1)
    y_min_best = y - K.one_hot(best, K.shape(y)[-1])
    second_best = K.argmax(y_min_best, axis=-1)

    best_value = K.max(y, axis=-1)
    second_best_value = K.max(y_min_best, axis=-1)
    return K.stack([
        K.stack([K.cast(best, 'float32'), best_value]),
        K.stack([K.cast(second_best, 'float32'), second_best_value])
    ])
Ejemplo n.º 24
0
def _get_accuracy(y_true, y_pred, mask, sparse_target=False):
  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)
Ejemplo n.º 25
0
        def acc_igm1(y_true, y_pred):
            """
            custom accuracy function which only considers known-labels(not -1)
            """
            known = K.equal(
                K.max(y_true, axis=-1),
                1)  # if max value == 0, it means -1 (unknown) label.
            correct = K.equal(K.argmax(y_true, axis=-1),
                              K.argmax(y_pred, axis=-1))
            true = K.all(K.stack([correct, known], axis=0), axis=0)

            return K.sum(K.cast(true, 'int32')) / K.sum(K.cast(known, 'int32'))
Ejemplo n.º 26
0
	def _quadratic_weighted_kappa(y_true, y_pred):
		"""
		Compute Quadratic Weighted Kappa metric.
		:param y_true: true labels.
		:param y_pred: predicted labels.
		:return: qwk value.
		"""
		y_pred = K.argmax(y_pred, 1, output_type=K.int32)
		y_true = K.argmax(y_true, 1, output_type=K.int32)
		conf_mat = K.confusion_matrix(y_true, y_pred, num_classes=num_classes, dtype=K.floatx())

		return quadratic_weighted_kappa_cm(conf_mat, num_classes, cost_matrix)
Ejemplo n.º 27
0
def exact_matched_accuracy(y_true, y_pred, mask_id):
    true_ids = bk.argmax(y_true, axis=-1)
    pred_ids = bk.argmax(y_pred, axis=-1)

    maskBool = bk.not_equal(true_ids, mask_id)
    maskInt64 = bk.cast(maskBool, 'int64')

    diff = (true_ids - pred_ids) * maskInt64
    matches = bk.cast(bk.not_equal(diff, bk.zeros_like(diff)), 'int64')
    matches = bk.sum(matches, axis=-1)
    matches = bk.cast(bk.equal(matches, bk.zeros_like(matches)), bk.floatx())
    return bk.mean(matches)
Ejemplo n.º 28
0
def accYanovichK(y_true, y_pred):
    """
        Not exactly Yanovich acc but close
    """
    y_true_class = K.argmax(y_true, axis=-1)
    y_pred_class = K.argmax(y_pred, axis=-1)

    ignore_mask = K.cast(K.not_equal(y_true_class, 0), 'int32')
    matches = K.cast(K.equal(y_true_class, y_pred_class),
                     'int32') * ignore_mask
    accuracy = K.sum(matches) / K.maximum(K.sum(ignore_mask), 1)
    return accuracy
Ejemplo n.º 29
0
    def sentence_accuracy(y_true, y_pred):
        y_true_class = K.argmax(y_true, axis=-1)
        y_pred_class = K.argmax(y_pred, axis=-1)

        ignore_mask = K.cast(K.equal(y_true_class, to_ignore), 'int32')
        matches = K.cast(K.equal(y_true_class, y_pred_class), 'int32')
        matches = K.any(K.stack([matches, ignore_mask], axis=0),
                        axis=0)  # This is logic OR
        accuracy = K.sum(K.cast(K.all(matches, axis=1), 'int32')) / K.sum(
            K.cast(K.any(matches, axis=1), 'int32'))

        return accuracy
Ejemplo n.º 30
0
def sparse_accuracy_ignoring_last_label(y_true, y_pred):
    nb_classes = K.int_shape(y_pred)[-1]
    y_pred = K.reshape(y_pred, (-1, nb_classes))

    y_true = K.one_hot(tfc.to_int32(K.flatten(y_true)), nb_classes + 1)
    unpacked = tf.unstack(y_true, axis=-1)
    legal_labels = ~tf.cast(unpacked[-1], tf.bool)
    y_true = tf.stack(unpacked[:-1], axis=-1)

    return K.sum(
        tfc.to_float(legal_labels & K.equal(K.argmax(
            y_true, axis=-1), K.argmax(y_pred, axis=-1)))) / K.sum(
                tfc.to_float(legal_labels))