Exemplo n.º 1
0
def TextCatCNN_v1(tok2vec: Model,
                  exclusive_classes: bool,
                  nO: Optional[int] = None) -> Model[List[Doc], Floats2d]:
    """
    Build a simple CNN text classifier, given a token-to-vector model as inputs.
    If exclusive_classes=True, a softmax non-linearity is applied, so that the
    outputs sum to 1. If exclusive_classes=False, a logistic non-linearity
    is applied instead, so that outputs are in the range [0, 1].
    """
    chain = registry.get("layers", "chain.v1")
    reduce_mean = registry.get("layers", "reduce_mean.v1")
    Logistic = registry.get("layers", "Logistic.v1")
    Softmax = registry.get("layers", "Softmax.v1")
    Linear = registry.get("layers", "Linear.v1")
    list2ragged = registry.get("layers", "list2ragged.v1")

    with Model.define_operators({">>": chain}):
        cnn = tok2vec >> list2ragged() >> reduce_mean()
        if exclusive_classes:
            output_layer = Softmax(nO=nO, nI=tok2vec.maybe_get_dim("nO"))
            model = cnn >> output_layer
            model.set_ref("output_layer", output_layer)
        else:
            linear_layer = Linear(nO=nO, nI=tok2vec.maybe_get_dim("nO"))
            model = cnn >> linear_layer >> Logistic()
            model.set_ref("output_layer", linear_layer)
    model.set_ref("tok2vec", tok2vec)
    model.set_dim("nO", nO)
    model.attrs["multi_label"] = not exclusive_classes
    return model
Exemplo n.º 2
0
def build_nel_encoder(tok2vec: Model, nO: Optional[int] = None) -> Model:
    with Model.define_operators({">>": chain, "**": clone}):
        token_width = tok2vec.maybe_get_dim("nO")
        output_layer = Linear(nO=nO, nI=token_width)
        model = (tok2vec >> list2ragged() >> reduce_mean() >> residual(
            Maxout(nO=token_width, nI=token_width, nP=2, dropout=0.0)) >>
                 output_layer)
        model.set_ref("output_layer", output_layer)
        model.set_ref("tok2vec", tok2vec)
    return model
Exemplo n.º 3
0
def build_nel_encoder(tok2vec: Model,
                      nO: Optional[int] = None) -> Model[List[Doc], Floats2d]:
    with Model.define_operators({">>": chain, "&": tuplify}):
        token_width = tok2vec.maybe_get_dim("nO")
        output_layer = Linear(nO=nO, nI=token_width)
        model = (((tok2vec >> list2ragged()) & build_span_maker()) >>
                 extract_spans() >> reduce_mean() >> residual(
                     Maxout(nO=token_width, nI=token_width, nP=2,
                            dropout=0.0)) >> output_layer)
        model.set_ref("output_layer", output_layer)
        model.set_ref("tok2vec", tok2vec)
    # flag to show this isn't legacy
    model.attrs["include_span_maker"] = True
    return model
Exemplo n.º 4
0
def build_simple_cnn_text_classifier(
        tok2vec: Model,
        exclusive_classes: bool,
        nO: Optional[int] = None) -> Model[List[Doc], Floats2d]:
    """
    Build a simple CNN text classifier, given a token-to-vector model as inputs.
    If exclusive_classes=True, a softmax non-linearity is applied, so that the
    outputs sum to 1. If exclusive_classes=False, a logistic non-linearity
    is applied instead, so that outputs are in the range [0, 1].
    """
    fill_defaults = {"b": 0, "W": 0}
    with Model.define_operators({">>": chain}):
        cnn = tok2vec >> list2ragged() >> reduce_mean()
        nI = tok2vec.maybe_get_dim("nO")
        if exclusive_classes:
            output_layer = Softmax(nO=nO, nI=nI)
            fill_defaults["b"] = NEG_VALUE
            resizable_layer: Model = resizable(
                output_layer,
                resize_layer=partial(resize_linear_weighted,
                                     fill_defaults=fill_defaults),
            )
            model = cnn >> resizable_layer
        else:
            output_layer = Linear(nO=nO, nI=nI)
            resizable_layer = resizable(
                output_layer,
                resize_layer=partial(resize_linear_weighted,
                                     fill_defaults=fill_defaults),
            )
            model = cnn >> resizable_layer >> Logistic()
        model.set_ref("output_layer", output_layer)
        model.attrs["resize_output"] = partial(
            resize_and_set_ref,
            resizable_layer=resizable_layer,
        )
    model.set_ref("tok2vec", tok2vec)
    model.set_dim(
        "nO", nO
    )  # type: ignore  # TODO: remove type ignore once Thinc has been updated
    model.attrs["multi_label"] = not exclusive_classes
    return model