<commit_before>from abc import ABCMeta
from keras.models import Model


class ModelAbstraction:
    """
    An abstract interface for a model that exposes the required functionalities
    """
    __metaclass__ = ABCMeta

    def __init__(self, model):
        """
        :param model: A function that takes a symbolic input and returns the
                      symbolic output for the model's predictions.
        """
        raise NotImplementedError('Model abstraction not implementated.')

    def get_hidden(self, layer):
        """
        :return: A symbolic representation of the hidden features
        :param layer: The name of the hidden layer to return features at.
        """
        error = 'Feature extraction for hidden layers not implemented'
        raise NotImplementedError(error)

    def get_logits(self):
        """
        :return: A symbolic representation of the output logits, values before
        softmax
        """
        error = 'get_logits not implemented'
        raise NotImplementedError(error)

    def get_probs(self):
        """
        :return: A symbolic representation of the output logits, values before
        softmax
        """
        error = 'get_probs not implemented'
        raise NotImplementedError(error)


class KerasModelWrapper(ModelAbstraction):
    """
    An implementation of FeatureExposer that wrapps a Keras model.
    """

    def __init__(self, model):
        """
        Create a wrapper from a Keras model
        :param model: A Keras model
        """
        super(KerasModelWrapper, self).__init__(model)

        # Initialize attributes
        self.model = model

    def get_hidden(self, layer):
        """
        :param layer: The name of the hidden layer
        :return: A symbolic representation of the hidden features
        """
        model = self.model

        # Create an extra model that exposes the hidden layer representation
        # Get input
        new_input = model.get_input_at(0)
        # Find the layer to connect
        target_feat = model.get_layer(layer).output
        # Build a new model
        h = Model(new_input, target_feat)<commit_msg>model abstraction, comments expanded and corrected.<commit_after>from abc import ABCMeta


class ModelAbstraction:
    """
    An abstract interface for model wrappers that exposes model symbols
    needed for making an attack. This abstraction removes the dependency on
    any specific neural network package (e.g. Keras) from the core
    code of CleverHans. It can also simplify exposing the hidden features of a
    model when a specific package does not directly expose them (needed by
    "features adversaries").
    """
    __metaclass__ = ABCMeta

    def __init__(self, model):
        """
        :param model: A function that takes a symbolic input and returns the
                      symbolic output for the model's predictions.
        """
        raise NotImplementedError('Model abstraction not implementated.')

    def get_hidden(self, layer):
        """
        :param layer: The name of the hidden layer to return features at.
        :return: A symbolic representation of the hidden features
        """
        error = 'Feature extraction for hidden layers not implemented'
        raise NotImplementedError(error)

    def get_logits(self):
        """
        :return: A symbolic representation of the output logits, values before
                 softmax.
        """
        error = 'get_logits not implemented'
        raise NotImplementedError(error)

    def get_probs(self):
        """
        :return: A symbolic representation of the output probabilities, values
                 after softmax.
        """
        error = 'get_probs not implemented'
        raise NotImplementedError(error)


class KerasModelWrapper(ModelAbstraction):
    """
    An implementation of ModelAbstraction that wraps a Keras model. It
    specifically exposes the hidden features of a model.
    """

    def __init__(self, model):
        """
        Create a wrapper from a Keras model

        :param model: A Keras model
        """
        super(KerasModelWrapper, self).__init__(model)

        # Initialize attributes
        self.model = model

    def get_hidden(self, layer):
        """
        :param layer: The name of the hidden layer
        :return: A symbolic representation of the hidden features
        """
        model = self.model

        from keras.models import Model

        # Create an extra model that exposes the hidden layer representation
        # Get input
        new_input = model.get_input_at(0)
        # Find the layer to connect
        target_feat = model.get_layer(layer).output
        # Build a new model
        h = Model(new_input, target_feat)
{'fraction_non_alphanumeric': 0.06589716684155299, 'fraction_numerical': 0.0004197271773347324, 'mean_word_length': 2.3258897418004185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2813814', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1149, 'n_words': 462}