Skip to main content

spacy exploration part 3: spacy data structures and pipelines

 Introduction:

We discussed about dependency parsing in part 2 of spacy exploration series. Now, in the part 3 of our spacy exploration, we will explore some more concepts of NLP usages by spacy pipelines and utilities. Let's dive in.

How does spacy work internally?

Spacy uses all types of optimizations possible to make the processing as fast as possible. One of the main trick in doing so is to use hash code for the strings, and turn them into string as late as possible. The way it helps is that, digits take fixed spaces and can be processed faster than them in most of the operation. For this reason,

all strings are hash coded and the vocabulary object behaves like a double dictionary, in which using the hash you can find the string, and using the string, you can find the hash. See the following examples to get the idea about hashing:


 

Now, let's go over the data structures of the main objects in nlp. First we will see how to create a doc object manually to understand the real structure of a doc object. For creating a doc object, we can use the Doc object. Let's see how to create a doc object manually in the next example:


 

See how nlp.vocab is entered in place of vocab and the word and spaces list is provided to structure the doc. Internally spacy also works like this too.

Next, we will see how the Span objects work internally. For creating a span, we provide start and end parameters to the doc object. To manually create we will do the same, but we will directly use the span object to create the Spans manually. Let's see the code snippet to check how it is done.


 

Next, let's look into the spacy pipelines. We have already seen how processing a doc using nlp(doc) creates all the entities and dependency tags, but we need to understand how internally the nlp pipelines are built so that this works.

 In the nlp pipeline, one by one, a text goes through tokenizer, tagger, parser and ner; and embeds the results in the doc. In such a pipeline, first the text is tokenized using tokenizer, and then the doc object stores the tokens as doc elements. After this, once the pos tagger runs on doc, the parts of speech are tagged and the token.pos tags are filled. After this, once the dependency parser is run, the dependency related params are filled and generated. Finally, ner element in the pipeline detects the entities and stores them in the doc.ents i.e. entities list. 


 

In short, this is how a nlp pipeline performs all the operations on a text piece using the pipeline. When we load a model, the meta.json file of this model contains this pipeline structure too. Thus when the model is called, the model uses this pipeline info from the meta file and creates the actual pipeline.

Now, finally, the question is that, how to know what are the pipeline elements and can we know them from program? the answer is yes; we can. 

Using pipe_names attribute, one can get the list of elements in the nlp pipelines. as well as using pipeline attribute one can get the list of the pipeline objects in the pipeline.

 

Also, to disable any pipe element one can use the disable parameter of the nlp object. i.e.let's say you have ['tagger','parser','ner'] in the pipeline and you write nlp.disable_pipe("tagger"); then the parser will not perform in the pipeline. 

So in this part, we have explored the structures and pipelines of spacy. In the next part, we will explore how to create custom pipeline elements and handle them in spacy.

First let's learn to write custom pipelines. There are many reasons to add custom pipelines. One of the many is to add custom functions or custom entities. According to spacy these are the reasons for creating custom pipelines:

To add custom pipeline element add_pipe feature is used. Below see the different types of add_pipe parameters possible to add pipe elements:


 You can easily see, there are two types of parameters, i.e. last and first, and, before and after. Last and first is used to append the custom components in the end and the beginning of the nlp pipeline. The before and after parameter is used to append the custom components in the pipeline before or after some pipeline element. Let's see some examples of creating and appending such custom components.


 In this example, clearly the custom_component, to count doc length in tokens is added in the first of the pipeline. Let's see some more examples about creating custom component:




Now that we know how to build custom pipelines; we are done with this part. We will explore neural model training in next part.

Comments

Popular posts from this blog

Tinder bio generation with OpenAI GPT-3 API

Introduction: Recently I got access to OpenAI API beta. After a few simple experiments, I set on creating a simple test project. In this project, I will try to create good tinder bio for a specific person.  The abc of openai API playground: In the OpenAI API playground, you get a prompt, and then you can write instructions or specific text to trigger a response from the gpt-3 models. There are also a number of preset templates which loads a specific kind of prompt and let's you generate pre-prepared results. What are the models available? There are 4 models which are stable. These are: (1) curie (2) babbage (3) ada (4) da-vinci da-vinci is the strongest of them all and can perform all downstream tasks which other models can do. There are 2 other new models which openai introduced this year (2021) named da-vinci-instruct-beta and curie-instruct-beta. These instruction models are specifically built for taking in instructions. As OpenAI blog explains and also you will see in our

Can we write codes automatically with GPT-3?

 Introduction: OpenAI created and released the first versions of GPT-3 back in 2021 beginning. We wrote a few text generation articles that time and tested how to create tinder bio using GPT-3 . If you are interested to know more on what is GPT-3 or what is openai, how the server look, then read the tinder bio article. In this article, we will explore Code generation with OpenAI models.  It has been noted already in multiple blogs and exploration work, that GPT-3 can even solve leetcode problems. We will try to explore how good the OpenAI model can "code" and whether prompt tuning will improve or change those performances. Basic coding: We will try to see a few data structure coding performance by GPT-3. (a) Merge sort with python:  First with 200 words limit, it couldn't complete the Write sample code for merge sort in python.   def merge(arr, l, m, r):     n1 = m - l + 1     n2 = r- m       # create temp arrays     L = [0] * (n1)     R = [0] * (n

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle