The Wayback Machine - https://web.archive.org/web/20250212041047/https://www.geeksforgeeks.org/getting-started-with-transformers/
Open In App

Transformers in Machine Learning

Last Updated : 06 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Transformer is a neural network architecture used for performing machine learning tasks. In 2017 Vaswani et al. published a paper ” Attention is All You Need” in which the transformers architecture was introduced. The article explores the architecture, workings, and applications of transformers.

Transformer Architecture is a model that uses self-attention to transform one whole sentence into a single sentence. This is a big shift from how older models work step by step, and it helps overcome the challenges seen in models like RNNs and LSTMs.

Need For Transformers Model in Machine Learning

Traditional models like RNNs (Recurrent Neural Networks) suffer from the vanishing gradient problem, which leads to long-term memory loss. RNNs process text sequentially, meaning they analyze words one at a time.

For example, in the sentence: “XYZ went to France in 2019 when there were no cases of COVID, and there he met the president of that country,” the word “that country” refers to “France”.

However, an RNN would struggle to link “that country” to “France”, since it processes each word in sequence, losing context over long sentences. This limitation prevents RNNs from understanding the full meaning of the sentence.

While adding more memory cells in LSTMs (Long Short-Term Memory networks) helped address the vanishing gradient issue, they still process words one by one. This sequential processing means LSTMs can’t analyze an entire sentence at once. Additionally, static embeddings in LSTMs fail to capture changing word meanings depending on context.

For instance, the word “point” has different meanings in these two sentences:

  • “The needle has a sharp point.”
  • “It is not polite to point at people.”

This shows how the same word can have different meanings depending on context, which traditional models like RNNs and LSTMs fail to capture. The transformer model, through its self-attention mechanism, processes the entire sentence in parallel, addressing these issues and making it significantly more effective at understanding context.

Architecture and Working of Transformers

Positional Encoding

Transformers lack an inherent understanding of sequential order. To solve this, Positional Encodings are added to token embeddings, providing information about the position of each token within a sequence.

Position-wise Feed-Forward Networks

The Feed-Forward Networks consist of two linear transformations with a ReLU activation. These layers are present in both the encoder and decoder, processing features at each position in the sequence.

Mathematically:

[Tex]\text{FFN}(x) = \max(0, xW_1 + b_1)W_2 + b_2 [/Tex]

Attention Mechanism

The attention mechanism in transformers uses a scaled dot-product approach, calculating weighted sums of query, key, and value vectors. To improve model performance, multi-head attention applies the attention mechanism multiple times in parallel, each with unique learned projections. The outputs are concatenated and transformed to generate the final result. This allows the model to capture diverse relationships and create richer, more nuanced representations of the input sequence. Multi-head attention enhances transformer models’ ability to process complex data.

Encoder-Decoder Architecture

The encoder-decoder structure is key to transformer models. The encoder processes the input sequence into a vector, while the decoder converts this vector back into a sequence. Each encoder and decoder layer includes self-attention and feed-forward layers. In the decoder, an encoder-decoder attention layer is added to focus on relevant parts of the input.

For example, a French sentence “Je suis étudiant” is translated into “I am a student” in English.

The encoder consists of two layers: Self-Attention and Feed-Forward. The input first passes through the self-attention layer, where the model processes the relationships between words. The output of this layer is then fed into a feed-forward neural network for further transformation. Sequential data has inherent temporal characteristics, meaning each word’s position impacts its meaning.

For instance, in the sentence “The cat didn’t chase the mouse, because it was not hungry,” the word ‘it’ refers to ‘cat’. The self-attention mechanism helps the model correctly associate ‘it’ with ‘cat’, allowing the model to reformulate the representation based on all words in the sentence.

Screenshot-2024-05-24-112357


The decoder architecture has three layers: Self Attention, Encoder-decoder attention, and Feed Forward. The decoder has both the self-attention and feed-forward layer, which is also present in the encoder, but between them is an attention layer that helps the decoder focus on relevant parts of the input sentence.

There are six layers of encoders and decoders in Transformer architecture. In the bottom encoder, word embeddings are performed, and each word is transformed into a vector of size 512. The input to the other encoders would be the output of the encoder that’s directly below. The various layers of the encoder are for discovering the NLP pipeline. Like- the first layer is used for Part of speech tags, the second layer for constituents, the third layer for dependencies, the fourth layer for semantic roles, the fifth for coreference, and the sixth for relations.

Applications of Transformers

Some of the applications of transformers are:

  1. NLP Tasks: Transformers are used for machine translation, text summarization, named entity recognition, and sentiment analysis.
  2. Speech Recognition: They process audio signals to convert speech into transcribed text.
  3. Computer Vision: Transformers are applied to image classification, object detection, and image generation.
  4. Recommendation Systems: They provide personalized recommendations based on user preferences.
  5. Text and Music Generation: Transformers are used for generating text (e.g., articles) and composing music.

Also Check:

Conclusion

In conclusion, Transformer Architecture in Machine learning helps in NLP projects, it follows Encoder and Decoder Architecture. As machine learning continues to evolve and the transformer architecture stands as a pivotal development, and then shaping future of NLP and other related domains.



Practice Tags :

Similar Reads

three90RightbarBannerImg