Imbalanced classification may benefit from weighting classes in the loss function inversely proportional to their frequency. Keras fit method currently allows to specify class weights as a dictionary mapping class indices to a weight value, used for weighting the loss function (during training only). However, the documentation specifies that class indices must be integers. (The internal weights standardization may still be compatible with one-hot encoding for single-label classification. E.g., at least in previous versions Keras would decode [1, 0, 0], [0, 1, 0], [0, 0, 1] labels into 0, 1, 2 integers, respectively.)

Keras alternatively allows to specify a weight per sample. The weight for each sample can then be computed as inversely proportional to the respective class frequence. In the multilabel case the weight could be chosen, e.g., as the mean or the maximum value of the weights of all classes attributed to a given sample. The following function computes weights as floats between 0 and 1 inversely proportional to the frequency of a given class. If multiple labels appear in a sample, the maximum weight is returned.

from collections import Counter

import numpy as np

def get_sample_weight(y):
    """Return sample weights. For each sample take the maximum weight
    of the respective labels.
    """
    y = np.asarray(y)
    class_count = Counter(np.concatenate([np.where(a == 1)[0] for a in y]))
    min_count = min(class_count.values())
    class_weight = {}
    for key, val in class_count.items():
        class_weight[key] = min_count / val
    sample_weight = []
    for label in y:
        idx, *__ = np.where(label == 1)
        sample_weight += [max([class_weight[i] for i in idx])]
    return np.array(sample_weight)

# Synthetic data.
y = [[1, 1, 0], [1, 0, 1], [1, 0, 0], [0, 1, 0], [1, 1, 0]]

# Sample weights
get_sample_weight(y)            # [0.33, 1., 0.25, 0.33, 0.33]

In the example above the first index corresponds to the most frequent class. Hence, the [1, 0, 0] sample has the lowest weight. However, e.g., [1, 0, 1] gets the largest weight of the two classes appearing in the sample.

For finer control Keras also accepts user-defined custom loss functions.