def gather_loss(loss_dict: dict, loss_weight: dict):
    """Gather overall loss and compute mean of individual losses.

    Args:
        loss_dict (dict): individual loss terms
        loss_weight (dict): weights for each loss, only the loss with valid weight will
            be meaned.

    """
    loss = 0.0
    scale_dict = {}
    for name, value in loss_dict.items():
        value = loss_dict[name].mean()
        weight = loss_weight.get(name, None)
        if weight:
            loss += value * weight
        scale_dict[name] = value.item()
    return scale_dict, loss