Text & Social Media Analytics

Chapter 13
Deep Learning, Transformers and Generative AI

Post Graduate Program in Business Analytics
Text & Social Media Analytics   •   Post Graduate

Deep Learning, Transformers and Generative AI

Introduction

Text and social media analytics has moved from counting words and classifying documents toward modelling meaning, context, interaction and generation. Deep learning enables systems to learn representations directly from text, while transformer architectures provide the computational foundation for most contemporary language models. Foundation models and large language models (LLMs) can summarize customer conversations, extract entities, classify opinions, answer questions over corporate documents and generate synthetic text. Their usefulness, however, depends on data quality, task design, evaluation, governance and cost control.

For business analysts, the central question is not simply whether a model is powerful. It is whether the model is appropriate, reliable, explainable enough for its use, legally permissible and economically justified. This chapter develops that perspective. It explains transformer architecture, foundation models, prompting, fine-tuning, retrieval-augmented generation (RAG), synthetic data, evaluation and the management of hallucination, privacy, bias and operational risk.

Learning Objectives

By the end of this chapter, you should be able to:

  1. Explain why attention and transformers are effective for text and social media analytics.
  2. Distinguish foundation models, LLMs, encoder models, decoder models and encoder–decoder models.
  3. Design prompts and select between prompting, fine-tuning and RAG.
  4. Evaluate generated and predictive outputs using task-specific and risk-sensitive measures.
  5. Identify and mitigate hallucination, privacy, bias, security, cost and governance risks.
  6. Propose a business-oriented generative AI solution and defend its design choices.

1. From Deep Learning to Language Models

Traditional text analytics commonly represents a document using a bag of words, n-grams or manually engineered features. Such representations are useful but often lose word order, context and semantic relationships. Deep learning uses multilayer neural networks to learn representations from data. In language applications, a token—such as a word, subword or punctuation mark—is mapped to a numerical vector called an embedding.

A neural layer can be expressed generally as:

h = φ(Wx + b)

Here, x is an input vector, W is a learned weight matrix, b is a bias vector, φ is a nonlinear activation function, and h is the output representation. Dimensions must be compatible: if x has d input features and the layer has k units, W has dimensions k × d, b and h have k components. The equation describes inference as well as training; during training, W and b are adjusted to reduce a loss function.

For a supervised task, a common objective is empirical risk minimization:

θ* = arg min_θ (1/n) ∑ᵢ₌₁ⁿ L(f_θ(xᵢ), yᵢ)

where θ denotes model parameters, n is the number of training examples, fθ is the model, xi is an input, yi is its target and L is a loss function. The formula assumes that training examples are represented consistently and that the selected loss reflects the business task. For multiclass classification, cross-entropy is commonly used:

L = −∑c₌₁ᶜ yc log(pc)

where yc is 1 for the observed class c and 0 otherwise, and pc is the model probability assigned to class c. Probabilities must be non-negative and sum to one.

Recurrent neural networks process tokens sequentially. They can model order but may struggle with long-range dependencies and parallel training. Convolutional models capture local patterns efficiently but are less naturally suited to arbitrary relationships between distant tokens. Transformers address these limitations through attention.

2. Attention and the Transformer Architecture

2.1 Self-attention

Self-attention allows each token to assign different weights to other tokens when constructing its contextual representation. For an input sequence, the model produces queries Q, keys K and values V through learned linear transformations. Scaled dot-product attention is:

Attention(Q, K, V) = softmax(QKᵀ / √dₖ)V

Q, K and V are matrices whose rows correspond to token positions; dk is the dimensionality of each key vector. The transpose creates pairwise query–key scores. Division by √dk controls the magnitude of scores, helping keep the softmax numerically stable. Softmax converts each row of scores into non-negative weights that sum to one. The output is a weighted combination of value vectors.

Attention is content-dependent: the representation of a word changes according to surrounding words. In “the bank approved the loan,” the context differs from “the boat reached the bank.” This contextualization is particularly important in social media, where abbreviations, sarcasm, code-switching and rapidly changing meanings are common.

2.2 Multi-head attention and positional information

A transformer uses multiple attention heads. Each head can learn different relationships, such as syntactic dependency, topic association or reference resolution. The head outputs are concatenated and projected into the model dimension. Because attention alone does not inherently encode sequence order, transformers add positional information. Positional encodings may be fixed, learned or represented through relative-position mechanisms.

A standard transformer block combines attention with a position-wise feed-forward network, residual connections and normalization. A simplified sequence is:

  1. Compute attention-based contextual representations.
  2. Add the input through a residual connection.
  3. Normalize the result.
  4. Apply a feed-forward network independently at each position.
  5. Apply another residual connection and normalization.

The feed-forward sublayer may be written as:

FFN(x) = W₂ σ(W₁x + b₁) + b₂

The exact ordering of normalization and residual operations differs among architectures. These details affect optimization, stability and computational performance.

2.3 Computational implications

For a sequence of length n, full self-attention generally requires O(n²) pairwise score computation and O(n²) attention-memory growth. This can become expensive for long documents. Long-context variants, sparse attention, sliding windows, compression and retrieval are approaches for reducing this burden. “Long context” also does not guarantee that a model will use every part of a document accurately; information placement and retrieval quality remain important.

3. Foundation Models and Large Language Models

A foundation model is trained on broad data and designed to support many downstream tasks. An LLM is a foundation model specialized primarily in language, although contemporary models may also process images, audio or structured inputs. Scale is not a sufficient definition: data composition, training objective, architecture, alignment procedures, context length and evaluation all matter.

3.1 Principal transformer families

FamilyTypical mechanismCommon usesMain limitation
Encoder-onlyBidirectional attention over an inputClassification, tagging, embeddings, retrievalNot naturally optimized for open-ended generation
Decoder-onlyCausal attention over preceding tokensCompletion, dialogue, generation, agentsMay require careful prompting for classification and factual tasks
Encoder–decoderEncoder reads input; decoder generates outputTranslation, summarization, structured transformationMore complex serving and model selection

Decoder-only language models are commonly trained with next-token prediction. Given tokens t1,…,tT, the objective is:

LNLL = −∑ₜ₌₁ᵀ log p_θ(tₜ | t₁,…,tₜ₋₁)

This is negative log-likelihood. During training, the model learns to assign probability to the observed next token. At generation time, it repeatedly predicts a distribution and selects a token according to a decoding strategy.

A temperature-adjusted distribution can be described as:

pᵢ(T) = exp(zᵢ/T) / ∑ⱼ exp(zⱼ/T)

where zi is the logit for token i and T > 0 is temperature. Lower T makes the distribution more concentrated; higher T makes it flatter. Temperature changes randomness, not factual knowledge. Top-k and nucleus (top-p) sampling further restrict candidate tokens, but no decoding parameter can guarantee truthfulness.

3.2 Pre-training, adaptation and alignment

Pre-training learns broad linguistic and world-pattern representations from large corpora. Adaptation may include supervised fine-tuning on task examples, instruction tuning, preference optimization or parameter-efficient methods such as adapters and low-rank updates. Alignment attempts to make outputs more useful, safe and consistent with specified preferences. It does not eliminate errors, hidden biases or conflicts between user instructions and organizational policy.

A model’s apparent fluency is not evidence that it possesses a verified database of facts. It generates likely continuations conditioned on input and learned parameters. Consequently, a polished answer can still be unsupported, outdated or wrong.

4. Prompting as Analytical Design

A prompt is an interface specification, not merely a question. A robust prompt typically defines the role or task, relevant context, output format, constraints, evidence requirements and failure behavior.

A practical template is:

Task: classify each customer post into one permitted category.

Context: provide the product, market and definitions of categories.

Input: delimit the text clearly.

Output contract: return valid JSON with fields label, confidence, and evidence_span.

Constraints: do not infer protected characteristics; use unknown when evidence is insufficient.

Quality control: explain what evidence supports the label and abstain when categories are ambiguous.

Zero-shot prompting supplies instructions without examples. Few-shot prompting adds representative demonstrations. Structured output reduces downstream parsing errors, but syntactically valid JSON does not guarantee semantic validity. Chain-of-thought should not be treated as a guaranteed reasoning audit; for production systems, concise rationales, evidence spans, tool traces and independently checked intermediate results are generally safer and easier to govern.

Prompt testing should use a fixed evaluation set containing ordinary, ambiguous, adversarial and out-of-distribution examples. Version prompts as carefully as code. Record model version, retrieval configuration, temperature, token usage and output validators.

5. Choosing Between Prompting, Fine-tuning and RAG

These methods solve different problems.

Prompting

Use prompting when the task is reasonably general, requirements change frequently, labelled data are limited and the required knowledge can be supplied in the input. It is usually the quickest starting point.

Fine-tuning

Fine-tuning changes model parameters using task-specific examples. It is useful for stable style, repeated classification patterns, specialized terminology or a consistent output format. It does not automatically provide current facts. Fine-tuning on confidential data can create privacy and memorization risks, and poor examples can reinforce organizational bias.

Parameter-efficient fine-tuning updates a smaller set of parameters or an adapter rather than the full model. This can reduce compute and storage requirements, although it still requires careful data governance and evaluation.

Retrieval-augmented generation

RAG retrieves relevant documents or passages and places them in the model’s context before generation. A typical pipeline is:

  1. Ingest and clean authoritative documents.
  2. Split them into meaningful passages.
  3. Create embeddings and store them in a vector index.
  4. Retrieve candidates for a user query.
  5. Optionally rerank candidates using a stronger relevance model.
  6. Generate an answer constrained to the retrieved evidence.
  7. Return citations, confidence indicators or an abstention when evidence is inadequate.

RAG is appropriate when knowledge changes, sources must be cited or access permissions vary. Its principal weakness is retrieval failure: an LLM cannot reliably cite evidence that was not retrieved. Access control must operate before retrieval, not merely in the prompt.

A useful managerial decision rule is: use prompting for instructions, fine-tuning for repeated behavior, and RAG for changing or organization-specific knowledge. Hybrid systems are often best.

6. Evaluation of Predictive and Generative Systems

Evaluation must be aligned with the intended decision. A single benchmark score is inadequate.

For binary classification, accuracy is:

Accuracy = (TP + TN) / (TP + TN + FP + FN)

Precision and recall are:

Precision = TP / (TP + FP)
Recall = TP / (TP + FN)

F1 is the harmonic mean:

F1 = 2 × Precision × Recall / (Precision + Recall)

TP, TN, FP and FN denote true positives, true negatives, false positives and false negatives. These measures assume a defined positive class and labelled ground truth. Accuracy can be misleading under class imbalance; for example, a model that always predicts the majority class may appear strong while missing all urgent complaints.

For generation, evaluation should examine multiple dimensions:

DimensionExample question
GroundednessAre claims supported by supplied evidence?
Factual correctnessCan claims be verified independently?
RelevanceDoes the response address the user’s question?
CompletenessAre important points omitted?
SafetyDoes it avoid harmful or prohibited content?
Style and formatDoes it satisfy the output contract?
FairnessDoes performance vary across relevant groups or dialects?
Operational valueDoes it improve time, quality or decision outcomes?

Automatic metrics such as ROUGE or BLEU can compare overlap with reference text, but lexical overlap is not equivalent to truth or usefulness. Embedding-based similarity can reward semantically related but factually incorrect text. Human evaluation, expert review, claim-level verification and task outcome measures remain important.

Evaluation should include a held-out test set, slice analysis, stress tests, red teaming, calibration checks and monitoring after deployment. If a model produces a confidence score, calibration should be assessed rather than assumed. In a calibrated system, among predictions assigned probability p, approximately p proportion should be correct; this property must be tested empirically.

7. Hallucination, Bias, Privacy and Security

Hallucination and uncertainty

Hallucination is an output that is unsupported, fabricated or factually incorrect. Causes include incomplete context, ambiguous prompts, weak retrieval, distribution shift, decoding variability and pressure to answer every question. Mitigations include authoritative retrieval, citation requirements, claim verification, constrained generation, abstention, tool use and human review. “I do not know” must be an acceptable system outcome.

Bias and representational harm

Bias may enter through training data, labels, sampling, language variation, prompt design or deployment decisions. Social media data are not neutral: some communities are overrepresented, some users are bots, and dialects can be misclassified as toxic or low quality. Evaluate performance by relevant language varieties, demographic groups where lawful and appropriate, content types and use cases. Do not infer sensitive attributes merely to create demographic reports without a legitimate basis and safeguards.

Privacy and confidentiality

Posts may contain personal data even when publicly visible. Risks include re-identification, memorization, unauthorized disclosure, prompt injection and sending confidential content to an external provider. Apply data minimization, purpose limitation, retention controls, access management, encryption and vendor due diligence. Redact or tokenize identifiers where possible. Maintain a clear distinction between public availability and permission for commercial processing.

Security and prompt injection

An attacker may place instructions inside a retrieved document or user message that attempt to override system rules, exfiltrate data or trigger unauthorized tools. Treat retrieved content as untrusted data, separate instructions from evidence, restrict tool permissions, validate outputs and log actions. Never allow a language model to decide alone whether it may access sensitive systems.

8. Cost, Infrastructure and Governance

Generative AI cost is driven by input tokens, output tokens, model pricing, request volume, latency requirements, retrieval, storage and human review. A simple monthly estimate is:

Monthly cost ≈ N × (I × Cᵢ + O × Cₒ) / 1,000,000 + Cfixed

N is the number of requests per month, I and O are average input and output tokens per request, Ci and Co are prices per million input and output tokens, and Cfixed includes fixed infrastructure and governance costs. The estimate assumes pricing is linear and excludes exceptional charges, taxes and human labour. Actual provider pricing must be checked before procurement.

Cost controls include shorter context, document chunking, caching, routing simple tasks to smaller models, batching, retrieval filters, output limits and reducing unnecessary generation. Cost reduction must not sacrifice privacy, accuracy or accessibility.

A governance framework should define model ownership, approved providers, data classifications, risk tiers, evaluation gates, incident response, audit logs, user disclosure and retirement procedures. High-impact decisions require stronger validation and meaningful human oversight. Depending on jurisdiction and application, data protection, consumer protection, employment, copyright and emerging AI regulation may apply. Legal review is not a substitute for technical controls, and technical controls are not a substitute for accountability.

9. Synthetic Data

Synthetic data are artificially generated examples designed to resemble selected properties of real data. They can support rare-event testing, privacy-preserving development, augmentation of underrepresented classes and simulation of customer conversations. They are not automatically private or unbiased. A generator may reproduce memorized personal information or amplify patterns in its source data.

A responsible workflow defines the intended use, generates data under constraints, checks for duplicates and sensitive content, compares distributions and label relationships with real data, and tests models trained on synthetic data against an independent real-world test set. Synthetic data should supplement—not silently replace—representative empirical data. Document the generation model, prompts, filters, sampling process and known limitations.

10. Applied Case: A Retailer’s Social Listening Assistant

Background

An illustrative national retailer receives approximately 100,000 public posts and service messages per month. Analysts currently read samples manually, classify complaints and prepare weekly summaries. Management wants faster detection of delivery failures and product-quality issues without allowing an LLM to make refunds or disciplinary decisions.

Problem

The retailer faces noisy multilingual text, sarcasm, duplicate posts, privacy concerns and rapidly changing product information. A general chatbot could produce fluent but unsupported summaries and could expose customer identifiers.

Analysis

The team defines four outputs: issue category, urgency, evidence span and a weekly evidence-linked summary. Labels are created from a stratified sample, including non-English posts, short posts, slang and difficult cases. Performance is assessed separately for urgent delivery complaints and ordinary product discussion because the costs of false negatives differ.

Approach and solution

An encoder model is fine-tuned for multilabel issue classification using approved, redacted examples. A RAG component retrieves current delivery policies, product notices and incident records from permission-controlled indexes. A decoder model generates summaries only from retrieved passages and links each claim to source posts or official records. Prompts require an unknown value when evidence is insufficient. A deterministic validator checks JSON structure, prohibited personal data and unsupported citation identifiers.

The system routes high-risk cases to analysts. It does not infer protected characteristics, determine compensation or contact customers automatically. Prompt, model and retrieval versions are logged. A monthly audit examines error rates by language variety, product category and urgency level. Analysts can correct labels, but corrections enter training only after review.

Outcome

In the pilot, analysts report shorter triage time and improved consistency, while the system’s principal value comes from prioritization and evidence organization rather than autonomous writing. The retailer rejects a proposed metric based only on summary fluency and instead tracks urgent-issue recall, citation validity, analyst override rate, privacy incidents, cost per processed post and time to escalation.

Learning points

The case demonstrates that a successful generative system is a governed workflow. Fine-tuning addresses stable classification behavior; RAG addresses changing knowledge; prompting establishes output constraints; human review manages consequential uncertainty. The business outcome depends on the complete system, not on the language model alone.

11. Contemporary Developments and Best Practices

Multimodal models increasingly combine text with images, audio and video, which is relevant to social commerce and brand monitoring. Smaller open-weight models enable local or private deployment but transfer responsibility for security, licensing, evaluation and infrastructure to the organization. Agentic systems can call tools and plan multi-step tasks, yet each additional tool increases the attack surface and makes authorization essential. Long-context models, mixture-of-experts architectures, quantization and retrieval improvements may reduce cost or extend capability, but they do not remove the need for evidence and evaluation.

Best practice is to begin with a narrowly defined decision problem and a baseline. Establish data permissions and a risk classification before selecting a model. Use the least capable model that meets the requirement, but test it rigorously. Separate retrieval, generation and validation. Prefer citations and abstention to unsupported confidence. Measure subgroup and slice performance. Keep humans responsible for high-impact decisions. Monitor drift, incidents, cost and user behaviour after deployment. Finally, document limitations so that stakeholders understand what the system can and cannot establish.

Chapter Summary

Transformers use self-attention to create context-sensitive representations and have become the dominant architecture for modern language systems. Foundation models and LLMs are broadly pretrained models that can be adapted through prompting, fine-tuning, retrieval and tool use. Prompting specifies tasks and constraints; fine-tuning changes model behaviour using examples; RAG supplies current, organization-specific evidence. None guarantees truth.

Effective text and social media analytics requires evaluation beyond fluency. Classification metrics such as precision, recall and F1 must be connected to business costs, while generative systems require checks for groundedness, correctness, relevance, safety, fairness and operational value. Hallucination, bias, privacy leakage, prompt injection and escalating token costs are system risks. Governance should include data minimization, access control, versioning, human oversight, monitoring and incident response. Synthetic data can support development but must be validated against independent real data. The central professional skill is designing a reliable, evidence-aware and economically justified workflow around a model.

Key Terms

Foundation model: A broadly pretrained model adaptable to many downstream tasks.

Large language model: A language-focused neural model trained to model token sequences at large scale.

Transformer: A neural architecture based primarily on attention mechanisms and feed-forward layers.

Token: A unit processed by a language model, such as a word, subword or symbol.

Embedding: A learned numerical vector representing an item or text segment.

Self-attention: Attention in which tokens compute contextual representations using other tokens in the same sequence.

Query, key and value: The three representations used to compute attention scores and weighted outputs.

Encoder: A model component that transforms an input sequence into contextual representations.

Decoder: A component that generates outputs, often autoregressively.

Prompt: Instructions, context and constraints supplied to a model.

Fine-tuning: Updating model parameters using task-specific examples.

Parameter-efficient fine-tuning: Adaptation that updates a smaller parameter set or auxiliary modules.

Retrieval-augmented generation: Generation grounded in documents retrieved for the user’s query.

Hallucination: Unsupported, fabricated or factually incorrect model output.

Groundedness: The degree to which an answer is supported by supplied evidence.

Synthetic data: Artificially generated data intended to reproduce selected properties of real data.

Prompt injection: Malicious or conflicting instructions embedded in user or retrieved content.

Calibration: Agreement between predicted probabilities and observed frequencies.

Token cost: Expense associated with processing input and output tokens.

Human-in-the-loop: A workflow in which people review, correct or authorize model-supported actions.

Review Questions

Short Answer Questions

  1. What problem does self-attention solve in transformer-based language modelling?
  2. Distinguish an encoder-only model from a decoder-only model.
  3. When is RAG generally preferable to fine-tuning?
  4. Define hallucination and state two mitigation strategies.
  5. Why can accuracy be misleading for imbalanced social media classification?

Long Answer Questions

  1. Explain the transformer architecture, including self-attention, positional information, residual connections and feed-forward layers.
  2. Compare prompting, fine-tuning and RAG in terms of data requirements, knowledge updating, cost and risk.
  3. Develop an evaluation framework for an LLM that summarizes customer complaints for senior management.
  4. Discuss privacy, bias, prompt injection and governance risks in an enterprise social listening system.
  5. Explain how synthetic data may be useful and why it cannot automatically be treated as private or representative.

Analytical / Application Questions

  1. A firm needs a system that answers questions about policies changed weekly and must cite authoritative sources. Recommend an architecture and justify the roles of retrieval, prompting, model selection and human review.
  2. A classifier has high overall accuracy but misses urgent complaints in a minority language variety. Design a diagnostic and remediation plan using appropriate metrics, slices and data practices.
  3. Design a cost and risk control plan for a high-volume customer-support LLM. Include token management, routing, privacy safeguards, evaluation gates and escalation rules.

Multiple Choice Questions with Answers

Question 1

Which mechanism allows a transformer token to weight information from other tokens in its sequence?

A. Self-attention

B. Batch normalization

C. Random undersampling

D. Decision pruning

Correct Answer: A. Self-attention

Question 2

In scaled dot-product attention, why is QKᵀ divided by √dₖ?

A. To reduce the number of tokens

B. To control score magnitude and improve softmax stability

C. To convert tokens into labels

D. To guarantee factual accuracy

Correct Answer: B. To control score magnitude and improve softmax stability

Question 3

Which model family is most naturally suited to generating text one token at a time?

A. Encoder-only

B. Decoder-only

C. Non-neural clustering

D. A static term-frequency table

Correct Answer: B. Decoder-only

Question 4

Which technique is primarily intended to supply changing, organization-specific evidence at inference time?

A. Retrieval-augmented generation

B. Removing positional information

C. Increasing classification accuracy by definition

D. Random token deletion

Correct Answer: A. Retrieval-augmented generation

Question 5

If TP = 80 and FP = 20, what is precision?

A. 0.20

B. 0.50

C. 0.80

D. 0. 

Correct Answer: C. 0.80

Question 6

Which statement about temperature is correct?

A. It changes the model’s training data

B. Lower temperature generally makes token selection more concentrated

C. It guarantees that generated claims are true

D. It removes the need for retrieval

Correct Answer: B. Lower temperature generally makes token selection more concentrated

Question 7

What is a principal weakness of using only lexical-overlap metrics for generated summaries?

A. They cannot measure token counts

B. They may reward wording overlap without establishing factual correctness

C. They always require protected attributes

D. They prevent human evaluation

Correct Answer: B. They may reward wording overlap without establishing factual correctness

Question 8

Which control best addresses prompt injection in retrieved documents?

A. Treat retrieved text as trusted system instructions

B. Allow unrestricted tool access

C. Separate instructions from untrusted evidence and validate tool actions

D. Increase temperature

Correct Answer: C. Separate instructions from untrusted evidence and validate tool actions

Question 9

Which method is most appropriate for adapting a model to a stable, repeated classification style using labelled examples?

A. Fine-tuning

B. Deleting the evaluation set

C. Increasing document length without retrieval

D. Ignoring label quality

Correct Answer: A. Fine-tuning

Question 10

Why should synthetic data be tested against an independent real-world test set?

A. To verify that performance transfers beyond artificial examples

B. To ensure every generated record is identical

C. To eliminate the need for data governance

D. To prove that the generator has no bias

Correct Answer: A. To verify that performance transfers beyond artificial examples

Text & Social Media Analytics
Chapter 13 of 15