Artificial Intelligence Applications in Engineering (MUH-920), week 13 of 14: interactive lab

Language lab: Tokens, sampling and grounded retrieval

Prof. Dr. Utku Kose, Süleyman Demirel University

Part A computes TF-IDF weights and cosine similarities by hand [1]. Part B learns byte-pair merges from engineering sentences and shows how any typed text splits into tokens [2]. Part C computes scaled dot-product attention with an optional causal mask, and Part D positional encodings and the effect of the scaling factor [3]. Part E turns next-token scores into probabilities with an adjustable temperature and top-k cut, and samples continuations. Part F collects the numbers of language models: sampling probabilities, perplexity, scaling and energy [4, 5]. Part G retrieves passages from a small engineering knowledge base with TF-IDF and assembles an answer that cites them, as retrieval-augmented generation does [1, 6], and Part H evaluates retrieval. Part I runs a diffusion process and the key quantities of autoencoders and adversarial networks [7].

Part A: TF-IDF and cosine similarity by hand

The three maintenance records and the query of the worked example are the default. The table gives the document frequency and idf of every term, the first panel the weights, length and normalised weights of every vector, and the second panel the cosine of the query with each record, product by product. Records and query can be edited, and the plain idf of the textbook can replace the smoothed one of scikit-learn [1].

Part B: Byte-pair tokenisation

Part C: Scaled dot-product attention step by step

Tokens with small embeddings, by default those of the worked example, pass through the matrices W_Q, W_K and W_V. The heatmap shows the attention weights, and a click on a row writes out its query, scores, exponentials, weights and output; the table lists the vectors of every token, and the causal mask hides later tokens as in a decoder [3].

Rows are queries, columns keys; click a row to write out its arithmetic.

Part D: Positional encodings and the scaling factor

The first panel draws the sinusoidal encodings of the original transformer and how the dot product of two encodings falls with their distance. The second draws random queries and keys and compares the softmax weights with and without the division by √d_k, which shows why large dot products need scaling [3].

Positional encodings

Why the scores are divided by √d_k

Part E: Temperature and top-k sampling

Context: "The concrete cubes were tested after twenty eight ..."

Part F: Language models by the numbers

Four calculators accompany the lecture: sampling probabilities with temperature and top-k, as in Part E but with every exponential written out; cross-entropy and perplexity from the probabilities of the actual tokens; the scaling law of the loss; and the energy and emissions of a training run [4, 5].

Temperature and top-k

Cross-entropy and perplexity

Scaling of the loss

Energy and emissions of a run

Part G: Retrieval with citations

Part H: Evaluating retrieval

Dense retrieval ranks passages by the cosine of their embeddings with the question embedding. The second panel turns the ranks of the relevant passages for a set of test questions into hit rates and the mean reciprocal rank, and the third checks whether instructions, question and passages fit into the context window [6, 8].

Dense retrieval

Hit rate and mean reciprocal rank

The context budget

Part I: Generative models by the numbers

The first panel adds noise to a small image of a crack with the schedule of the worked example or that of Ho and colleagues and shows how the share of the signal falls. The second computes the latent cost and a latent draw of a variational autoencoder, and the third the best discriminator for a data and a generator density [7, 9, 10].

Forward diffusion of a small image

Image at step t

The latent cost of a variational autoencoder

The best discriminator

References

[1] Manning, C. D., Raghavan, P., & Schütze, H. (2008). Introduction to Information Retrieval. Cambridge University Press.

[2] Sennrich, R., Haddow, B., & Birch, A. (2016). Neural machine translation of rare words with subword units. In Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (pp. 1715-1725). https://doi.org/10.18653/v1/P16-1162

[3] Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention is all you need. In Advances in Neural Information Processing Systems 30 (pp. 5998-6008). https://arxiv.org/abs/1706.03762

[4] Kaplan, J., McCandlish, S., Henighan, T., et al. (2020). Scaling laws for neural language models. arXiv preprint arXiv:2001.08361. https://arxiv.org/abs/2001.08361

[5] Strubell, E., Ganesh, A., & McCallum, A. (2019). Energy and policy considerations for deep learning in NLP. In Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (pp. 3645-3650). https://doi.org/10.18653/v1/P19-1355

[6] Lewis, P., Perez, E., Piktus, A., et al. (2020). Retrieval-augmented generation for knowledge-intensive NLP tasks. In Advances in Neural Information Processing Systems 33 (pp. 9459-9474). https://arxiv.org/abs/2005.11401

[7] Ho, J., Jain, A., & Abbeel, P. (2020). Denoising diffusion probabilistic models. In Advances in Neural Information Processing Systems 33 (pp. 6840-6851). https://arxiv.org/abs/2006.11239

[8] Reimers, N., & Gurevych, I. (2019). Sentence-BERT: Sentence embeddings using Siamese BERT-networks. In Proceedings of EMNLP-IJCNLP 2019 (pp. 3982-3992). https://doi.org/10.18653/v1/D19-1410

[9] Kingma, D. P., & Welling, M. (2014). Auto-encoding variational Bayes. In 2nd International Conference on Learning Representations (ICLR 2014). https://arxiv.org/abs/1312.6114

[10] Goodfellow, I., Pouget-Abadie, J., Mirza, M., et al. (2014). Generative adversarial nets. In Advances in Neural Information Processing Systems 27 (pp. 2672-2680). https://arxiv.org/abs/1406.2661

[11] Ouyang, L., Wu, J., Jiang, X., et al. (2022). Training language models to follow instructions with human feedback. In Advances in Neural Information Processing Systems 35 (pp. 27730-27744). https://arxiv.org/abs/2203.02155

[12] Ji, Z., Lee, N., Frieske, R., et al. (2023). Survey of hallucination in natural language generation. ACM Computing Surveys, 55(12), 248. https://doi.org/10.1145/3571730