Fine Tuning OPEN AI GPT 3 Transformer Model for Custom Dataset
Now-a-days Transformer models are bieng utilized a lot in the field of NLP. It was first proposed by google in the paper "Attention is all you need "in 2017. In this paper, it was proposed that utilization of Attention Mechanism can increase the outcome of the given NLP task. The main contribution of this paper is given below:
In this work we propose the Transformer, a model architecture eschewing recurrence and instead relying entirely on an attention mechanism to draw global dependencies between input and output. The Transformer allows for significantly more parallelization … the Transformer is the first transduction model relying entirely on self-attention to compute representations of its input and output without using sequence-aligned RNNs or convolution.
"Attention is all you need "[1]
The architecture as proposed by the paper is given below:
"Attention is all you need "[1]
Right after that companies started utilizing it to create different models and implementing it for a large amount of data to develop multiple pretrained models. Therefore, now there are a number of pre-trained models that are available to use.
Hugging Face has a lot of these Pre-trained AI models and they can be used according to the need of the application. Other than that OPEN AI GPT-3 model is the biggest language model available with 175B parameters and trained on 45 GB of text. Therefore, the performance of this model is far better than any other model. It also has only two parts, one part is prompt in which you write your task and hint for GPT-3 and then it gives you result according to that task.
Implementation using Python:
The GPT-3 model can be used from the playground given on OPENAI website, where they also guide you along the tasks that you can solve with this and what inputs will be perfect for that task. Other than that python code can also be utilized to implement and use OPENAI.
The first step is to make an account on OPENAI and take the key specific to your account.
The second step is to open command window and install OPEN AI in your environment.
pip install openai
Next step is to open your editor or notebook to import OPEN AI and using it
import openai
openai.api_key = "your key"
This is an example of how to enter prompt for paraphrasing task:
prompt = "Translate this into English:\n\nTechnology is born when principles of science are applied to make a tool or something useful. At the root of the technology is an idea conceived by a man and materialized by the application of science. The technology could be simple as well as complicated. The most primitive form of a wheel made by cutting a circular log of wood is also a technology. While a million-dollar worth jet engine which provides the necessary lift to the jetliner is also an example of technology and a pretty advanced one at that. Technology is everywhere, in our houses, our offices, you might ignore it as a part of the daily ritual but today’s modern life is inseparable with technology. The bus that drives you to school is technology; the mike that the principal uses in the morning assembly is a technology and the guitar you play in music class is also a technology."
The next step is to select the engine and Temperature for completion of task. I have selected davinci as engine and temparature=1 so that my model takes more risks
response = openai.Completion.create(engine="text-davinci-001", prompt=prompt, temperature=1
max_tokens=1800,
top_p=1,
best_of=3,
),
print(response)
After that, we can see the result and it will give us output such as this one:
Recommended by LinkedIn
{ "choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"text": "\n\nTechnology is defined as the application of scientific principles to make tools or something useful. The most primitive form of a wheel, made by cutting a circular log of wood is an example of a technology. While a million-dollar worth jet engine, which provides the necessary lift to the jetliner is also an example of technology and a pretty advanced one at that. Technology is everywhere, in our houses, our offices, you might ignore it as a part of the daily ritual but today's modern life is inseparable with technology. The bus that drives you to school is technology; the microphone that the principle uses in the morning assembly is a technology and the guitar you play in music class is also a technology."
}
],
"created": 1650153479,
"id": "cmpl-4xmadCLfKIBubch71Vu6FuVUj8vo5",
"model": "text-davinci:001",
"object": "text_completion"
}
GPT-3 is trained on a large number of text and therefore this gives us promising results most of the time. However, if we dig deeper and apply for customized tasks that are related to one corporate client or a single type of data, it will be even better to fine tune it according to our custom dataset. The steps for fine tuning are given below:
Fine-Tuning:
Before fine tuning an OPEN AI model, you must have a dataset in this format:
{"prompt":" To date, over 300 apps are using GPT-3 across varying categories and industries, from productivity and education to creativity and games->", "completion":" The GPT-3 library is used in over 300 applications across numerous categories and industries, from productivity and education to creativity and games "}
This is for paraphrasing application, where in prompt there is a sentence and in completion the paraphrased sentence. In this way you will have a complete dataset in .csv format.
Now for this, open command window and the environment in which OPEN AI is already installed, after that create the dataset according to GPT 3 by giving .csv file as an input
openai tools fine_tunes.prepare_data -f C:\F Disk\data.csv
This will create a file in the same directory with extension .jsonl, this file will be utilized in the fine tuning:
openai api fine_tunes.create -t C:\F Disk\data.jsonl -m ada –batch_size 64
In this command, we have also given a base model, in this case its ada because its small and fast during demonstrations. After this we can open the Python editor and write the following code:
import openai
openai.Completion.create(
model=FINE_TUNED_MODEL,
prompt=YOUR_PROMPT)
This will give you results according to the fine tuning that you have performed on the OPEN AI model.
In this way, you can get better results for your own data that you have in your company or for your client. The OPEN AI API is not free but gives best results on NLP tasks, in the next article we will discuss Fine Tuning Hugging Face models which is free of cost. I have also discussed hugging Face briefly before with implementation on webapp. That article can be seen here:
I am a machine learning Practitioner and provide services related to AI and Data Science projects to companies as well as research collaborators. If you have a task related to AI that you want to discuss, you can connect with me with the link given below:
References:
[1] Vaswani, Ashish & Shazeer, Noam & Parmar, Niki & Uszkoreit, Jakob & Jones, Llion & Gomez, Aidan & Kaiser, Lukasz & Polosukhin, Illia, “Attention is all you need” , 2017