Can I extract and use classifier component from a trained transformer pipeline? #12275
-
I've trained a pipeline which contains a transformer and a textcat_multilabel. The config is attached below. We know that a pooling method is applied to the transformer output (in my case it is Also, I figured out I can extract the pipeline component by doing My question is ... is it possible, that I make a tensor with shape (1, 768) and input to the textcat_multilabel component directly to get the classification result? And how can I implement it? Thank!
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey there, Thanks for the question! Let me first clarify one thing: the pooling = {"@layers":"reduce_mean.v1"} in the config refers to pooling the word-piece representations of the transformer together to align with the tokens produced by the The pooling within the You are also correct in that you can retrieve the components from the pipeline. I tend to use You can also access the underlying textcat = nlp.get_pipe("textcat")
textcat_model = textcat.model However, the cnn_model = (
tok2vec
>> list2ragged()
>> attention_layer
>> reduce_sum()
>> residual(maxout_layer >> norm_layer >> Dropout(0.0))
) I hope I made it a bit more clear. |
Beta Was this translation helpful? Give feedback.
Hey there,
Thanks for the question! Let me first clarify one thing: the
in the config refers to pooling the word-piece representations of the transformer together to align with the tokens produced by the
Tokenizer
.The pooling within the
textcat
uses thereduce_sum
and it happens right here https://github.com/explosion/spaCy/blob/master/spacy/ml/models/textcat.py#L123. But you are right, for eachDoc
thetextcat
does pool whole document into a single vector before feeding it forward to the output layer.You are also correct in that you can retrieve the components from the pipeline. I tend to use
nlp.get_pipe("textcat")
instead of thenlp.components[i]
…