//  [python]
def yolo_loss(
            self,
            yolo_output,
            y_true,
            anchors,
            num_classes,
            ignore_thresh=.5):
        loss = 0.0
        anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
        input_shape = tf.shape(yolo_output[0])[1: 3] * 32
        input_shape = tf.to_float(input_shape)
        grid_shapes = [
            tf.to_float(
                tf.shape(
                    yolo_output[layer])[1:3]) for layer in range(3)]
        for index in range(3):
            object_mask = y_true[index][..., 4:5]
            class_probs = y_true[index][..., 5:]
            grid, predictions, pred_xy, pred_wh = self.yolo_head(
                yolo_output[index], anchors[anchor_mask[index]], num_classes,
                input_shape, training=True)
            pred_box = tf.concat([pred_xy, pred_wh], axis=-1)
            raw_true_xy = y_true[
                              index][..., :2] * grid_shapes[index][::-1] - grid
            object_mask_bool = tf.cast(object_mask, dtype=tf.bool)
            raw_true_wh = tf.log(
                tf.where(tf.equal(y_true[index][..., 2:4] / anchors[
                    anchor_mask[index]] * input_shape[::-1], 0),
                         tf.ones_like(y_true[index][..., 2:4]),
                         y_true[index][..., 2:4] / anchors[
                             anchor_mask[index]] * input_shape[::-1]))
            box_loss_scale = 2 - y_true[
                                     index][..., 2:3] * y_true[index][..., 3:4]
            ignore_mask = tf.TensorArray(
                dtype=tf.float32, size=1, dynamic_size=True)

            def loop_body(internal_index, ignore_mask):
                true_box = tf.boolean_mask(
                    y_true[index][internal_index, ..., 0:4],
                    object_mask_bool[internal_index, ..., 0])
                iou = self.box_iou(pred_box[internal_index], true_box)
                best_iou = tf.reduce_max(iou, axis=-1)
                ignore_mask = ignore_mask.write(
                    internal_index, tf.cast(
                        best_iou < ignore_thresh, tf.float32))
                return internal_index + 1, ignore_mask

            _, ignore_mask = tf.while_loop(
                lambda internal_index, ignore_mask: internal_index < tf.shape(
                    yolo_output[0])[0], loop_body, [
                    0, ignore_mask])
            ignore_mask = ignore_mask.stack()
            ignore_mask = tf.expand_dims(ignore_mask, axis=-1)
            xy_loss = (
                    object_mask
                    * box_loss_scale
                    * tf.nn.sigmoid_cross_entropy_with_logits(
                          labels=raw_true_xy,
                          logits=predictions[..., 0:2])
            )
            wh_loss = (
                object_mask
                * box_loss_scale
                * 0.5
                * tf.square(raw_true_wh - predictions[..., 2:4]))
            confidence_loss = (
                    object_mask
                    * tf.nn.sigmoid_cross_entropy_with_logits(
                                  labels=object_mask,
                                  logits=predictions[..., 4:5])
                    + (1 - object_mask)
                    * tf.nn.sigmoid_cross_entropy_with_logits(
                                  labels=object_mask,
                                  logits=predictions[..., 4:5])
                    * ignore_mask
            )
            class_loss = (
                object_mask
                * tf.nn.sigmoid_cross_entropy_with_logits(
                    labels=class_probs, logits=predictions[..., 5:])
            )
            xy_loss = tf.reduce_sum(
                xy_loss) / tf.to_float(tf.shape(yolo_output[0])[0])
            wh_loss = tf.reduce_sum(
                wh_loss) / tf.to_float(tf.shape(yolo_output[0])[0])
            confidence_loss = tf.reduce_sum(
                confidence_loss) / tf.to_float(tf.shape(yolo_output[0])[0])
            class_loss = tf.reduce_sum(
                class_loss) / tf.to_float(tf.shape(yolo_output[0])[0])

            loss += xy_loss + wh_loss + confidence_loss + class_loss

        return loss