Large Language Models, Prompt Engineering, and Part-of-Speech Tagging
Prerequisite Knowledge
This lecture builds on the following concepts from earlier lectures. If any feel unfamiliar, review the linked notes before proceeding.
Previously Covered in This Subject
- Part-of-Speech Tagging and WordNet — first encounter with POS tagging in Lecture 1 (Introduction to Natural Language Processing).
- Why language is hard: ambiguity — word order and lexical ambiguity from Lecture 1 (Introduction to Natural Language Processing).
- Unigram, bigram, and trigram language models — the Markov assumption from Lecture 4 (CBOW, GloVe, and Statistical Language Modeling).
- Bigram probabilities: MLE estimation — counting from a toy corpus in Lecture 4 (CBOW, GloVe, and Statistical Language Modeling).
- Neural language models — from Lecture 5 (Perplexity and Neural Language Models).
- N-gram versus neural language models — the comparison from Lecture 5 (Perplexity and Neural Language Models).
6.1 Large Language Models (LLMs)
6.1.1 What makes an LLM "large"
Hook: We closed the last module with n-gram language models and neural language models, weighing their pros and cons. If an LLM is "the same language model, just scaled up," then what exactly grows — and does that growth change how the model behaves?
A large language model (LLM) is the language model we already studied, built with the same neural approach, scaled up enormously. The word "large" is doing real work here: compared with n-gram models and the traditional feedforward networks, an LLM is genuinely huge. It is not a slightly bigger n-gram table — it is a different league of size on three separate dimensions.
Definition — what "large" means. An LLM is a general-purpose language model that is large in all three of these ways:
- Parameters. The model weights learned during training — a modern LLM has about 175 billion parameters and needs gigabytes of memory just to hold them. When people say "large language model," they usually mean at least 100 billion parameters; 175 billion is a typical headline number.
- Training data. The model is pre-trained on web-scale corpora — in principle, the entire web corpus.
- Compute. Training and running the model need high-end server hardware, A100- or H100-class GPUs. None of this runs on a CPU.
Conceptually, an LLM sits at the combination of generative AI (Gen AI) and deep learning, with deep learning itself a part of machine learning — this is where language modeling fits inside the Gen AI scope. Because LLMs are general purpose, built for language understanding rather than for one single task, one model can serve many NLP applications: once the model knows language, you can apply it to a lot of other applications. This generality is the key benefit and the reason LLMs became so popular. The model is pre-trained on a certain dataset; if you want to use it for a specific dataset, such as a domain-specific corpus it was not trained on, you can fine-tune it.
Worked example: what does 175 billion parameters cost in hardware?
A parameter is a number stored in the model. If we store each parameter as a 4-byte floating-point number:
An A100 or H100 GPU holds about 80 GB of memory, so just holding the weights in memory needs roughly GPUs — and training is far costlier than serving. Compare this with an n-gram model: its "weights" are simply a table of word-pair counts, small enough to fit on a laptop.
Sense-check: the numbers line up with the claim that only organizations with data-center budgets can train such models from scratch.
The model families named in the industry today are GPT, BERT, LLaMA, Claude, Flan, Palm, and Bloom — many are open source, many are commercial. Hugging Face's transformers library hosts a huge collection of them, and you typically reach them through API keys. Only very large organizations, like Google and the big cloud providers, can train these models from scratch, because training demands enormous resources. If you want to install and use a model locally, you go for the smaller variants: Llama 3.1, for example, ships an 8-billion-parameter checkpoint that runs on a single workstation GPU — billions of parameters, a world away from the hundreds of billions that the frontier models need.
Two practical benefits follow from pre-training. First, you do not need labeled training examples the way traditional supervised ML does: no labeled training examples are needed to use an LLM — only training data in general. Second, when you use a pre-trained model at inference time for an application, you do not need to retrain the model. There is one more thing to remember: you have to think about a very good prompt input — the way you give input to the LLM defines the quality of the output, and prompt engineering is the area that designs these prompts effectively.
Scope — what "large" does not buy you. The three "large" claims hold only for the model itself, not for every task you hand it.
- Being large does not mean being correct: the output quality depends on the input prompt, which is why prompt engineering exists.
- A general-purpose model pre-trained on web text can be weak on specialized domains such as legal or medical text — that is why fine-tuning exists.
- Training from scratch is out of reach for all but the biggest organizations; local users must settle for smaller variants.
Visual intuition. Imagine a bar chart with the number of parameters on a log scale. Near the left edge sit the n-gram tables and early feedforward networks, in the millions to hundreds of millions; BERT-class models reach a few hundred million; the LLM bar towers past 175 billion — roughly three orders of magnitude above the next bar. The landmark to read is that jump: scale here is not incremental, it is exponential, and the jump is what creates the general-purpose behavior.
Pitfalls.
- Confusing parameters with tokens. A model's "1 million parameters" are its learned weights, not the input tokens it processes (see the Q&A below).
- Assuming an LLM needs labeled examples. The pre-trained model is already trained; at inference time you simply use it.
- Planning to train one from scratch locally. A 175-billion-parameter model needs about nine 80 GB GPUs just to hold its weights; training multiplies that several times over.
- Forgetting the prompt. The same model gives good or bad answers depending on how you ask — the input is part of the system.
Recap: an LLM is the neural language model you already know, scaled to roughly 100+ billion parameters, web-scale training data, and data-center compute; its generality lets one model serve many tasks. Bridge: that generality is only useful if the input is right — which is exactly what prompt engineering, the next topic, is about.
Real-world: because language appears in most industry applications, LLMs are useful almost everywhere — the model families above run inside search, customer service, code assistants, and enterprise tools, and Hugging Face is the common catalog for open weights. Everywhere, the economic question is the same: the model knows language already, so the remaining work is adapting it (fine-tuning) and talking to it well (prompting).
6.1.2 Pretraining and fine-tuning
Pre-trained models (PTMs) are trained on web-scale data, so they may not be suitable for a specialized domain such as legal text or medical text. Fine-tuning fixes this: you take the pre-trained transformer architecture and retrain the model on the new, smaller domain dataset, updating many of the pre-trained weights to accommodate the new data.
Fine-tuning = training that starts from a pre-trained model's weights and continues on a new, smaller dataset. It is itself a part of training, and it involves a lot of parameter tuning and resources. It works best when the new dataset is large and similar to the dataset the model was pre-trained on.
Worked example: adapting a general model to a domain.
A pre-trained LLM has seen general web text, so asking it to answer legal questions directly gives mediocre results. Fine-tuning on a legal corpus — say 100,000 annotated legal documents — shifts the weights toward legal vocabulary and conventions, and the same model now produces usable legal answers. The same recipe applies to medical text, financial filings, or customer-support logs.
Sense-check: this matches the rule of thumb — fine-tuning helps most when the domain data is large enough and close to the pre-training distribution.
Assumption — when fine-tuning does not help. If the new dataset is tiny or very different from what the model saw in pre-training, fine-tuning gains little and risks harming the general knowledge. In that case retrieval-augmented generation (RAG), which connects the model to external knowledge, is the usual alternative — the class returns to RAG after the mid sem.
There are parameter-efficient fine-tuning techniques, such as LoRA, that make the process cheaper and more efficient — but those belong to next semester's courses, where they are studied properly.
Exam note: parameter-efficient fine-tuning (LoRA and the like) is outside the scope of this course — it is covered in the next semester courses, so you only need the idea here. Retrieval augmented generation (RAG) returns after the mid sem, and fine-tuning is touched on again there.
6.1.3 Student Questions and Answers
Q: When we say an LLM has, say, 1 million parameters, are those parameters the model weights or the input tokens?
A: Model weights. Internally, an LLM is a transformer, which you studied in the deep learning course: a stack of encoder layers — BERT itself has a large stack (BERT-base stacks 12 encoder layers, BERT-large 24). Each encoder layer has multi-headed attention — say 8 attention heads — and every attention head carries its own query, key, and value matrices; on top sit the feed-forward network and a softmax function, and backpropagation learns all of these weights. These weights are the parameters, for any model: an ML model, a neural network, a transformer, an agentic AI system — everywhere, the parameters are nothing but the model weights. In the end, all of ML, including deep learning, LLMs, agentic AI, and Gen AI, boils down to learning the weights with the backpropagation and gradient descent algorithms.
6.1.4 LLM use cases
Real-world: the use cases are broad — text summarization, machine translation, search and information retrieval (including enterprise search engines), conversational AI, question answering, sentiment analysis, and more. Perplexity, for example, is an LLM-powered tool that summarizes research papers for you. Because language appears in most industry applications, LLMs are useful almost everywhere: the same pre-trained weights serve a chatbot in one deployment and an enterprise search index in another, which is exactly the generality described above.
6.1.5 Where the deeper LLM content lives
This introduction deliberately stays at the surface. The transformer architecture is studied in detail later in this NLP course when we build contextual word embeddings, and the mathematical details of LLMs are covered in the next semester's specialization courses — the LLM-for-Gen-AI course, NLP applications, and conversational AI — which is why the program has so many electives. Today's goal is to give a flavor of these state-of-the-art areas and to fix the terminology, so that the names you meet in industry — GPT, BERT, LLaMA, Claude, Flan, Palm, Bloom, LoRA, RAG — are familiar when the deeper courses start.
6.2 Prompt Engineering and Generative AI
6.2.1 What is a prompt, and what is prompt engineering
Hook: The model has already learned language during training. So why does the wording of your request change the answer so much — and how do you get the model to give you what you actually want?
A prompt is the input you give to an LLM — the text through which you ask the model to do something. Prompt engineering is the craft of designing these prompts of high quality so that the model produces accurate, relevant, and useful answers. The aim is to make sure the machine understands the intent of the human, so it can process the user's input and give a more accurate, relevant answer.
There are two flavors of prompt engineering. Hard prompt engineering is what you do every day: the user writes the prompt in words. Soft prompt engineering is automatic: the system generates the prompt itself, in the form of vector embeddings, rather than in text. We focus on hard prompts — the ones users give. A newer practice is to ask the LLM itself to write the prompt for you: today's models can produce high-quality prompts on demand, which in turn makes the model's answers better. In other words, prompt engineering is not only about what you type — it is also a skill the model itself can exercise.
6.2.2 The context window
Every LLM product — ChatGPT, Claude, GYANMATI, and the rest — restricts how many tokens a particular prompt can use, unless you pay for extra tokens. That limit is the context window.
Context window = the maximum number of tokens a single prompt may hold. It covers the input words plus whatever output the model generates, with roughly a thousand words as a typical budget. In the simplest tokenization, which you saw in the first NLP session, tokens are just the words of the sentence, split on spaces with the NLTK library; the details, like byte pair encoding, come next semester.
Because LLMs have a tendency to generate very long outputs, it is always a good idea to restrict your output: "give me a definition of prompt engineering in two lines," "return the answer in four lines."
Why restrict the output length? Long outputs are not just slow to read. Every token the model generates costs money (you pay per token in most APIs) and consumes energy — so restricting the output also reduces the carbon footprint of every request. This is a habit worth building when you use ChatGPT, Claude, or GYANMATI.
Worked example: budgeting a 1000-word context window. You paste a 600-word document and ask the model to summarize it. The input has already consumed 600 of the 1000 words; if the model replies with a 500-word summary, the request exceeds the window. Asking for a four-line answer keeps the total inside the budget — and charges you for fewer tokens.
Sense-check: short output requests keep the total prompt inside the window and cut both cost and energy per request.
6.2.3 Designing a good prompt: context, role, and output limits
A good prompt combines a few ingredients.
Three ingredients make a strong prompt:
- Context. Before the actual instruction, you can say "I am a researcher, summarize this document" — that sets the scene for the real prompt.
- Role. You can declare a persona — "I am a faculty member designing mathematical problems for students" — and the model's output changes with the role you pick: as a faculty member, as a student, as a researcher, or as a naive user who is not aware of certain concepts, you get different outputs.
- Constraints. You can specify the output format: "generate the answer in five words," "in five lines."
All of this goes into the input prompt, but you must stay mindful of the context window while you build it.
Worked example: building one prompt with all three ingredients. Task: get a practice problem for a course.
- Context: "I am a faculty member designing a mid-semester NLP exam."
- Role: "You are an examiner for a natural language processing course."
- Instruction: "Write one mathematical problem on the hidden Markov model, with the probability tables provided and the answer not shown."
- Constraint: "Return the problem in five lines."
Assembled prompt: "I am a faculty member designing a mid-semester NLP exam. You are an examiner for a natural language processing course. Write one mathematical problem on the hidden Markov model, with the probability tables provided and the answer not shown. Return the problem in five lines."
Sense-check: each ingredient changes the output — drop the role and the same request returns an explanation instead of an exam problem.
6.2.4 Zero-shot and few-shot inference
LLMs are language completion machines: they look in the training data and produce output tokens that answer your prompt. Sometimes you get garbage — so the way you set up the request matters.
Zero-shot means giving the model a task with no examples at all — "N shot" means N examples, so 0 shot means no examples. Few-shot inference means pasting a few labeled examples inside the prompt before asking the model to do the task. Here inference means using the already-trained model for your application; training is where backpropagation learns all the model weights. At inference time you can paste a couple of labeled examples — "this review is positive," "this review is negative" — and the model mirrors that pattern.
Worked example: sentiment classification, zero-shot vs few-shot.
Zero-shot prompt: "Classify this review: 'I loved the movie, the acting was great.' Sentiment in three or four words." The output comes back vague — you asked about a movie and the model replies "very nice book." The answer is not useful and not relevant to the user's input.
Few-shot prompt: "Review: 'I loved the movie.' Sentiment: positive. Review: 'The plot made no sense.' Sentiment: negative. Review: 'I loved the movie, the acting was great.' Sentiment:" Now the model mirrors the pattern and returns a proper sentiment, for example "positive".
Sense-check: only the examples changed — the model, the task, and the question stayed the same, and the few-shot version returns a usable answer.
Why does showing examples work? It works the same way it does for people: if you explain a generic concept with a concrete example, it becomes clearer at once; the machine processes the input information better when it has seen examples. Few-shot inference (one, two, three examples) consistently gives better results than zero-shot, because you are showing the machine what you want.
| Dimension | Zero-shot | Few-shot |
|---|---|---|
| Examples in prompt | None | One to a few labeled examples |
| Output quality | Can be vague or off-topic ("very nice book" for a movie review) | Mirrors the given pattern; usually accurate |
| Prompt cost | Lowest (fewest tokens) | Slightly higher |
| When to pick | Quick tasks where the model already knows the format | Tasks with a specific output format you want enforced |
The trade-off is the context window — you cannot paste hundreds of examples at inference time, so you restrict yourself to a few. During training itself, people also use instruction fine-tuning with many examples, and there the context-window restriction does not apply. Prompt engineering, in one line: define and design your prompts intelligently so the LLM or Gen AI model gives you better results. Today's LLMs are even trained for prompt engineering — given an input prompt, they internally try to generate a better prompt so the model produces a better answer.
6.2.5 Generative AI and its relationship to LLMs
Gen AI is a generation task: from the name itself, it generates new data based on some content it has seen before — based on a given prompt, it generates new content. About 80% of today's applications use textual LLM models generating textual content; even code generation is textual content generation. But many LLM-based models are multimodal: they generate audio content, video content, images, and more.
Real-world: text-to-SQL and code generation platforms (write the SQL or Python for you, no-code platforms), text summarization, machine translation, sentiment analysis, image generation, voice generation (the Whisper API generates voices), video generation tools, 3D models, and combinations such as virtual reality. Gen AI also powers conversational AI assistants — a whole course next semester — and coding agents that not only write code but fix bugs for you: they tell you which libraries are missing, what to import, what to reinstall, what parameter errors you have, or whether a file is missing. All of these errors are detected automatically by the coding agents. The unifying idea is generation: the model produces new content — text, code, audio, video, images — from the prompt you give it.
6.2.6 Warnings: when not to use LLMs
LLMs and agentic AI are not automatically the right answer for every application.
- They are data hungry — they need huge resources and they cost a lot.
- Before jumping into transformers, large language models, and agentic AI technologies, check whether the technology is actually useful for the problem at hand.
- Explainability is the biggest challenge: you cannot easily say why an LLM produced a particular output, which matters in regulated settings.
- Responsible AI is another big challenge: you do not want your company's data deleted automatically by these AI tools — that costs money for your organization.
Make wise decisions about adopting Gen AI or agentic AI: evaluate first, then adopt.
When the models fit and the resources exist, they are useful, generic, and will give good results — but the evaluation step comes first. The pattern to internalize: LLM-based adoption is a business decision with real costs (tokens, energy, data risk), not a default choice for every NLP problem.
6.2.7 Student Questions and Answers
Q: There are a lot of topics here, and you said we will learn many of them next semester. How much do we need to read these topics for the exams?
A: These topics are just for introduction — there won't be many questions on LLMs. The mathematical problems will come from the earlier topics: skip-gram, negative sampling, neural language modeling, and evaluation. This LLM and prompt engineering content is for your understanding, to give you a sense of the state of the art, not for exam problems.
6.3 Part-of-Speech Tagging: The Problem
6.3.1 What is part-of-speech tagging
Hook: Part of speech is a concept from school: noun, pronoun, verb, adjective, adverb, and the rest. Machines did not go to school, though — for a machine every word is garbage; it understands the meaning of none of them. How do we hand that school knowledge to a computer?
Part-of-speech (POS) tagging is the process of automatically assigning these tags to the words of a sentence. The tags are also called lexical class markers: lexicons are the words of the language, and each word carries an associated part-of-speech tag. If the machine must do this tagging automatically, the problem is genuinely difficult — even for a human it can get challenging. And this is not one-word-at-a-time tagging: you are not given one word at a time and asked for its tag; the tags are assigned to the whole sentence together.
Q: What is a part of speech, and why do we need it? (Class answers: noun, pronoun — the parts of speech are used to structure a sentence, to make it grammatically and syntactically correct, to understand the meaning of the sentence, and to identify the relationship between words.)
A: Exactly. Grammar checking is one use — making the sentence grammatically and syntactically correct. But beyond that, POS tagging is required in almost every NLP application: understanding the meaning of the sentence, identifying the relationships between words, chatbots that understand meaning. This is a fundamental concept in NLP.
Exam note: no question in the exam will ask you to define a part-of-speech tag, and you will not be asked to figure out the tags of a sentence. If a POS tagging problem appears, the tag table will be provided in the exam paper — nothing to memorize.
6.3.2 POS tagging is a sequence learning problem
You have studied RNNs and LSTMs, so you have met the sequence learning problem. Given an input sequence of tokens or words, we want to generate an output sequence — not one input, one output, but a sequence of inputs where there is dependency and order, and the entire sequence must be processed together.
Sequence learning = predicting an output sequence from an input sequence, where order and dependencies matter. Two properties make POS tagging a sequence problem:
- Order matters. "The cat sat on the wall" means something; "wall cat on sat the" means nothing. The same words in a different order carry a different message.
- The length is not fixed. You may tag a phrase, a sentence, a paragraph, or a document.
Because the input is a sequence and the output is a sequence, we cannot use traditional single-output classifiers such as logistic regression or naive Bayes. That is why we use RNNs, LSTMs, and even transformers — language generation is also a sequence learning problem, and many real-world problems today take a sequence of inputs and produce a sequence of outputs. POS tagging is exactly this: a sequence of words in, a sequence of tags out, one tag per word.
Q: What is the sequence learning problem? (Student: it is like "cat sat on the wall" but not "wall cat on sat" — the order of the words matters, and we cannot remember all the sequence, so the memory has to be long enough — dependency among the words.)
A: Yes — order is one of the key factors, and the dependency among the words is exactly what I mean. Given an input sequence of tokens we generate an output sequence; you cannot process it one token at a time. So the classic sequence learning models — RNN, LSTM, and transformers — are what we use.
6.3.3 Real-world applications of POS tagging
Real-world: POS tagging is used in nearly every NLP application today.
- Voice assistants use it to identify a command — attention mechanisms capture the key POS tags to disambiguate and pin down the actual meaning; the targets are usually nouns, proper nouns, common nouns, and sometimes adjectives.
- Chatbots and agentic AI systems need it, and when an agent makes an API or tool call, it must identify the keywords — typically the names of the functions.
- Search engines use it to disambiguate: is "play" a verb or a noun? Is cricket the insect or the game?
- Information extraction and named entity recognition use it, as does conversational AI ("book me a flight from this source to this destination" — source and destination are named entities, which are nothing but proper nouns).
- Grammar checking needs it; machine translation needs it: in "Chetana teaches at the university," Chetana means inspiration in Hindi, but it must be retained as a proper noun and not translated into its meaning, while teaches, a verb, is translated as a verb into the target language.
- Social media analytics and sentiment use adjectives, which are the POS tags you need to extract; text-to-speech systems use it; biomedical and domain-specific applications use it.
Everywhere, POS tagging plays a very important role: almost every downstream tool needs to know what role each word plays before it can do its own job.
6.3.4 The agentic-AI connection: "set the timer for oven to 10 minutes"
Why POS tagging matters in today's agentic AI world. Consider the command "set the timer for oven to 10 minutes." If the word set attaches to oven, the system might perform a dangerous action — actually setting the oven rather than the timer. The relations among the words must be captured effectively: set is an action the machine needs to perform (a verb), timer is a noun phrase, for the oven is a prepositional phrase. These POS tags disambiguate the user's input, make the behavior more deterministic, remove ambiguity, prevent unsafe actions, and support compliance. Both statistical and neural POS taggers are used in many agentic pipelines today.
The lesson generalizes: an agent that executes commands on the real world cannot afford to misread which word is the action and which word is the object. Tagging is the cheap, deterministic layer that pins those roles down before the agent acts.
6.3.5 Approaches: rule-based, statistical, neural, and LLM-based
Four families of approaches exist.
- Rule-based tagging is the simplest: you write rules — "the previous word is an article, so the next word has to be a noun"; "after an article and a noun, the next word is generally a verb or an adjective." English has an SVO structure, so first words are typically proper nouns, second words adjectives, third words proper nouns, and so on.
- Statistical approaches — the HMM and CRF that we study today — use probabilities estimated from tagged data; they are still used in many agentic pipelines for POS tagging.
- Neural approaches use the sequence learning models, such as RNN and LSTM.
- LLM- and transformer-based models apply the large pre-trained models from the previous topic.
| Dimension | Rule-based | Statistical (HMM, CRF) | Neural (RNN, LSTM) | LLM/transformer |
|---|---|---|---|---|
| Accuracy | Not good | Good | Excellent | Best of all |
| Speed | Fast | Fast | A little lower | Highest cost |
| Explainability | Best — you wrote the rule | Moderate | Not very explainable | Inherits the issue |
| Scalability | Poor — how many rules can you write? | Scales with data | Scales with data | Scales with compute |
The general guidance: start simple and go to the complex; choose the LLM-based approach only when you have a requirement of near-100% accuracy, you have a lot of compute power, and explainability is not a big criterion.
6.3.6 Traditional POS tags and open word classes
The traditional English set has eight parts of speech: noun, pronoun, verb, adjective, adverb, preposition, determiner (article), and interjection, plus auxiliaries and conjunctions.
Q: What are the different traditional parts of speech in English? (Class answers: noun, pronoun, verb, adverb, adjective, preposition, determiner, interjection.)
A: Right — noun, pronoun, verb, adjective, adverb, preposition, determiner, interjection, and also auxiliaries and articles. On top of these eight, the finer-grained tags we just listed exist.
Fine-grained variants exist in industry: past and present tense of verbs, past participle, plural and singular nouns, proper and common nouns, comparative and superlative adjectives (big, bigger, biggest), different pronoun categories, and even tags for special characters like "and" and commas.
Open vs closed word classes — a very useful concept that applies to every language: English, German, Hindi, Marathi, French, Spanish, Sanskrit.
- Closed classes have a fixed set of vocabulary: articles are just a few (a, an, the); prepositions are a fixed set (of, in, by, to). For a closed class you can do the tagging by dictionary lookup — check which part-of-speech tag the word belongs to — so it is very easy.
- Open classes change over time: new nouns appear all the time (names of people, places, hotels, organizations, courses); verbs change — Gen Z has come up with its own vocabularies — and meanings shift: "cool" used to mean somebody was not good, and now it means good. Words are added, words are deleted, meanings change; that is why language is interesting — it is evolving.
Nouns, verbs, adjectives, and adverbs are the open classes. Closed classes are easy to tag; open classes are the hard part.
Analogy: picture a board game. The rule words printed on the box never change — "roll," "pass," "skip." That is the closed class: a small, fixed set. The sticky notes players add for a house rule keep growing forever — that is the open class. Grammar runs on a fixed set of glue words plus an open flood of content words, which is why dictionary lookup works for the first group and fails for the second.
6.3.7 The Penn Treebank tag set
Real-world: in practice the tags are not written out in full. The Penn Treebank tag set, developed at Penn, uses short abbreviations, and it is what NLTK and spaCy expose (each library has its own similar convention, with minor variations in the number of tags and how they are represented, but the idea is the same). The Penn Treebank set has 45 POS tags.
A tagged sentence from the Penn set looks like this — the capital-letter strings above the words are the tags the system assigned:
| The | grand | jury | commented | on | a | number | of | other | topics | . |
|---|---|---|---|---|---|---|---|---|---|---|
| DT | JJ | NN | VBD | IN | DT | NN | IN | JJ | NNS | . |
DT is a determiner (the, a), JJ an adjective (grand, other), NN a singular noun (jury, number), VBD a past-tense verb (commented), NNS a plural noun (topics), and the full stop has its own tag (.).
Two tags deserve special mention because the words are so common in English: IN, the special tag for the preposition "in," and TO, the special tag for the preposition "to."
Pitfall: the single IN tag covers both prepositions and subordinating conjunctions — "although" in "although I left" is also IN. The word "to" is the exception: it gets its own tag, TO, not IN. When you read a tagged sentence, do not assume every two-letter tag maps to a single grammar role.
6.3.8 Ambiguity: why tagging is hard
Natural language is very ambiguous, and this is the core difficulty of automatic tagging. Consider the single word "back" — it takes a different tag in each of these four sentences:
Worked example: the four tags of "back".
- "back door" — back describes the door, so it is an adjective: back/JJ.
- "on my back" — back is a body part, a thing, so it is a noun: back/NN (the standard tag; the sentence decides the class, not the dictionary).
- "win the voters back" — back tells how, so it is an adverb: back/RB.
- "promise to back the bill" — back is the action, so it is a verb: back/VB.
One spelling, four tags. The only thing that changed was the words nearby.
Sense-check: the same word earns four different lexical class markers in four different sentences, which is exactly why tagging cannot be done word by word in isolation.
So the tag of a word cannot be determined in isolation; you have to look into the context of the word. Similarly, "race" can be a noun or a verb: in one context it is a verb, in another context ("the race of a human being") it is a noun. The classic triple "I can can the can" shows the same effect: the first can is a modal verb (be able to), the second is a main verb (put in a can), the third is a noun (the container) — three roles for one spelling in one sentence.
Recap: tagging is choosing, not looking up. A word like "back" brings several possible tags; the sentence around it decides which one fits here. For one sentence there can be multiple plausible tag sequences, and the goal of a POS tagger is to find the best tag sequence for the given sentence — which is exactly the problem the hidden Markov model solves in the next topic.
6.5 The Ice Cream Example and Viterbi
6.5.1 Setup: Jason's ice cream diary
Hook: You are a climate scientist with no weather records for one old summer — but you find a diary that lists how many ice creams someone ate each day. Can you reconstruct the weather from ice cream alone?
The best-known worked example for HMM decoding comes from the Jurafsky and Martin textbook appendix. A certain Jason kept a diary recording how many ice creams he ate during every day of the summer, and his records were used to do weather prediction — and, interestingly, to study global warming.
The model. The simplified example has two states: hot and cold. There are three possible observations: Jason eats 1, 2, or 3 ice creams in a day. The states (hot/cold) play the role of the POS tags, and the observations (ice cream counts) play the role of the words.
The transition and emission probabilities — the numbers on the arrows and in the boxes of the diagram — were estimated from Jason's records, exactly the way we estimated probabilities from the three labeled sentences above: first day he ate one ice cream while the weather was hot, second day two ice creams while cold, and so on; counting over the records gives the probabilities.
| Parameter | Meaning | Value |
|---|---|---|
| day one is hot | 0.8 | |
| day one is cold | 0.2 | |
| , | transitions from hot | 0.7, 0.3 |
| , | transitions from cold | 0.4, 0.6 |
| , , | emissions on hot days | 0.2, 0.4, 0.4 |
| , , | emissions on cold days | 0.5, 0.4, 0.1 |
Your task: given the observation sequence "Jason ate 1 ice cream, then 3, then 1," predict the weather sequence — what was the weather on each of the three days?
Many weather sequences are consistent with the observations. Maybe the weather was hot, cold, hot; but on cold days Jason sometimes ate two ice creams, so cold, cold, cold is also possible, and he also ate one ice cream on hot days, so hot, hot, hot is possible. For a two-state, three-observation example there are candidate weather sequences — HHH, HHC, HCH, HCC, CHH, CHC, CCH, CCC — and we compute the probability of each with the HMM model; whichever has the highest probability is the prediction.
Visual intuition. The ice-cream HMM diagram has two boxes, HOT and COLD, with curved arrows between them carrying the transition values (0.7/0.3 out of hot, 0.4/0.6 out of cold) and a start arrow into HOT carrying 0.8. Under each box, three arrows drop down to the counts 1, 2, 3 carrying the emission values. The picture is the same state diagram as the weather chain, with the tags replaced by weather states and the words replaced by ice cream counts.
6.5.2 Worked example: the probability of one weather sequence
Worked example: scoring the candidate sequence cold, hot, cold for observations 1, 3, 1.
The computation walks through step by step, one factor per position.
Step 1 — start. The initial probability of the second state, cold, is (state one is hot, state two is cold).
Step 2 — day 1 emission. The probability of 1 ice cream given cold is .
Step 3 — transition. Hot given cold — the transition from cold to hot is .
Step 4 — day 2 emission. 3 ice creams given hot is .
Step 5 — transition back. Cold given hot — the transition back is .
Step 6 — day 3 emission. The last day's emission: 1 ice cream given cold, again.
Step 7 — multiply all six factors:
The probability of the cold, hot, cold sequence is 0.0024.
Sense-check: multiplying six fractions under 1 always shrinks the product, so a small number like 0.0024 is expected; what matters is comparing it against the other seven candidates.
To see how the comparison plays out, score two more of the eight candidates with the same six-factor product. For hot, hot, hot (day-1 observation 1, day-2 observation 3, day-3 observation 1):
For cold, cold, cold:
The remaining five candidates (HHC, HCH, HCC, CHH, CCH) are scored with the same recipe, and the largest product wins. Every sequence probability needs both the transition factors and the emission factors — that is the point of the example.
6.5.3 Student Questions and Answers
Q: We know the probability of 1 ice cream given cold. Using Bayes' theorem we can find the probability of hot given 1. If we do this for each of the three days, can we always say which weather is the maximum — hot or cold — using pure mathematics?
A: No — because we have to take the joint probability. It is not just the ice cream emissions; the transitions between the weather states also have to be considered. That is exactly why we multiply the transitions and emissions together — the joint probability of the whole weather sequence with the whole observation sequence.
6.5.4 Why we need Viterbi
The ice cream example already shows the problem with the naive approach: two states and three observations forced eight sequence computations to find the argmax. Scale that to POS tagging:
The enumeration explosion. The Penn Treebank has 45 tags, and a typical English sentence has 8 to 10 words, so a naive search considers about tag sequences per sentence:
In general, for tags and words there are tag lists — for the ice cream example, ; for POS tagging, roughly 17 trillion per sentence. That is computationally heavy for every sentence.
Visual intuition. Picture a grid: one column per word position, one row per tag — 45 rows and 8 columns. Naive search would enumerate every possible walk down the columns. Viterbi instead fills the grid left to right, and in each cell keeps only the single best path that reaches that cell; one max per cell replaces the full enumeration, and the search never explodes.
The optimization is Viterbi. Viterbi is the same hidden Markov model algorithm, with the full enumeration replaced by greedily selecting the maximum probability at every point. The intuition: at each step you keep only the most probable partial path into each tag, so the search stays proportional to the grid instead of the exponentially many walks through it.
The full Viterbi algorithm, its worked example, and MEMM (the maximum entropy Markov model) are completed in the next session.
Exam note: you can expect mathematical problems on this for the mid sem exam, so the next session matters — it completes Viterbi and MEMM and finishes the pre-mid sem syllabus.
Exam Guidance Summary
- LLMs, prompt engineering, and Gen AI are introductions only. There won't be many exam questions on them; the mathematical problems come from the earlier topics — skip-gram, negative sampling, neural language modeling, and evaluation. Read this part for understanding and for a flavor of the state of the art, not for exam problems.
- POS tagging: nothing to memorize about tags. No question asks you to define a part-of-speech tag, and you will not be asked to figure out the tags of a sentence. If a POS tagging problem appears, the tag table will be provided in the exam paper.
- HMM and Viterbi: mathematical problems are expected. Expect mathematical problems on the HMM material for the mid sem exam. The mid sem exam syllabus is the pre-mid sem syllabus, which is completed in the next session (Viterbi + MEMM), followed by a full recap session of the pre-mid sem syllabus.
- Practice the counting method. Practice computing transition and emission probabilities from a small labeled corpus with the counting method, and be careful with the direction of transition probabilities — the previous tag conditions the next tag. The tag sequence NNP, VB, DT, NNPS, STOP scoring 0.0076 and the race disambiguation (verb reading about 850 times the noun reading) are the two worked patterns to rehearse.
Key Industry Applications
- LLMs and Gen AI: text summarization (Perplexity for research papers), machine translation, search and information retrieval including enterprise search, conversational AI assistants, question answering, sentiment analysis, code generation (text-to-SQL, no-code platforms), image generation, voice generation (Whisper), video generation, 3D models, and virtual reality.
- POS tagging: voice assistants (command identification), agentic AI and tool/API calling (function-name keywords), search engine disambiguation (cricket the game vs the insect), information extraction and named entity recognition, conversational AI slot filling ("book me a flight from this source to this destination"), grammar checking, machine translation (proper nouns are retained, verbs are translated), social media sentiment analytics (adjectives), text-to-speech, and biomedical applications.
- Sequence learning models generally: information extraction, semantic role labeling (source vs destination roles), and bioinformatics — including a cancer research project where gene sequences were analyzed to identify faulty genes and enable personalized treatment that spares the good cells.
NLP Lecture 6 Notes · Large Language Models, Prompt Engineering, and Part-of-Speech Tagging
Sections Breakdown
What makes an LLM large (parameters, data, compute), pre-training and fine-tuning, and LLM use cases.
Prompts, the context window, zero-shot and few-shot inference, generative AI, and when not to use LLMs.
What POS tagging is, sequence learning, real-world applications, approaches, open vs closed classes, the Penn Treebank, and ambiguity.
The HMM setup, Markov chain warm-up, posterior/prior/likelihood, bigram and independence assumptions, and counting probabilities from labeled data.
Jason's ice cream diary as HMM decoding, scoring candidate weather sequences, and why Viterbi replaces enumeration.
Exam Revision Notes
Below is the distilled, exam-ready core. Every entry comes from the full explanation above. Use this section for rapid review; return to the main notes when a point needs more context.
Large Language Models (LLMs)
Must-know: An LLM is large in parameters (about 175 billion), data (web-scale pre-training), and compute (A100/H100 GPUs); the parameters are the model weights (attention query/key/value matrices and feed-forward weights) learned by backpropagation and gradient descent. LLM content is introduction-only for exams.
⚠️ Top pitfall: Confusing parameters with input tokens: parameters are the learned weights of the transformer (Q, K, V matrices, feed-forward weights), not the tokens fed to the model.
Self-check: Why can only very large organizations train LLMs from scratch, and which Llama 3.1 variant would you pick to run a model locally?
Connects to: 6.2
Prompt Engineering and Generative AI
Must-know: Prompt engineering is introduction-only content: expect mathematical problems on skip-gram, negative sampling, neural language modeling, and evaluation, not on LLMs. Few-shot (a few labeled examples in the prompt) beats zero-shot for enforcing an output format.
⚠️ Top pitfall: Forgetting the context window: LLMs generate long outputs, so an unrestricted answer can exceed the token budget and cost more money and energy — always restrict the output length.
Self-check: Why does zero-shot sentiment classification fail on a movie review, and what change fixes it?
Connects to: 6.1
Part-of-Speech Tagging: The Problem
Must-know: No exam question asks you to define a part-of-speech tag or to figure out the tags of a sentence; the tag table is provided in the exam. Know why tagging is hard (ambiguity: 'back' as JJ/NN/RB/VB, context decides) and the open vs closed class distinction.
⚠️ Top pitfall: Tagging each word with its own most common tag ignores the neighbors and breaks on words like 'back' and 'race', whose tag depends on the surrounding context.
Self-check: Why would an agentic system misbehave on 'set the timer for oven to 10 minutes' if it ignores the POS tags?
Connects to: 6.4, 6.5
Statistical POS Tagging with the Hidden Markov Model
Must-know: HMM tagging: P(t_1^n | w_1^n) is proportional to likelihood x prior; the bigram and independence assumptions factor it into product of emissions and transitions; both tables come from counting in a labeled corpus. Expect mathematical problems on this HMM material in the mid sem exam.
⚠️ Top pitfall: Reading the transition in the wrong direction: in NNP -> VB -> DT the factor is P(DT|VB) = 0.25, not P(VB|DT) = 0.05; the 0.05 belongs to the reverse arrow. Also, a zero count in any factor zeroes the whole sequence product.
Self-check: In the three-sentence corpus, why is P(STOP|NNS) = 1/4 and P(the|DT) = 3/4?
Connects to: 6.3, 6.5
The Ice Cream Example and Viterbi
Must-know: Score a candidate weather/tag sequence by multiplying transitions and emissions together (the joint probability), never emissions alone. Naive enumeration is 2^3 = 8 for ice cream but ~45^8 for POS tagging, so Viterbi keeps only the most probable partial path at each point. Mathematical problems on Viterbi are expected in the mid sem exam.
⚠️ Top pitfall: Using Bayes' theorem on the emissions alone (hot given 1, cold given 1) day by day: the answer requires the joint probability — the transitions between weather states must be multiplied in, not just the emissions.
Self-check: How many candidate weather sequences are there for three days with two states, and why is the same enumeration infeasible for POS tagging?
Connects to: 6.4, 7.1
Exam Guidance Summary
Must-know: HMM and Viterbi material carries the mathematical exam problems (mid sem); LLM/prompt content is introduction-only; the POS tag table is provided in the exam, so nothing to memorize.
⚠️ Top pitfall: Reading transition probabilities in the wrong direction — the previous tag conditions the next tag (P(DT|VB) = 0.25, not P(VB|DT) = 0.05).
Self-check: Which topics will the mathematical exam problems come from, and which topics are introduction-only?
Connects to: 6.4, 6.5
Key Industry Applications
Must-know: LLM/Gen AI applications are broad (text, code, image, voice, video); POS tagging is the enabling layer for voice assistants, agentic tool calls, NER, and translation.
Self-check: Where does POS tagging sit inside a machine translation pipeline (proper nouns retained, verbs translated)?
Connects to: 6.1, 6.2, 6.3
Was this lecture useful?
BitsNotes AI Assistant
Subject Notes AssistantConfigure AI Chat
Choose how to access the chatbotSigned in as
Powered by BitsNotes — 20 messages per day. No API key needed. Want unlimited access? Use "Bring Your Own Key" mode.
Sign in to use AI Chat
Get 20 free AI messages per day to ask questions about your lecture notes. Sign in with Google or GitHub — it takes 5 seconds.
Sign In to BitsNotesSwitch to "Bring Your Own Key" tab above for unlimited access with any OpenAI-compatible provider.