예제 #1
0
    def init_embed(self, embeddings: Dict[str, TensorDef],
                   **kwargs) -> BaseLayer:
        """This method creates the "embedding" layer of the inputs, with an optional reduction

        :param embeddings: A dictionary of embeddings

        :Keyword Arguments: See below
        * *embeddings_reduction* (defaults to `concat`) An operator to perform on a stack of embeddings
        * *embeddings_dropout = float(kwargs.get('embeddings_dropout', 0.0))

        :return: The output of the embedding stack followed by its reduction.  This will typically be an output
          with an additional dimension which is the hidden representation of the input
        """
        reduction = kwargs.get(
            'embeddings_reduction',
            kwargs.get('embed_reduction_type', 'concat-subtract'))
        reduction = create_embeddings_reduction(embed_reduction_type=reduction,
                                                **kwargs)
        embeddings_dropout = float(kwargs.get('embeddings_dropout', 0.0))
        if len(embeddings) != 1:
            raise Exception("Currently we only support a single embedding")

        key_name = list(embeddings.keys())[0]
        key1 = f"{key_name}[0]"
        key2 = f"{key_name}[1]"
        embeddings_dual = {
            key1: embeddings[key_name],
            key2: embeddings[key_name]
        }
        return EmbeddingsStack(embeddings_dual,
                               embeddings_dropout,
                               reduction=reduction)
예제 #2
0
    def init_embed(self, embeddings: Dict[str, TensorDef],
                   **kwargs) -> BaseLayer:
        """This method creates the "embedding" layer of the inputs, with an optional reduction

        :param embeddings: A dictionary of embeddings
        :param kwargs: See below

        :Keyword Arguments:
        * *embeddings_reduction* (defaults to `concat`) An operator to perform on a stack of embeddings
        * *embeddings_name* (``str``) Optional override to Keras default names
        * *embeddings_dropout* (``float``) how much dropout post-reduction (defaults to 0.0)
        :return: The output of the embedding stack followed by its reduction.  This will typically be an output
          with an additional dimension which is the hidden representation of the input
        """

        name = kwargs.get('embeddings_name')
        reduction = kwargs.get('embeddings_reduction',
                               kwargs.get('embed_reduction_type', 'concat'))
        reduction = create_embeddings_reduction(embed_reduction_type=reduction,
                                                **kwargs)
        embeddings_dropout = float(kwargs.get('embeddings_dropout', 0.0))
        return EmbeddingsStack(embeddings,
                               embeddings_dropout,
                               reduction=reduction,
                               name=name)