Пример #1
0
class UnivariateInteractionModel(InteractionModel):
    """
    Defines an interaction model that configures feature layer operations
    on individual features
    """

    def __init__(
        self,
        feature_config: FeatureConfig,
        feature_layer_keys_to_fns: dict,
        tfrecord_type: str,
        max_sequence_size: int = 0,
    ):
        self.feature_config = feature_config
        self.tfrecord_type = tfrecord_type
        self.max_sequence_size = max_sequence_size
        self.feature_layer_map = FeatureLayerMap()
        self.feature_layer_map.add_fns(feature_layer_keys_to_fns)

    def feature_layer_op(self, inputs: Dict[str, Input]):
        if self.tfrecord_type == TFRecordTypeKey.EXAMPLE:
            train_features, metadata_features = define_example_feature_layer(
                feature_config=self.feature_config, feature_layer_map=self.feature_layer_map
            )(inputs)
        else:
            train_features, metadata_features = define_sequence_example_feature_layer(
                feature_config=self.feature_config,
                feature_layer_map=self.feature_layer_map,
                max_sequence_size=self.max_sequence_size,
            )(inputs)

        return train_features, metadata_features

    def transform_features_op(self, train_features, metadata_features):
        # TODO: Make train_features a dictionary
        train_features = tf.concat(train_features, axis=-1, name="train_features")

        return train_features, metadata_features
Пример #2
0
class UnivariateInteractionModel(InteractionModel):
    """
    Defines an interaction model that configures feature layer operations
    on individual features
    """
    def __init__(
        self,
        feature_config: FeatureConfig,
        tfrecord_type: str,
        feature_layer_keys_to_fns: dict = {},
        max_sequence_size: int = 0,
        file_io: FileIO = None,
    ):
        """
        Constructor for instantiating a UnivariateInteractionModel

        Parameters
        ----------
        feature_config : `FeatureConfig` object
            FeatureConfig object that defines list of model features
            and the feature transformation functions to be used on each
        tfrecord_type : {"example", "sequence_example"}
            Type of TFRecord protobuf being used for model training
        feature_layer_keys_to_fns : dict
            Dictionary of custom feature transformation functions to be applied
            on the input features
        max_sequence_size : int, optional
            Maximum size of the sequence in SequenceExample protobuf
        file_io : FileIO object
            `FileIO` object that handles read write operations
        """
        self.feature_config = feature_config
        self.tfrecord_type = tfrecord_type
        self.max_sequence_size = max_sequence_size
        self.feature_layer_map = FeatureLayerMap()
        self.feature_layer_map.add_fns(feature_layer_keys_to_fns)
        self.file_io = file_io

    def feature_layer_op(self, inputs: Dict[str, Input]):
        """
        Convert input tensorflow features into numeric
        train features and metadata features by applying respective feature
        transformation functions as specified in the FeatureConfig

        Parameters
        ----------
        inputs : dict
            Dictionary of the inputs to the tensorflow keras model

        Returns
        -------
        train_features : dict
            Dict of feature tensors that are used for training
        metadata_features : dict
            Dictionary of feature tensors that can be used for
            computing custom metrics and losses
        """
        train_features, metadata_features = define_feature_layer(
            feature_config=self.feature_config,
            tfrecord_type=self.tfrecord_type,
            feature_layer_map=self.feature_layer_map,
            file_io=self.file_io,
        )(inputs)

        return train_features, metadata_features

    def transform_features_op(self, train_features: Dict[str, tf.Tensor],
                              metadata_features: Dict[str, tf.Tensor]):
        """
        Convert train and metadata features which have feature layer
        functions applied to them into dense numeric tensors.
        Sorts the features by name and concats the individual features
        into a dense tensor.

        Parameters
        ----------
        inputs : dict
            Dictionary of the inputs to the tensorflow keras model

        Returns
        -------
        train_features : `tf.Tensor`
            Dense tensor object that is used for training
        metadata_features : dict
            Dictionary of feature tensors that can be used for
            computing custom metrics and losses
        """
        # Sorting the train features dictionary so that we control the order
        train_features_list = [
            train_features[k] for k in sorted(train_features)
        ]

        # Concat all train features to get a dense feature vector
        train_features_transformed = tf.concat(train_features_list,
                                               axis=-1,
                                               name="train_features")

        return train_features_transformed, metadata_features
Пример #3
0
class UnivariateInteractionModel(InteractionModel):
    """
    Defines an interaction model that configures feature layer operations
    on individual features
    """
    def __init__(
        self,
        feature_config: FeatureConfig,
        feature_layer_keys_to_fns: dict,
        tfrecord_type: str,
        max_sequence_size: int = 0,
        file_io: FileIO = None,
    ):
        self.feature_config = feature_config
        self.tfrecord_type = tfrecord_type
        self.max_sequence_size = max_sequence_size
        self.feature_layer_map = FeatureLayerMap()
        self.feature_layer_map.add_fns(feature_layer_keys_to_fns)
        self.file_io = file_io

    def feature_layer_op(self, inputs: Dict[str, Input]):
        """
        Apply feature layer functions on each of the tf.keras.Input

        Args:
            inputs: dictionary of keras input symbolic tensors

        Returns:
            train_features: dictionary of feature tensors that can be used for training
            metadata_features: dictionary of feature tensors that can be used as additional metadata
        """
        train_features, metadata_features = define_feature_layer(
            feature_config=self.feature_config,
            tfrecord_type=self.tfrecord_type,
            feature_layer_map=self.feature_layer_map,
            file_io=self.file_io,
        )(inputs)

        return train_features, metadata_features

    def transform_features_op(self, train_features: Dict[str, tf.Tensor],
                              metadata_features: Dict[str, tf.Tensor]):
        """
        Transform train_features and metadata_features after the
        univariate feature_layer fns have been applied.

        Args:
            train_features: dictionary of feature tensors that can be used for training
            metadata_features: dictionary of feature tensors that can be used as additional metadata

        Returns:
            train_features: single dense trainable feature tensor
            metadata_features: dictionary of metadata feature tensors
        """

        # Sorting the train features dictionary so that we control the order
        train_features_list = [
            train_features[k] for k in sorted(train_features)
        ]

        # Concat all train features to get a dense feature vector
        train_features_transformed = tf.concat(train_features_list,
                                               axis=-1,
                                               name="train_features")

        return train_features_transformed, metadata_features