Skip to content

Commit 9766dbc

Browse files
committed
Update Introduction to LLMs.ipynb
1 parent 1791389 commit 9766dbc

File tree

1 file changed

+101
-31
lines changed

1 file changed

+101
-31
lines changed

Module 9 - GenAI (LLMs and Prompt Engineering)/2. Intro to LLMs/Introduction to LLMs.ipynb

Lines changed: 101 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,50 @@
88
"# **Introduction to LLMs**\n",
99
"\n",
1010
"## **What's Covered**\n",
11-
"1. A little bit about Transformers\n",
11+
"1. An Era Before Transformers\n",
1212
"2. Attention is all you need\n",
13-
"3. What is Language Modeling?\n",
14-
"4. What are LLMs?\n",
15-
"5. Pre-Training, Transfer Learning and Fine-Tuning\n",
16-
"6. Popular Modern LLMs\n",
13+
"3. A little bit about Transformers\n",
14+
"4. Advantages of Transformers\n",
15+
"5. Disadvantages of Transformers\n",
16+
"6. What is Language Modeling?\n",
17+
"7. What are LLMs?\n",
18+
"8. Pre-Training, Transfer Learning and Fine-Tuning\n",
19+
"9. Popular Modern LLMs\n",
1720
" - BERT\n",
1821
" - GPT\n",
1922
" - T5\n",
2023
" - Domain Specific LLMs\n",
21-
"7. Prompt Engineering\n",
22-
"8. Applications\n",
23-
"9. Quick Summary"
24+
"10. Prompt Engineering\n",
25+
"11. Applications\n",
26+
"12. Quick Summary\n",
27+
"13. What Next? How to use LLMs?"
2428
]
2529
},
2630
{
2731
"cell_type": "markdown",
28-
"id": "686df7bd-ff4d-4a6e-ad8d-c6ad674dc567",
32+
"id": "9c515617-57f8-42f5-b1b1-13406bb8e8e3",
2933
"metadata": {},
3034
"source": [
31-
"## **A little bit about Transformers:**\n",
32-
"<img style=\"float: right;\" width=\"400\" height=\"400\" src=\"data/images/transformer.jpeg\">\n",
35+
"## **An Era Before Transformers**\n",
3336
"\n",
34-
"1. Sequence to Sequence Model.\n",
35-
"2. Has two main components: Encoder and Decoder\n",
36-
"3. An **encoder** which is tasked with taking in raw text, splitting them up into its core components, convert them into vectors and using **self-attention** to understand the context of the text.\n",
37-
"4. A **decoder** excels at generating text by using a modified type of attention (i.e. **cross attention**) to predict the next best token.\n",
38-
"5. Transformers are **trained** to solve a specific NLP task called as **Language Modeling**.\n",
39-
"6. **Why not RNNs? -** Transformer's self attention mechanism allows each word to \"attend to\" all other words in the sequence which enables it to capture long-term dependencies and contextual relationships between words. The goal is to understand each word as it relates to the other tokens in the input text.\n",
40-
"7. **Limitation:** Transformers are still limited to an input context window (i.e. maximum length og text it can process at any given moment)"
37+
"1. **2013 and before:** Various Neural Network Architectures like ANN, CNN and RNN became very popular. They use to work well for tabular data, image data and sequential data like text respectively.\n",
38+
"2. **[(2014) Sequence to Sequence Learning with Neural Networks](https://arxiv.org/pdf/1409.3215.pdf)** paper introduced the concept of **Encoder-Decoder Architecture** to solve a seq2seq task, like machine translation.\n",
39+
" - The paper introduces Seq2Seq models, which are neural network architectures designed for mapping input sequences to output sequences. Unlike traditional models that rely on fixed-length input-output mappings, Seq2Seq models can handle variable-length sequences, making them suitable for tasks such as machine translation, summarization, and question answering.\n",
40+
" - The core of the Seq2Seq model is the encoder-decoder architecture. The encoder processes the input sequence while maintaining the hidden state and generates a fixed-length representation, often referred to as a context vector. This context vector encapsulates the representation of the whole sentence.\n",
41+
" - The decoder then uses this representation to generate the output sequence one token at a time.\n",
42+
" - Both encoder and decoder used RNN/LSTM cells due to their ability to capture sequential dependencies.\n",
43+
" - This architecture used to work well with smaller sentence.\n",
44+
" - **The Problem:** While it could handle variable-length input and output sequences, it used to rely on generating a single fixed-length context vector for the entire input sequence, which can lead to information loss, especially for longer sequences.\n",
45+
"3. **[(2015) Neural Machine Translation by Joint Learning to Align and Translate](https://arxiv.org/pdf/1409.0473.pdf)** paper introduced the concept of **Attention Mechanism** to solve the above problem.\n",
46+
" - Unlike traditional NMT models that encode the entire source sentence into a fixed-length context vector, the attention mechanism allows the model to focus on different parts of the source sentence dynamically while generating the translation.\n",
47+
" - Attention Mechanism also addressed the problem of learning alignment between input and output sequences, enables the model to weigh the importance of each word in the source sentence differently during translation. By dynamically adjusting the attention weights, the model can focus more on relevant words and ignore irrelevant ones, leading to more accurate translations.\n",
48+
" - At each timestamp of the decoder, the dynamically calculated context vector indicates which timestamps of the encoder sequence are expected to have the most influence on the current decoding step of the decoder.\n",
49+
" - In simple terms, context vector will be the weighted sum of encoders hidden state. And these weights are called as **attention weights**.\n",
50+
" - The attention mechanism has improved, the quality of translation on long input sentences. But it was not able to solve a huge fundamental flaw i.e. sequential training.\n",
51+
" - **The Problem:** Since the architecture relies on LSTM units, a notable challenge arises due to the sequential nature of training. Specifically, only one token can be processed at a time as input to the encoder, leading to slow training times. Consequently, it becomes impractical to train the model efficiently with large datasets. This limitation inhibits the application of techniques like transfer learning, which typically involve leveraging pretrained models on large datasets to improve performance on new tasks. Additionally, fine-tuning, which involves further training pretrained models on task-specific data, is also hindered by the slow training process in this architecture.\n",
52+
" - Now because of the above problem, for any task which we are suppose to solve, we have to train the model from scratch. And it takes a huge amount of time, efforts and data.\n",
53+
" - **Transfer Learning:** Transfer learning involves leveraging knowledge gained from solving one problem and applying it to a different, but related, problem.\n",
54+
" - **Fine-Tuning:** Fine-tuning, on the other hand, refers to the process of taking a pretrained model and further training it on task-specific data to adapt it to a particular problem or domain. This typically involves adjusting the parameters of the pretrained model to better suit the new task while retaining the knowledge learned from the original training."
4155
]
4256
},
4357
{
@@ -46,8 +60,72 @@
4660
"metadata": {},
4761
"source": [
4862
"## **Attention is all you need**\n",
49-
"1. **Attention** is a mechanism that assigns different weights to different parts of the input allowing the model to prioritize and emphasize the most important information while performing tasks like translation or summarization.\n",
50-
"2. Attention allows a model to focus on different parts of the input dynamically, leading to improved performance."
63+
"\n",
64+
"**[(2017) Attention is all you need](https://arxiv.org/pdf/1706.03762.pdf)** paper introduced by Google which solves the sequential training problem of earlier architecture by removing the need of RNN cells completely.\n",
65+
"1. It has the encoder-decoder architecture.\n",
66+
"2. It relies solely on self-attention mechanisms and feed-forward neural networks.\n",
67+
"3. Recall that **Attention** is a mechanism that assigns different weights to different parts of the input allowing the model to prioritize and emphasize the most important information while performing tasks like translation or summarization. Attention allows a model to focus on different parts of the input dynamically, leading to improved performance.\n",
68+
"4. **Self-Attention Mechanism:** The key innovation of the Transformer is the self-attention mechanism, which allows each word in the input sequence to attend to all other words in the sequence. This enables capturing global dependencies and alleviates the need for recurrent connections.\n",
69+
"5. **Positional Encoding:** To retain positional information of words in the input sequence without using recurrence, the model introduces positional encodings. These encodings are added to the input embeddings to provide information about the position of each word in the sequence.\n",
70+
"6. **Multi-Head Attention:** The Transformer employs multi-head attention mechanisms, where attention is computed multiple times in parallel with different learned linear projections. This allows the model to focus on different parts of the input sequence simultaneously, enhancing its ability to capture diverse patterns.\n",
71+
"7. **Parallelization and Scalability:** By relying on self-attention mechanisms and feed-forward layers, the Transformer architecture facilitates parallelization of computation across different parts of the input sequence. This results in faster training times and better scalability compared to traditional recurrent models."
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"id": "fe8b6f3b-e318-4607-baea-878ecd6fef78",
77+
"metadata": {},
78+
"source": [
79+
"## **A little bit about Transformers**\n",
80+
"\n",
81+
"<img style=\"float: right;\" width=\"400\" height=\"400\" src=\"data/images/transformer.jpeg\">\n",
82+
"\n",
83+
"1. Introduced by Google in the year 2017\n",
84+
"2. Transformer is a Sequence to Sequence Model which was proposed initially to solve the task of Machine Translation\n",
85+
"3. Has two main components: Encoder-Decoder and Attention Mechanism\n",
86+
"4. An **encoder** which is tasked with taking in raw text, splitting them up into its core components, convert them into vectors and using **self-attention** to understand the context of the text.\n",
87+
"5. A **decoder** excels at generating text by using a modified type of attention (i.e. **cross attention**) to predict the next best token.\n",
88+
"6. Transformers revolutionized NLP by enabling highly scalable training. By leveraging parallel computation and efficient self-attention mechanisms, the Transformer architecture allows for training on massive datasets with unprecedented efficiency. This scalability laid the foundation for the concept of **Transfer Learning** in NLP. Subsequent models such as BERT, GPT, and T5 were developed, leveraging pre-trained Transformer-based architectures that could be easily **fine-tuned** for a wide range of NLP tasks, further advancing the field of natural language processing.\n",
89+
"7. Transformers are **trained** to solve a specific NLP task called as **Language Modeling**.\n",
90+
"8. **Why not RNNs? -** RNN units can become a bottleneck due to sequential training. Due to parallel training capabilities and self attention mechanism of transformer, it allows each word to \"attend to\" all the other words in the sequence which enables it to capture long-term dependencies and contextual relationships between words at scale. The goal is to understand each word as it relates to the other tokens in the input text.\n",
91+
"9. **Limitation:** Transformers are still limited to an input context window (i.e. maximum length of text it can process at any given moment)\n",
92+
"10. Timeline\n",
93+
" - Till 2013 - RNN/LSTMs/GRU\n",
94+
" - 2014 - Seq2seq tasks using Encoder-Decoder architecture\n",
95+
" - 2015 - Attention Mechanism\n",
96+
" - 2017 - Transformers\n",
97+
" - 2018 - BERT by Google / GPT by OpenAI\n",
98+
" - 2019 - T5 by Google\n",
99+
" - 2020 - Stable Diffusion / GPT3\n",
100+
" - 2021 - DALL-E / Github Copilot\n",
101+
" - 2022 - ChatGPT"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"id": "33e00c8e-37b5-4f8a-9122-d948b9dd5bee",
107+
"metadata": {},
108+
"source": [
109+
"## **Advantages of Transformers**\n",
110+
"1. Parallel Training and Scalable\n",
111+
"2. Transfer Learning\n",
112+
"3. Multimodal Input and Output\n",
113+
"4. Flexible Architecture: Encoder only transformer models like BERT, Decoder only transformer like GPT and Encode-Decoder based model like T5.\n",
114+
"5. Ecosystem: HuggingFace, OpenAI, Cohere, etc..."
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"id": "206b0961-1a21-437f-914c-5ae1e5991638",
120+
"metadata": {},
121+
"source": [
122+
"## **Disadvantages of Transformers**\n",
123+
"1. Needs high computational resources like space and GPUs\n",
124+
"2. Huge amount of Data is required to train a model using transformers\n",
125+
"3. Overfitting\n",
126+
"4. Energy Consumptions\n",
127+
"5. Interpretation\n",
128+
"6. Biasness due to data and Ethical Concerns"
51129
]
52130
},
53131
{
@@ -230,9 +308,9 @@
230308
" - Zero-Shot Classification\n",
231309
"2. Given the task, what model(s) work for that task?\n",
232310
"\n",
233-
"**Example:**\n",
234-
"> **Business Problem:** Generate a news feed for an app so that users can scroll through\n",
235-
"> **Mapping to a NLP task:** Given news article, a standard NLP task is to summarize\n",
311+
"**Example:** \n",
312+
"> **Business Problem:** Generate a news feed for an app so that users can scroll through \n",
313+
"> **Mapping to a NLP task:** Given news article, a standard NLP task is to summarize \n",
236314
"\n",
237315
"Now before we get into how to solve problems like above, a quick note on NLP ecosystem:\n",
238316
"\n",
@@ -246,14 +324,6 @@
246324
"| **Spark NLP** | Scale-out, production-grade NLP |\n",
247325
"| **LangChain** | LLM Workflows |"
248326
]
249-
},
250-
{
251-
"cell_type": "code",
252-
"execution_count": null,
253-
"id": "3dfbe827-1705-48c0-8b0e-c17a2cee53a3",
254-
"metadata": {},
255-
"outputs": [],
256-
"source": []
257327
}
258328
],
259329
"metadata": {

0 commit comments

Comments
 (0)