| by Arround The Web | No comments

Transformer in Hugging Face – Everything You Need to Know

Hugging Face is a Machine Learning and AI community firm created by Julien Chaumond, Clément Delangue, and Thomas Wolf in 2016. which aims to provide data scientists and AI engineers access to more than 20000+ pre-trained models of transformers architecture. Transformers are a type of neural network design that is utilized to generate exceptional results in a variety of NLP tasks, including text categorization, question answering, and language modeling.

Hugging Face Transformers

The Hugging Face Transformers library is a collection of pre-trained transformer models and these models are trained on large datasets of text and code. You can download any required model from the model hub using their API, Model Hub is a hugging face webpage containing thousands of pre-trained models.

Applications of Hugging Face Transformers

Hugging Face Transformers can be used for:

  • Text classification: Classifying text, such as reviews, blogs, and other posts.
  • Language modeling: Generating text that is like a given text prompt.
  • Translation: Text translation in various languages.
  • Answering Queries: Reply to a variety of queries.

Extra Features

Hugging Face Transformers are comprised of thousands of datasets, and APIs which help programmers in a variety of ways. Most of them are deep learning, such as PyTorch, TensorFlow, and Jax. Hugging Face Transformers library is an open-source pre-trained model for specific tasks you can change according to your needs or create new transformer models from scratch.

Advantages of Hugging Face Transformers

Some key advantages of using transformers are:

  • Transformers can achieve extraordinary results on different natural language processing projects.
  • They are pre-trained on large textual datasets, which makes them easy to use and deploy.
  • They are open source and anyone can use and modify them as they see fit.

Limitations of Hugging Face Transformers

Limitations are listed here:

  • They can be expensive to train.
  • It can be challenging to adjust to specific tasks.
  • They are not ideal options for general-purpose NLP jobs.

Implementation

Let’s implement transformers from the hugging face library. It follows a few steps:

Step 1: Install Package
Firstly, you need to install the transformer by following the command:

pip install transformers

Step 2: Import Required Libraries
After installing the transformer, you need to import libraries:

from transformers import GPT2LMHeadModel, GPT2Tokenizer

In above code

GPT2LMHeadModel is a class from the hugging face libraries that represents the GPT-2 model with a language modeling head.

GPT2Tokenizer is also a class from hugging face libraries that are used for tokenization in the GPT-2 model.

Step 3: Get the Pre-trained Model and Tokenizer
The environment has been created now get the pre-trained model and tokenizer:

model_name = "gpt2-medium"
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)

In the following code:

The Model “gpt2-medium” is used and initializes its associated tokenizer.

Step 4: Prompt Text

After Initializing the model and tokenizer now time to initialize an input for text generation.

prompt_text = "Once upon a time a man"

Step 5: Tokenize the Input
After taking input from the user now tokenize that input:

input_ids = tokenizer.encode(prompt_text, return_tensors="pt")

In the above code:

The “tokenizer.encode()” function tokenizes the input and converts it into numerical input IDs that can be processed by the model.

Step 6: Generate Text using Model
Now it’s almost ready. Generate the text using the model and store it in any variable, like used “output”.

output = model.generate(input_ids, max_length=100, num_return_sequences=1)

The code uses the:

model.generate(): Function is used to generate text that is on the tokenized input

max_length: Specifies the maximum length of the text that is being generated

num_return_sequences: Specify the number of sequences generated, In the above case it is set to 1

Step 7: Decode and Print the Output
Lastly, you need to decode the generated text and print the output.

generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print("Generated Text:\n", generated_text)

In the above code, tokenizer. decode() function is used to convert the generated output into human-readable text.

Output

Conclusion

Overall, transformers are a powerful tool for natural language processing(NLP). You can use transformers for multiple tasks and achieve outstanding results. However, they can be computationally expensive to train and deploy, and you cannot consider transformers as the best choice for all-natural language processing tasks.

Share Button

Source: linuxhint.com

Leave a Reply