Skip to main content

huggingface transformers library part 3: Natural language generation

 Introduction:

The part of machine learning which deals with language modeling and textual data; can be divided into three broad parts. These three parts are:
(1) Natural language processing (NLP) : this is processing of unstructured text data into structured, analyzed and often quantized  data form. 
(2) Natural language understanding or inferencing ( NLU/ NLI): this refers to working on algorithms and models which understand written languages rather than just statistically analyzing it.
(3) Natural language generation (NLG): this refers to building models/frameworks which generate contents which mimic the quality of actual human writings.

For years, NLP, NLU and NLG have progressed, with much variance and overlap in between them. With the introduction of RNN, LSTM, CNN etc, NLG systems have shown much better progress than previous times; just like the counterparts. In this article, we will test out the performance of NLG tasks using the transformers library; namely we will test models like bart-uncased, t5 from google and others. Without further introduction; let's dive in.

What are different NLG tasks?

NLG is the art of making machines generate human languages like them; so it comes as a sub-component of many text based data science tasks. Some of the well known NLG tasks are:
(1) machine translation ( seq2seq models)
(2) abstractive summarization
(3) Dialogue generation ( part of advanced chatbots)
(4) Creative writing: storytelling, poetry generation
(5) freeform QnA systems 
(6) caption generation for images
(7) dynamic reporting ( such as generating dynamic reports from jsons using seq2seq models)
and others.

I have already briefly talked about the abstractive summarization using bart, t5 and distillbart in this previous post. If you are interested to learn more about summarization using abstractive methods; read that post first and then you can continue. Now, for this post, I am planning to explore creative writing, i.e. storytelling task. 
My goal will be to provide a topic or even a few sentences for the story to start with and then check how it proceeds. If I see a chance to include some additional logic to improve the quality we'll do that too. 

Story-time: generate a story:

First of all, don't let our hopes get up; as AI as we know of it currently; still doesn't hold their idea for much long. You can read a lot of stories created in this site using ai and human prompts; and most of them are classic text-dumps from a lot of web content. So our first goal will be to get there and then from that point; try and improve further. 
For story generation, we have to use the "text generation" pipeline.

basic code and "once upon a time" story:

We use the sample code format from transformers example. Simply the code is:
generator = pipeline('text-generation',model = "gpt2",
                      tokenizer="gpt2")
story_50 = generator("once upon a time there was a boy", max_length = 50) 
story_100 = generator("once upon a time there was a boy", max_length = 100) 
story_1000 = generator("once upon a time there was a boy", max_length = 1000)  
Now, we will look at the outputs and discuss further. Let's look at the 50 words story first. 
The story reads like: 
once upon a time there was a boy on Christmas Day, but the boy's parents
couldn't find it. It still seemed as if, with the help of a little word game, a person of goodwill could be found and the person who once tried

Clearly, although these look like a sentence; there is no proper character assignment. In the first part, the prompt; I started it with a boy; but after the coma,
 "..parents couldn't find it."  
Clearly, the boy becomes it. 
Also, the second sentence is even weirder; with as the sentence has no meaning at all. So at the small part, the story can be safely told to be failing. We will try to investigate the reason later.
 
Now look at the 100 words story. It looks like:
once upon a time there was a boy called John. A child, John had lived with his father in the forest, and when he had met William he gave him a long-awaited return to the city. Though far from alone among his friends, he was a strong
soldier, and to do more for them they needed a leader. So, William put out a message he made in his memory to his father, asking his father to help him find his missing friends. For as long as he lived

Now clearly, each sentences have meanings as well as the context is being vaguely maintained. Although sentence to sentence, although characters exist now, the meaning doesn't hold up much better than the 50 word story. i.e. in some sense, it is a nonsense too. 

Now, become ready for the masterpiece. The final piece is the story of 1000 words from that one line prompt. We will read a part of it, which will give a view on how good the story is. 
Here is a small part of the 1000 words story.
 
once upon a time there was a boy who would give up a great many pleasures on earth, and have never ceased with them, for his delight was to be more numerous than in the world and his labour more abundant. And while he saw what he did, he would think himself so strong for eternity that he could even take with him things which had not to be. And with that in mind he went into Egypt, and he laid before the holy priests men about him his favourite pleasures and learned the art of his trade. He was like Solomon from the first when he was called a God, but a King at once became him, and then afterwards in spite of his fame and riches he was not worth the sight of sight and the ear, and did but have their honour and honour-things made of gold before him. He was in such a disposition and so pure a spirit that a man should go into Egypt to see one of his masters in his youth. And when he saw one of the priests he went, whereupon as he was going out about to show off his prowess to the people, he saw there four men in a car, and of them was the Master. And he said unto them "Told you to go to the king of the Jews, I will come." So they all entered and sat down and went out. And when they had come to the king of the Jews there he brought unto him his first-born son. This is the son of the Master. And, behold, he was on a horse riding through the country: the king of the Jews said unto him, "Thou hast given me gold; take thy hand out of mine shoulders and do these of my sons, and I shall give you the gold of all Israel: then shall ye not forsake this day the Sabbath for the rest of the world: but that thou wilt find my son the master of all the world, thy son the king of the Jews and the true master of the elect and the ruler of all the living creatures for evermore, the same will ye not forsake that day which thou hast been foretold of my son: and I will give thee one more for ever, when thou hast fulfilled the one that I give thee and have given thee thy other two, that thou mightest see my son the king of Israel." 
 
See this sounds like an authentic bible story. The story holds most of its characters, a vague timeline idea as well as mentions events thoroughly. My guess is that this is highly "inspired" by some webpage containing christian stories and so on. But then again, I have not been able to find these exact stories using basic google search. By a slim chance, actually I might have generated authentic bible story on my own!☺
 
I will talk about more complex procedures of story generation, such as social planning and theory of mind in the next post about story generation. I will update the link for the next post here. Until then, stay tuned!
 
reference:
 
 

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