def get_weights(layer):
    """Get weights of Keras layer, possibly applying sparsifying mask.

    Parameters
    ----------

    layer: keras.layers.Layer

    Returns
    -------

    : tuple
        The layer weights and biases.
    """

    all_weights = layer.get_weights()
    if len(all_weights) == 2:
        weights, biases = all_weights
    elif len(all_weights) == 3:
        weights, biases, masks = all_weights
        weights = weights * masks
    else:
        raise ValueError("Layer {} was expected to contain weights, biases "
                         "and, in rare cases,masks.".format(layer.name))
    return weights, biases