[{"content":"Building agents with LLM (large language model) as its core controller is a cool concept. Several proof-of-concept demos, such as AutoGPT, GPT-Engineer, and BabyAGI, serve as inspiring examples. The potentiality of LLM extends beyond generating well-written copies, stories, essays, and programs; it can be framed as a powerful general problem solver.\nAgent System Overview In a LLM-powered autonomous agent system, LLM functions as the agent\u0026rsquo;s brain, complemented by several key components:\nPlanning Subgoal and decomposition: The agent breaks down large tasks into smaller, manageable subgoals, enabling efficient handling of complex tasks. Reflection and refinement: The agent can do self-criticism and self-reflection over past actions, learn from mistakes and refine them for future steps. Memory Short-term memory: All the in-context learning as utilizing short-term memory of the model to learn. Long-term memory: This provides the agent with the capability to retain and recall information over extended periods, often by leveraging an external vector store and fast retrieval. Tool use The agent learns to call external APIs for extra information that is missing from the model weights, including current information, code execution capability, and access to proprietary information sources. Component One: Planning A complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition Chain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to \u0026ldquo;think step by step\u0026rdquo; to utilize more test-time computation to decompose hard tasks into smaller and simpler steps.\nTree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS or DFS with each state evaluated by a classifier or majority vote.\nTask decomposition can be done:\nBy LLM with simple prompting like \u0026quot;Steps for XYZ.\\n1.\u0026quot; By using task-specific instructions With human inputs Self-Reflection Self-reflection is a vital aspect that allows autonomous agents to improve iteratively by refining past action decisions and correcting previous mistakes.\nReAct (Yao et al. 2023) integrates reasoning and acting within LLM by extending the action space to be a combination of task-specific discrete actions and the language space. The prompt template incorporates explicit steps for LLM to think:\nT A O h c b o t s u i e g o r ( h n v R t : a e : t p i e o a n t : e d m a n y t i m e s ) Reflexion (Shinn \u0026amp; Labash 2023) is a framework to equip agents with dynamic memory and self-reflection capabilities. After each action $a_t$, the agent computes a heuristic $h_t$ and optionally may decide to reset the environment to start a new trial depending on the self-reflection results.\nComponent Two: Memory Types of Memory Memory can be defined as the processes used to acquire, store, retain, and later retrieve information. There are several types of memory in human brains:\nSensory Memory: The earliest stage of memory, providing the ability to retain impressions of sensory information after the original stimuli have ended.\nShort-Term Memory (STM) or Working Memory: It stores information that we are currently aware of and needed to carry out complex cognitive tasks. Short-term memory has the capacity of about 7 items (Miller 1956) and lasts for 20-30 seconds.\nLong-Term Memory (LTM): Can store information for a remarkably long time, ranging from a few days to decades, with essentially unlimited storage capacity.\nWe can roughly consider the following mappings:\nSensory memory as learning embedding representations for raw inputs; Short-term memory as in-context learning, restricted by the finite context window length; Long-term memory as the external vector store accessible via fast retrieval. Maximum Inner Product Search (MIPS) The external memory can alleviate the restriction of finite attention span. A standard practice is to save the embedding representation into a vector store that supports fast maximum inner-product search (MIPS).\nCommon choices of ANN algorithms for fast MIPS:\nLSH (Locality-Sensitive Hashing): Introduces a hashing function such that similar input items are mapped to the same buckets with high probability. ANNOY (Approximate Nearest Neighbors Oh Yeah): The core data structure are random projection trees. HNSW (Hierarchical Navigable Small World): Builds hierarchical layers of small-world graphs where bottom layers contain actual data points. FAISS (Facebook AI Similarity Search): Applies vector quantization by partitioning the vector space into clusters. ScaNN (Scalable Nearest Neighbors): Main innovation is anisotropic vector quantization. Component Three: Tool Use Tool use is a remarkable and distinguishing characteristic of human beings. Equipping LLMs with external tools can significantly extend the model capabilities.\nMRKL (Karpas et al. 2022), short for \u0026ldquo;Modular Reasoning, Knowledge and Language\u0026rdquo;, is a neuro-symbolic architecture for autonomous agents containing a collection of \u0026ldquo;expert\u0026rdquo; modules where the LLM works as a router.\nBoth TALM (Tool Augmented Language Models; Parisi et al. 2022) and Toolformer (Schick et al. 2023) fine-tune a LM to learn to use external tool APIs.\nHuggingGPT (Shen et al. 2023) is a framework to use ChatGPT as the task planner to select models available in HuggingFace platform. The system comprises 4 stages:\nTask planning: LLM parses user requests into multiple tasks Model selection: LLM distributes tasks to expert models Task execution: Expert models execute on specific tasks Response generation: LLM receives execution results and provides summarized results Challenges After going through key ideas and demos of building LLM-centered agents, several common limitations emerge:\nFinite context length: The restricted context capacity limits the inclusion of historical information, detailed instructions, API call context, and responses. Challenges in long-term planning and task decomposition: Planning over a lengthy history and effectively exploring the solution space remain challenging. Reliability of natural language interface: The reliability of model outputs is questionable, as LLMs may make formatting errors and occasionally exhibit rebellious behavior. Citation Cited as:\nWeng, Lilian. (Jun 2023). \u0026ldquo;LLM-powered Autonomous Agents\u0026rdquo;. Lil\u0026rsquo;Log. https://lilianweng.github.io/posts/2023-06-23-agent/.\nReferences [1] Wei et al. \u0026ldquo;Chain of thought prompting elicits reasoning in large language models.\u0026rdquo; NeurIPS 2022\n[2] Yao et al. \u0026ldquo;Tree of Thoughts: Deliberate Problem Solving with Large Language Models.\u0026rdquo; arXiv preprint arXiv:2305.10601 (2023).\n[3] Yao et al. \u0026ldquo;ReAct: Synergizing reasoning and acting in language models.\u0026rdquo; ICLR 2023.\n[4] Shinn \u0026amp; Labash. \u0026ldquo;Reflexion: an autonomous agent with dynamic memory and self-reflection\u0026rdquo; arXiv preprint arXiv:2303.11366 (2023).\n[5] Karpas et al. \u0026ldquo;MRKL Systems: A modular, neuro-symbolic architecture that combines large language models, external knowledge sources and discrete reasoning.\u0026rdquo; arXiv preprint arXiv:2205.00445 (2022).\n[6] Schick et al. \u0026ldquo;Toolformer: Language Models Can Teach Themselves to Use Tools.\u0026rdquo; arXiv preprint arXiv:2302.04761 (2023).\n[7] Shen et al. \u0026ldquo;HuggingGPT: Solving AI Tasks with ChatGPT and its Friends in HuggingFace\u0026rdquo; arXiv preprint arXiv:2303.17580 (2023).\n[8] Park, et al. \u0026ldquo;Generative Agents: Interactive Simulacra of Human Behavior.\u0026rdquo; arXiv preprint arXiv:2304.03442 (2023).\n","permalink":"https://imflash217.github.io/portfolio/2023-06-23-llm-autonomous-agents/","summary":"An overview of building autonomous agents with LLM as the core controller, covering planning, memory, and tool use components.","title":"LLM Powered Autonomous Agents"},{"content":"numpy v/s pandas One subtle aspect that lot of engineers miss is the access control design of numpy and pandas.\nnumpy uses row-major access control. pandas uses column-major access control. Generally its seen that engineers complain about slow-nature of numpy compared to pandas. One of the prime culprits of this issue is the lack of knowledge about the access control used in these libraries.\nBy default, numpy uses row-major but it allows the user to specify the access control while creating the ndarray. Below, are all scenarios of accessing the row/col using pandas or numpy is shown.\nIterating numpy ndarray using column-major import numpy as np df_np = df.to_numpy() n_rows, n_cols = df_np.shape start = time.time() for j in range(n_cols): for item in df_np[:, j]: pass print(time.time() - start) Iterating numpy ndarray using row-major (DEFAULT) start = time.time() for i in range(n_rows): for item in df_np[i, :]: pass print(time.time() - start) Key Takeaway Library Default Memory Layout Faster Access numpy Row-major (C-order) Row iteration pandas Column-major Column iteration 💡 Tip: When using numpy, iterate over rows. When using pandas, iterate over columns. Or better yet, use vectorized operations!\n","permalink":"https://imflash217.github.io/portfolio/til/042123/","summary":"numpy uses row-major, pandas uses column-major access  -  a subtle performance difference.","title":"numpy v/s pandas"},{"content":"Many new Transformer architecture improvements have been proposed since the original \u0026ldquo;The Transformer Family\u0026rdquo; post about three years ago. Here we do a big refactoring and enrichment - restructuring the hierarchy of sections and improving many sections with more recent papers. Version 2.0 is a superset of the old version, about twice the length.\nNotations Symbol Meaning $d$ The model size / hidden state dimension / positional encoding size $h$ The number of heads in multi-head attention layer $L$ The segment length of input sequence $N$ The total number of attention layers in the model $\\mathbf{X} \\in \\mathbb{R}^{L \\times d}$ The input sequence where each element has been mapped into an embedding vector of shape $d$ $\\mathbf{W}^k \\in \\mathbb{R}^{d \\times d_k}$ The key weight matrix $\\mathbf{W}^q \\in \\mathbb{R}^{d \\times d_k}$ The query weight matrix $\\mathbf{W}^v \\in \\mathbb{R}^{d \\times d_v}$ The value weight matrix Transformer Basics The Transformer (Vaswani, et al., 2017) model has an encoder-decoder architecture, as commonly used in many NMT models. Later simplified Transformer was shown to achieve great performance in language modeling tasks, like in encoder-only BERT or decoder-only GPT.\nAttention and Self-Attention Attention is a mechanism in neural network that a model can learn to make predictions by selectively attending to a given set of data. The amount of attention is quantified by learned weights and thus the output is usually formed as a weighted average.\nSelf-attention is a type of attention mechanism where the model makes prediction for one part of a data sample using other parts of the observation about the same sample.\nTransformer relies on the scaled dot-product attention: given a query matrix $\\mathbf{Q}$, a key matrix $\\mathbf{K}$ and a value matrix $\\mathbf{V}$, the output is a weighted sum of the value vectors:\n$$\\text{Attention}(\\mathbf{Q}, \\mathbf{K}, \\mathbf{V}) = \\text{softmax}\\left(\\frac{\\mathbf{Q}\\mathbf{K}^\\top}{\\sqrt{d_k}}\\right)\\mathbf{V}$$For a query and a key vector $\\mathbf{q}_i, \\mathbf{k}_j \\in \\mathbb{R}^d$, we have a scalar score:\n$$a_{ij} = \\text{softmax}\\left(\\frac{\\mathbf{q}_i \\mathbf{k}_j^\\top}{\\sqrt{d_k}}\\right) = \\frac{\\exp\\left(\\frac{\\mathbf{q}_i \\mathbf{k}_j^\\top}{\\sqrt{d_k}}\\right)}{\\sum_{r \\in S_i} \\exp\\left(\\frac{\\mathbf{q}_i \\mathbf{k}_r^\\top}{\\sqrt{d_k}}\\right)}$$Multi-Head Self-Attention The multi-head self-attention module is a key component in Transformer. Rather than only computing the attention once, the multi-head mechanism splits the inputs into smaller chunks and then computes the scaled dot-product attention over each subspace in parallel:\n$$\\text{MultiHead}(\\mathbf{X}_q, \\mathbf{X}_k, \\mathbf{X}_v) = [\\text{head}_1; \\dots; \\text{head}_h] \\mathbf{W}^o$$$$\\text{where head}_i = \\text{Attention}(\\mathbf{X}_q\\mathbf{W}^q_i, \\mathbf{X}_k\\mathbf{W}^k_i, \\mathbf{X}_v\\mathbf{W}^v_i)$$Positional Encoding Because self-attention operation is permutation invariant, it is important to use proper positional encoding to provide order information to the model.\nSinusoidal Positional Encoding Sinusoidal positional encoding is defined as follows, given the token position $i$ and the dimension $\\delta$:\n$$\\text{PE}(i, \\delta) = \\begin{cases} \\sin\\left(\\frac{i}{10000^{2\\delta'/d}}\\right) \u0026 \\text{if } \\delta = 2\\delta' \\\\ \\cos\\left(\\frac{i}{10000^{2\\delta'/d}}\\right) \u0026 \\text{if } \\delta = 2\\delta' + 1 \\end{cases}$$Rotary Position Embedding Rotary position embedding (RoPE; Su et al. 2021) encodes the absolute position with a rotation matrix and multiplies key and value matrices of every attention layer with it to inject relative positional information at every layer.\nGiven a vector $\\mathbf{z}$, to rotate it counterclockwise by $\\theta$, we multiply by a rotation matrix:\n$$R = \\begin{bmatrix} \\cos\\theta \u0026 -\\sin\\theta \\\\ \\sin\\theta \u0026 \\cos\\theta \\end{bmatrix}$$When generalizing to higher dimensional space, RoPE divides the $d$-dimensional space into $d/2$ subspaces and constructs a block-diagonal rotation matrix. Then both key and query matrices incorporate positional information:\n$$\\mathbf{q}_i^\\top \\mathbf{k}_j = (\\mathbf{R}^d_{\\Theta, i} \\mathbf{W}^q \\mathbf{x}_i)^\\top (\\mathbf{R}^d_{\\Theta, j} \\mathbf{W}^k \\mathbf{x}_j) = \\mathbf{x}_i^\\top \\mathbf{W}^q \\mathbf{R}^d_{\\Theta, j-i} \\mathbf{W}^k \\mathbf{x}_j$$Efficient Attention The computation and memory cost of vanilla Transformer grows quadratically with sequence length and hence it is hard to be applied on very long sequences.\nSparse Attention Patterns A simple alteration to make self-attention less expensive is to restrict the attention span of each token to local context only, so that self-attention grows linearly with the sequence length.\nSparse Transformer (Child et al., 2019) introduced factorized self-attention, through sparse matrix factorization, making it possible to train dense attention networks with hundreds of layers on sequence length up to 16,384.\nLow-Rank Attention Linformer (Wang et al. 2020) approximates the full attention matrix with a low rank matrix, reducing the time \u0026amp; space complexity to be linear:\n$$\\overline{\\text{head}}_i = \\text{softmax}\\left(\\frac{\\mathbf{X}_q \\mathbf{W}^q_i (\\mathbf{E}_i \\mathbf{X}_k \\mathbf{W}^k_i)^\\top}{\\sqrt{d}}\\right) \\mathbf{F}_i \\mathbf{X}_v \\mathbf{W}^v_i$$Content-based Attention The Reformer (Kitaev, et al. 2020) replaces the dot-product attention with locality-sensitive hashing (LSH) attention, reducing the complexity from $\\mathcal{O}(L^2)$ to $\\mathcal{O}(L \\log L)$.\nA hashing scheme $x \\mapsto h(x)$ is locality-sensitive if it preserves the distancing information between data points. The Reformer adopts: $h(x) = \\arg\\max([xR; -xR])$ where $\\mathbf{R} \\in \\mathbb{R}^{d \\times b/2}$ is a fixed random matrix.\nLonger Context The length of an input sequence for transformer models at inference time is upper-bounded by the context length used for training. Naively increasing context length leads to high consumption in both time $\\mathcal{O}(L^2 d)$ and memory $\\mathcal{O}(L^2)$.\nContext Memory Transformer-XL (Dai et al., 2019) modifies the architecture to reuse hidden states between segments with an additional memory:\n$$\\widetilde{\\mathbf{h}}_{\\tau+1}^{(n-1)} = [\\text{stop-gradient}(\\mathbf{h}_{\\tau}^{(n-1)}) \\circ \\mathbf{h}_{\\tau+1}^{(n-1)}]$$$$\\mathbf{Q}_{\\tau+1}^{(n)} = \\mathbf{h}_{\\tau+1}^{(n-1)} \\mathbf{W}^q$$$$\\mathbf{K}_{\\tau+1}^{(n)} = \\widetilde{\\mathbf{h}}_{\\tau+1}^{(n-1)} \\mathbf{W}^k$$Transformers for Reinforcement Learning The self-attention mechanism avoids compressing the whole past into a fixed-size hidden state and does not suffer from vanishing or exploding gradients as much as RNNs.\nDecision Transformer (DT; Chen et al 2021) formulates RL problems as a process of conditional sequence modeling, outputting the optimal actions conditioned on the desired return:\n$$\\tau = (\\hat{R}_1, s_1, a_1, \\hat{R}_2, s_2, a_2, \\dots, \\hat{R}_T, s_T, a_T)$$Three linear layers are added for return-to-go, state, and action respectively. The prediction head learns to predict $a_t$ corresponding to the input token $s_t$.\nCitation Cited as:\nWeng, Lilian. (Jan 2023). The Transformer Family Version 2.0. Lil\u0026rsquo;Log. https://lilianweng.github.io/posts/2023-01-27-the-transformer-family-v2/.\nReferences [1] Vaswani et al. \u0026ldquo;Attention is all you need.\u0026rdquo; NIPS 2017.\n[2] Su et al. \u0026ldquo;Roformer: Enhanced transformer with rotary position embedding.\u0026rdquo; arXiv preprint arXiv:2104.09864 (2021).\n[3] Child et al. \u0026ldquo;Generating Long Sequences with Sparse Transformers\u0026rdquo; arXiv:1904.10509 (2019).\n[4] Wang et al. \u0026ldquo;Linformer: Self-Attention with Linear Complexity.\u0026rdquo; arXiv preprint arXiv:2006.04768 (2020).\n[5] Kitaev et al. \u0026ldquo;Reformer: The Efficient Transformer\u0026rdquo; ICLR 2020.\n[6] Dai et al. \u0026ldquo;Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context.\u0026rdquo; ACL 2019.\n[7] Chen et al. \u0026ldquo;Decision Transformer: Reinforcement Learning via Sequence Modeling\u0026rdquo; arXiv preprint arXiv:2106.01345 (2021).\n[8] Press et al. \u0026ldquo;Train Short, Test Long: Attention With Linear Biases Enables Input Length Extrapolation.\u0026rdquo; ICLR 2022.\n","permalink":"https://imflash217.github.io/portfolio/2023-01-27-the-transformer-family-v2/","summary":"A comprehensive overview of the Transformer architecture and its many variants, covering attention mechanisms, positional encoding, efficient attention patterns, and applications to reinforcement learning.","title":"The Transformer Family Version 2.0"},{"content":"Definition Hash Table is a data structure which stores data in an associative manner (i.e. in a (key, value) pair).\nIn a hash table, the data is stored in an array format where each data-value has its own unique index-value. Due to this feature, the access to data becomes very fast if we know the desired index-value; irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hashing to generate the index where an element is to be inserted or to be located from. Hashing Hashing is a technique to convert a range of key values into a range of indexes of an array.\nDirect-Address Tables (DAT) DAT works well when the universe U of keys is small. But when U is very large then it is IMPRACTICAL to have all |U| slots.\nHash Functions A good hash function satisfies the assumption of simple uniform hashing: each key is equally likely to hash to any of the m slots.\nDivision Method: $$h(k) = k \\mod m$$Multiplication Method: $$h(k) = \\lfloor m(k \\cdot A \\mod 1) \\rfloor$$ where $0 \u003c A \u003c 1$. Knuth suggests $A \\approx \\frac{\\sqrt{5} - 1}{2} = 0.6180339887...$\nCollision Resolution Chaining Each slot of the hash-table holds a linked list of all the elements that hash to the same slot. Expected Time: $O(1 + \\alpha)$ where $\\alpha = n/m$ is the load factor Open Addressing All elements are stored in the hash-table itself. Each slot has either an element or NIL. Linear Probing: $h(k, i) = (h'(k) + i) \\mod m$ Quadratic Probing: $h(k, i) = (h'(k) + c_1 i + c_2 i^2) \\mod m$ Double Hashing: $h(k, i) = (h_1(k) + i \\cdot h_2(k)) \\mod m$ ","permalink":"https://imflash217.github.io/portfolio/algorithms/hashtables/","summary":"Deep dive into hash tables  -  hashing, collision resolution via chaining and open addressing, and hash functions.","title":"Hash Tables"},{"content":"Electromagnetic Spectrum, properties of material medium \u0026amp; its effects. Electromagnetic Spectrum The different parts of the electromagnetic spectrum have very different effects upon interaction with matter. Starting with low frequency radio waves, the human body is quite transparent (you can listen to your portable radio inside your home since the waves pass freely through the walls of your house and even through the person beside you!) As you move upward through microwaves and infrared to visible light, you absorb more and more strongly. In the lower ultraviolet range, all the UV from the sun is absorbed in a thin outer layer of your skin. As you move further up into the x-ray region of the spectrum, you become transparent again, because most of the mechanisms for absorption are gone. You then absorb only a small fraction of the radiation, but that absorption involves the more violent ionization events.\nEach portion of the electromagnetic spectrum has quantum energies appropriate for the excitation of certain types of physical processes. The energy levels for all physical processes at the atomic and molecular levels are quantized, and if there are no available quantized energy levels with spacings which match the quantum energy of the incident radiation, then the material will be transparent to that radiation, and it will pass through. If electromagnetic energy is absorbed, but cannot eject electrons from the atoms of the material, then it is classified as non-ionizing radiation, and will typically just heat the material. Microwaves The quantum energy of microwave photons is in the range 0.00001 to 0.001 eV which is in the range of energies separating the quantum states of molecular rotation and torsion.\nThe interaction of microwaves with matter other than metallic conductors will be to rotate molecules and produce heat as result of that molecular motion. Conductors will strongly absorb microwaves and any lower frequencies because they will cause electric currents which will heat the material. Most matter, including the human body, is largely transparent to microwaves. High intensity microwaves, as in a microwave oven where they pass back and forth through the food millions of times, will heat the material by producing molecular rotations and torsions. Since the quantum energies are a million times lower than those of x-rays, they cannot produce ionization and the characteristic types of radiation damage associated with ionizing radiation. Infrared The quantum energy of infrared photons is in the range 0.001 to 1.7 eV which is in the range of energies separating the quantum states of molecular vibrations.\nInfrared is absorbed more strongly than microwaves, but less strongly than visible light. The result of infrared absorption is heating of the tissue since it increases molecular vibrational activity. Infrared radiation does penetrate the skin further than visible light and can thus be used for photographic imaging of subcutaneous blood vessels. Visible Light The primary mechanism for the absorption of visible light photons is the elevation of electrons to higher energy levels. There are many available states, so visible light is absorbed strongly. With a strong light source, red light can be transmitted through the hand or a fold of skin, showing that the red end of the spectrum is not absorbed as strongly as the violet end.\nWhile exposure to visible light causes heating, it does not cause ionization with its risks. You may be heated by the sun through a car windshield, but you will not be sunburned - that is an effect of the higher frequency uv part of sunlight which is blocked by the glass of the windshield.\nUltraviolet The near ultraviolet is absorbed very strongly in the surface layer of the skin by electron transitions. As you go to higher energies, the ionization energies for many molecules are reached and the more dangerous photoionization processes take place.\nSunburn is primarily an effect of uv, and ionization produces the risk of skin cancer. The ozone layer in the upper atmosphere is important for human health because it absorbs most of the harmful ultraviolet radiation from the sun before it reaches the surface. The higher frequencies in the ultraviolet are ionizing radiation and can produce harmful physiological effects ranging from sunburn to skin cancer.\nX-ray Since the quantum energies of x-ray photons are much too high to be absorbed in electron transitions between states for most atoms, they can interact with an electron only by knocking it completely out of the atom. That is, all x-rays are classified as ionizing radiation. This can occur by giving all of the energy to an electron (photoionization) or by giving part of the energy to the electron and the remainder to a lower energy photon (Compton Scattering). At sufficiently high energies, the x-ray photon can create an electron positron pair.\nAuthor Disclaimer Author: Vinay Kumar (@imflash217) Date: 28/January/2021\nThe contents of this article were originally published at the references below. I have assembled it for my own understanding. Feel free to reuse and tag along the references. 👍\nReferences: ","permalink":"https://imflash217.github.io/portfolio/blogs/physics/blog_01282021/","summary":"How EM radiation interacts with matter across the spectrum  -  from radio waves to X-rays and ionizing effects.","title":"Electromagnetic Spectrum, properties of material medium \u0026 its effects"},{"content":"Awesome List: PositionalEncoding: https://e2eml.school/transformers.html https://github.com/tensorflow/tensor2tensor/issues/1591 https://kazemnejad.com/blog/transformer_architecture_positional_encoding/ BatchNorm: https://colab.research.google.com/drive/1Sw2GXJmylz9DvtPaBfJIeutoXNISelZy?usp=sharing https://e2eml.school/batch_normalization.html How BatchNorm Helps?: https://arxiv.org/pdf/1805.11604.pdf Understanding BatchNorm: https://proceedings.neurips.cc/paper/2018/file/36072923bfc3cf47745d704feb489480-Paper.pdf Batch-ReNorm: https://arxiv.org/pdf/1702.03275.pdf StreamingNorm: https://arxiv.org/pdf/1610.06160.pdf Online Norm: https://proceedings.neurips.cc/paper/2019/file/cb3ce9b06932da6faaa7fc70d5b5d2f4-Paper.pdf How optimization works: https://e2eml.school/how_optimization_works_1.html Einops: https://github.com/arogozhnikov/einops FlashAttention: https://arxiv.org/abs/2205.14135 Mamba: https://arxiv.org/abs/2312.00752 ","permalink":"https://imflash217.github.io/portfolio/awesome/","summary":"Curated list of resources on positional encoding, batch norm, transformers, and deep learning papers.","title":"Awesome List"},{"content":"Lecture #1 Word Meaning Denotational Semantics Distributional Semantics Localist Representation (like one-hot vectors) Distributed Representation (like word-vectors) NLTK example: synonyms of word good import nltk from nltk.corpus import wordnet as wn for synset in wn.synsets(\u0026#34;good\u0026#34;): for lemma in synset.lemmas(): print(synset.pos(), lemma.name()) Problems with toolkits like WordNet Great as a resource but missing nuance Example: proficient is listed as a synonym for good; but it is correct only in some contexts. Missing new meaning of words Example: new slang words etc like wicked, badass, nifty etc. IMPOSSIBLE to keep up-to-date Very Subjective Requires human labour to curate and maintain Can\u0026rsquo;t compute accurate word-similarity. References ","permalink":"https://imflash217.github.io/portfolio/nlp/cs224n/cs224n_1/","summary":"Stanford CS224N Lecture 1  -  word meaning, distributional semantics, and word vectors.","title":"CS224N - Lecture 1"},{"content":"STRATEGY Pattern from abc import ABC, abstractmethod from collections import namedtuple Customer = namedtuple(\u0026#34;Customer\u0026#34;, \u0026#34;name fidelity\u0026#34;) class LineItem: def __init__(self, product, quantity, price): self.product = product self.quantity = quantity self.price = price def total(self): return self.price * self.quantity class Order: \u0026#34;\u0026#34;\u0026#34;This is the CONTEXT part of the Strategy-Pattern\u0026#34;\u0026#34;\u0026#34; def __init__(self, customer, cart, promotion=None): self.customer = customer self.cart = list(cart) self.promotion = promotion def total(self): if not hasattr(self, \u0026#34;__total\u0026#34;): self.__total = sum(item.total() for item in self.cart) return self.__total def due(self): if self.promotion is None: discount = 0 else: discount = self.promotion.discount(self) return self.total() - discount def __repr__(self): return f\u0026#34;\u0026lt;Order total: {self.total():.2f} due: {self.due():.2f}\u0026gt;\u0026#34; Abstract Strategy class Promotion(ABC): \u0026#34;\u0026#34;\u0026#34;Abstract Base Class for implementing promotions\u0026#34;\u0026#34;\u0026#34; @abstractmethod def discount(self, order): \u0026#34;\u0026#34;\u0026#34;return discount as +ve amount\u0026#34;\u0026#34;\u0026#34; pass class FidelityPromotion(Promotion): \u0026#34;\u0026#34;\u0026#34;5% off for customers with 1000+ fidelity points\u0026#34;\u0026#34;\u0026#34; def discount(self, order): return order.total() * 0.05 if order.customer.fidelity \u0026gt;= 1000 else 0 class BulkItemPromotion(Promotion): \u0026#34;\u0026#34;\u0026#34;10% discount for each item with 20+ units\u0026#34;\u0026#34;\u0026#34; def discount(self, order): discount = 0 for item in order.cart: if item.quantity \u0026gt;= 20: discount += item.total() * 0.1 return discount class LargeOrderPromotion(Promotion): \u0026#34;\u0026#34;\u0026#34;7% discount on orders with 10+ distinct items\u0026#34;\u0026#34;\u0026#34; def discount(self, order): distinct_items = {item.product for item in order.cart} if len(distinct_items) \u0026gt;= 10: return order.total() * 0.07 return 0 ","permalink":"https://imflash217.github.io/portfolio/python/design_patterns/","summary":"Strategy pattern in Python using ABC and namedtuples for flexible discount policies.","title":"Design Patterns"},{"content":"DETR: End-to-End Object Detection with Transformers The paper DETR (DEtection TRansformer) is a new method that views object detection as a direct set prediction problem. The approach streamlines the detection pipeline, effectively removing the need for many hand-designed components like non-maximum suppression or anchor generation.\nThe key idea is to use a transformer encoder-decoder architecture combined with a bipartite matching loss that forces unique predictions.\nReview Presentation Slides Coming soon\n","permalink":"https://imflash217.github.io/portfolio/transformers/detr/","summary":"DETR  -  end-to-end object detection using transformers with bipartite matching loss.","title":"DETR"},{"content":"Part-1 Welcome to einops We don\u0026rsquo;t write y = x.transpose(0,2,3,1) We write comprehensible code y = einops.rearrange(x, \u0026#34;b c h w -\u0026gt; b h w c\u0026#34;) einops supports widely used tensor packages viz. numpy, pytorch, tensorflow, chainer, gluon and extends them. What\u0026rsquo;s in this tutorial? Fundamentals: reordering, composition, and decomposition of tensors. Operations: rearrange, reduce, repeat How much can you do with a single operation? Preparations import numpy from utils import display_np_arrays_as_images display_np_arrays_as_images() Load a batch of images ## there are 6 images of shape 96x96 ## with 3 color channels packed as tensors images = np.load(\u0026#34;./resources/test_images.npy\u0026#34;, allow_pickle=False) print(images.shape) ## (6, 96, 96, 3) Composition of axes from einops import rearrange ## composing the first two axes together ## i.e. 6 images of shape (96, 96, 3) are rearranged ## into a single image of shape (6*96, 96, 3) rearrange(images, \u0026#34;b h w c -\u0026gt; (b h) w c\u0026#34;).shape ## (576, 96, 3) Decomposition of axes ## decomposition is the reverse of composition rearrange(images, \u0026#34;b h w c -\u0026gt; b h w c\u0026#34;).shape ## (6, 96, 96, 3) Order of axes matters ## moving color channel to the 2nd position rearrange(images, \u0026#34;b h w c -\u0026gt; b c h w\u0026#34;).shape ## (6, 3, 96, 96) ","permalink":"https://imflash217.github.io/portfolio/blogs/deep_learning/einops/","summary":"Introduction to einops  -  readable tensor operations with rearrange, reduce, and repeat replacing cryptic transposes.","title":"Einops - Part 1"},{"content":"Deep Learning Architectures using EINOPS In this section we will be rewriting the building blocks of deep learning in both the traditional PyTorch way as well as using einops library.\nImports import math import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from einops import rearrange, reduce, repeat, asnumpy, parse_shape from einops.layers.torch import Rearrange, Reduce Simple ConvNet Using only PyTorch class ConvNet(nn.Module): def __init__(self): super(ConvNet, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) def forward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2(x), 2)) x = x.view(-1, 320) # \u0026lt;-- RESHAPE x = F.relu(self.fc1(x)) x = self.fc2(x) return F.log_softmax(x, dim=1) Using einops class ConvNetEinops(nn.Module): def __init__(self): super(ConvNetEinops, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) def forward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2(x), 2)) x = rearrange(x, \u0026#34;b c h w -\u0026gt; b (c h w)\u0026#34;) # \u0026lt;-- einops RESHAPE x = F.relu(self.fc1(x)) x = self.fc2(x) return F.log_softmax(x, dim=1) Multi-Head Attention Using only PyTorch class MultiHeadAttention(nn.Module): def __init__(self, n_head, d_model, d_k, d_v): super().__init__() self.n_head = n_head self.w_qs = nn.Linear(d_model, n_head * d_k) self.w_ks = nn.Linear(d_model, n_head * d_k) self.w_vs = nn.Linear(d_model, n_head * d_v) def forward(self, q, k, v, mask=None): B, Lq, Lk, Lv = q.size(0), q.size(1), k.size(1), v.size(1) n_head, d_k, d_v = self.n_head, self.d_k, self.d_v qs = self.w_qs(q).view(B, Lq, n_head, d_k) # \u0026lt;-- RESHAPE ks = self.w_ks(k).view(B, Lk, n_head, d_k) vs = self.w_vs(v).view(B, Lv, n_head, d_v) qs = qs.permute(0, 2, 1, 3) # \u0026lt;-- TRANSPOSE ks = ks.permute(0, 2, 1, 3) vs = vs.permute(0, 2, 1, 3) # ... rest of attention Using einops class MultiHeadAttentionEinops(nn.Module): def __init__(self, n_head, d_model, d_k, d_v): super().__init__() self.n_head = n_head self.w_qs = nn.Linear(d_model, n_head * d_k) self.w_ks = nn.Linear(d_model, n_head * d_k) self.w_vs = nn.Linear(d_model, n_head * d_v) def forward(self, q, k, v, mask=None): qs = rearrange(self.w_qs(q), \u0026#34;b l (head k) -\u0026gt; b head l k\u0026#34;, head=self.n_head) ks = rearrange(self.w_ks(k), \u0026#34;b t (head k) -\u0026gt; b head t k\u0026#34;, head=self.n_head) vs = rearrange(self.w_vs(v), \u0026#34;b t (head v) -\u0026gt; b head t v\u0026#34;, head=self.n_head) # ... rest of attention ","permalink":"https://imflash217.github.io/portfolio/blogs/deep_learning/einops2/","summary":"Building ConvNets and Multi-Head Attention with einops  -  comparing traditional PyTorch vs einops implementations.","title":"Einops - Part 2: Deep Learning Architectures"},{"content":"Correction of Distribution Shifts Covariate Shift Correction Label Shift Correction Concept Shift Correction Concept Shift is much harder to fix in a principled manner. For instance, in a situation where the problem suddenly changes from classifying cats v/s dogs to classifying white v/s black animals; it will be unreasonable to assume that we can do much better by collecting new labels and training from scratch.\nFortunately, in practice such concept-shifts are very rare, but instead it is usually observed that the task keeps on changing gradually. For example:\nTraffic Camera lenses degrade over time due to wear-\u0026amp;-tear and hence the quality of images degrade too. News content changes gradually; i.e. new stories appear while the news still remain almost same. In computational advertising, new products get launched and hence popularity of products changes and the CTR-Predictor needs to change as well. In such above cases, we use the same strategy that we used while training the network to make them adapt to change in data i.e.; we use Transfer Learning by finetuning the pre-trained weights with the new acquired data.\nA taxonomy of Learning Problems Armed with the knowledge of dealing with changes in the distribution of data/labels, we now focus on other aspects of machine learning formulations:\nBatch Learning ","permalink":"https://imflash217.github.io/portfolio/blogs/deep_learning/dl_04_09/","summary":"Notes on covariate shift, label shift, and concept shift  -  how to detect and correct distribution changes.","title":"Environment \u0026 Distribution Shift"},{"content":"Linked List Question #1\nWhat two properties are typically stored in the nodes of a singly linked list?\n🏆 value and next\nQuestion #2\nWhat terms are commonly used to describe the first node \u0026amp; last node of a linked list?\n🏆 HEAD for first-node\n🏆 TAIL for last-node\nQuestion #3\nWhat is the dummy head pattern for a linked-list?\n🏆 The dummy head pattern is where we use a fake node to act as the HEAD of the linked-list. The dummy-head is used to simplify edge cases such as inserting the first node into an empty linked-list.\nQuestion #4\nWhy might the expression current_node.next.val be UNSAFE?\n🏆 If the current node is a TAIL-node then its .next will be None and None object does not have .val attribute.\nQuestion #5\nWhat are the time complexities for the following linked-list operations?\nLookup Insert Delete 🏆\nOperation Time Complexity Lookup $O(n)$ Insert $O(1)$ Delete $O(1)$ ","permalink":"https://imflash217.github.io/portfolio/algorithms/qa/","summary":"Interview-style Q\u0026amp;A covering linked list fundamentals  -  nodes, pointers, head/tail, and common operations.","title":"Linked List Q\u0026A"},{"content":"Loss Functions Cross Entropy Cross Entropy is usually used in multiclass classification tasks.\nPareto Optimization: An area of multiple criteria decision making that is concerned with mathematical optimization problems involving more than one objective functions to be optimized simultaneously.\nCross Entropy using Numpy import numpy as np def cross_entropy(preds, labels): xentropy = 0 for i in range(len(preds)): xentropy -= preds[i] * np.log(labels[i]) # NOTE the `-=` instead of `+=` return xentropy ","permalink":"https://imflash217.github.io/portfolio/blogs/deep_learning/loss/","summary":"Cross entropy loss explained with NumPy implementation for multiclass classification tasks.","title":"Loss Functions"},{"content":"When \u0026amp; When NOT to use Machine Learning: As its adoption in the industry quickly grows, ML has proven to be a powerful tool for a wide range of problems. Despite an incredible amount of excitement and hype generated by people both inside and outside the field, ML is not a magic tool that can solve all problems. Even for problems that ML can solve, ML might not be an optimal solution.\nSo, before starting an ML project, we must ask whether ML is necessary and cost-effective\nSome applications where use of ML have been widely adopted are:\nPredictive Typing Machine Translation Fraud Detection Price Optimization Churn Prediction Automated Support Ticket Classification Brand Monitoring Sentiment Analysis Healthcare applications When to use ML ML is an approach to learn complex patterns from existing data and use these patterns to make predictions on unseen data.\nCondition Explanation The system has patterns to learn ML solutions only work when there are patterns to learn Data is available or can be collected Without data, there are NO patterns to learn Patterns are complex If it can be solved by simple heuristics, you don\u0026rsquo;t need ML The problem is at scale ML shines when operating at scale Predictions must change (non-static) If the answer never changes, just hardcode it When NOT to use ML When the task is simple enough for hand-crafted rules When cost-effectiveness is not achieved When you need 100% explainability When the error cost is too high and you need deterministic outcomes ","permalink":"https://imflash217.github.io/portfolio/mlsd/01_overview/","summary":"When to use ML and when not to  -  cost-effectiveness and practical adoption guidelines.","title":"ML System Design - Overview"},{"content":"Naïve Bayes Classifiers In this article we will talk about Multinomial Naïve Bayes Classifier, so called because it is a Bayesian Classifier that makes a simplifying (naïve) assumption about the interaction b/w features.\nLet\u0026rsquo;s understand the intuition of this classifier in the context of text classification. Given a text document we first represent the text document as a bag-of-words (i.e. an unordered set of words in the document with their position information removed) keeping only the word-frequency in the given document. In this bag-of-words representation, all we care about is how many times a given word appears in this document.\nNaïve Bayes is a probabilistic classifier, meaning that for a given document d, out of all classes $c\\in C$ the classifier returns the class $\\hat{c}$ which has the maximum posterior probability given the document d.\n","permalink":"https://imflash217.github.io/portfolio/nlp/naive_bayes/","summary":"Multinomial Naive Bayes for text classification  -  intuition, math, and implementation.","title":"Naïve Bayes Classifiers"},{"content":"The Preface of the key technological stuffs here Tips \u0026amp; Tricks LR Scheduler Similar to the learning rate, the lr-scheduler to apply depends on the classifier \u0026amp; the model. For image classifiers and SGD optimizer, the Multi-Step LR Scheduler is shown to be a good choice. Models trained with Adam commonly use a smooth exponential-decay in the lr or a cosine-like scheduler. For TRANSFORMERS: 🚨 Remember to use a learning rate WARMUP 🚨 The cosine-scheduler is often used for decaying the lr afterwards (but can also be replaced by exponential decay) Regularization Regularization is important in networks when we see a significantly higher training performance than test performance. ","permalink":"https://imflash217.github.io/portfolio/blogs/notes/","summary":"Practical tips on LR schedulers, optimizers, and training tricks for deep learning models.","title":"Notes"},{"content":"Probability Theory A key concept in the field of pattern recognition is that of uncertainty. It arises through:\nnoise on measurements finite size of datasets. Probability theory provides a consistent framework for the quantification and manipulation of uncertainty and forms one of the central foundations of Pattern Recognition.\nWhen combined with Decision Theory, it allows us to make optimal predictions given all the information available to us, even though that information may be incomplete or ambiguous.\nExample: Red/Blue boxes The setup Imagine we have two boxes red_box and blue_box. Each box contains different number of fruits oranges and apples.\nr b e l d u _ e b - _ - o b x a o o a o : p r x p r p a : p a l n l n e g e g s e s e : s : s : : 2 3 6 1 The Experiment Now, we do the following steps:\nSelect a box: Randomly pick one of the boxes (either red_box or blue_box) Pick a fruit: Then, randomly select an item of fruit from the box. Replace the fruit: Having observed the type of the picked fruit (apple or orange), now, replace it in the box from which it came. Repeat steps 1-to-3 multiple times. The Pre-conditions / Assumptions Now, let\u0026rsquo;s suppose in doing the above experiment,\nwe pick the red_box 40% of the times and the blue_box 60% of the times. Also, assume that when we remove an item of fruit from a box, we are equally likely to select any of the pieces of fruits in the box. The Theory In this experiment:\nthe identity of the box to be chosen is a random variable $B$ which can take two possible values r and b (corresponding to red or blue boxes). Similarly, the identity of fruit is also a random variable $F$ and it can take either of the values a and o (corresponding to apple and orange respectively). Definition:\nThe Probability of an event is defined as the fraction of times, that event occurs out of the total number of trials (in the limit that the total number of trials goes to infinity). All probabilities must lie in the interval [0, 1]\nThus, the probability of selecting the red_box is $4/10$ and that of the blue_box is $6/10$.\n$$ p(B=r) = 4/10 $$$$ p(B=b) = 6/10 $$","permalink":"https://imflash217.github.io/portfolio/blogs/prob/intro/","summary":"Bayesian probability foundations  -  the red/blue box experiment, random variables, and decision theory basics.","title":"Probability Theory - Introduction"},{"content":"Python Snippets 1: MappingProxyType Immutable Mappings The mapping types provided by the standard library are all mutable; but you may need to guarantee that a user cannot change a mapping by mistake.\nSince Python 3.3 the types module provides a wrapper class MappingProxyType which, given a mapping returns a mappingproxy instance that is read-only but a dynamic-view of the original mapping.\nThis means that the original mapping can be seen through mappingproxy but changes cannot be made through it.\nfrom types import MappingProxyType d = {1:\u0026#34;A\u0026#34;} d_proxy = MappingProxyType(d) ## creating a proxy for the original dict d print(d_proxy) ## mappingproxy({1: \u0026#39;A\u0026#39;}) print(d_proxy[1]) ## \u0026#39;A\u0026#39; ## d_proxy[2] = \u0026#34;B\u0026#34; ## THIS WILL RAISE TypeError d[2] = \u0026#34;B\u0026#34; ## changing the original dict `d` print(d_proxy) ## mappingproxy({1: \u0026#39;A\u0026#39;, 2: \u0026#39;B\u0026#39;}) -- reflected in proxy! 2: Walrus Operator The walrus operator := (assignment expression) was introduced in Python 3.8.\n## Without walrus operator results = [] for x in range(10): y = x ** 2 if y \u0026gt; 20: results.append(y) ## With walrus operator results = [y for x in range(10) if (y := x**2) \u0026gt; 20] 3: __slots__ By default, Python stores instance attributes in a per-instance __dict__. When you have millions of instances, this overhead adds up. Using __slots__ tells Python to store instance attributes in a fixed-size tuple instead.\nclass PointSlots: __slots__ = (\u0026#39;x\u0026#39;, \u0026#39;y\u0026#39;) def __init__(self, x, y): self.x = x self.y = y # This is much more memory efficient than: class PointDict: def __init__(self, x, y): self.x = x self.y = y ","permalink":"https://imflash217.github.io/portfolio/gists/python_snippets/","summary":"MappingProxyType, walrus operator, \u003cstrong\u003eslots\u003c/strong\u003e  -  useful Python snippets and idioms.","title":"Python Snippets"},{"content":"Regular Expressions One of the unsung successes in standardization of computer science has been the Regular Expressions (RE), a language for specifying text search strings. This practical language is in every computer language, word processor, and text processing tools like the Unix\u0026rsquo;s grep.\n🏆 A regular expression is an algebraic notation for characterizing a set of strings.\nThey are particularly useful for searching in texts, when we have a pattern to search for and a corpus of texts to search through. A regex search function will search through the corpus returning all texts that match the pattern. The corpus can be a single document or a collection. For example, the Unix cmd tool grep takes a regex and returns every line of the input document that matches the pattern in regex.\n🚨 RegEx comes in many variants. In this article we will be discussing extended regex.\nBasic Patterns References: ","permalink":"https://imflash217.github.io/portfolio/nlp/regex/","summary":"Regular expression fundamentals  -  pattern syntax for text search and processing.","title":"Regular Expressions (RegEx)"},{"content":"Sorting Algorithms Insertion Sort Vanilla Insertion Sort f } o r i i b n y = s e \" 1 r p , t a i 2 A r , [ w i i 3 ] s , e i n s t w . o a , p s s n o \" { r t d e o d w n a r t r o a y t h A e [ 0 c : o i r - r 1 e ] c t p o s i t i o n This above version has $\\theta(n)$ steps and each step has $\\theta(n)$ comparisons. So this version of the algorithm is $\\theta(n^2)$ runtime complexity.\nBinary Insertion Sort This improved version is slightly improved by using Binary Search while searching for the position to place the key A[i] in the sorted part of the array (i.e. A[0:i-1])\nf } o r i i b n y = s e \" 1 r B , t i n 2 A a , [ r i y 3 ] , S i e n a t r . o c , h s \" n o { r d t o e w d n a t r o r a t y h e A [ c 0 o : r i r - e 1 c ] t p o s i t i o n This above version has $\\theta(n)$ steps and each step has $\\theta(\\log n)$ comparisons. So this version of the algorithm has $\\theta(n \\log n)$ comparisons; but $\\theta(n^2)$ swaps. So, overall complexity is still $\\theta(n^2)$\nMerge Sort m e r g e i e _ f l s s o l e r e : L R r t n e ( ( = = t A A u ) ) m m r : e e n \u0026lt; r r g g m 2 e e e : _ _ r s s g r o o e e r r ( t t t L u ( ( , r A A n [ [ R 0 n ) A : / n 2 / : 2 n ] ] ) ) Time Complexity: $T(n) = c_1 + 2T(n/2) + cn$\nBy Master\u0026rsquo;s Theorem: $T(n) = \\theta(n \\log n)$\nSpace Complexity: $\\theta(n)$ auxiliary space\nHeap Sort The HEAP is an implementation of the abstract data-type called priority queue An array visualized as a nearly complete Binary-Tree Heap properties: parent(i) = i / 2 left_child(i) = 2 * i right_child(i) = 2 * i + 1 Max-Heap Property: For every node i (other than the root): A[parent(i)] \u0026gt;= A[i]\nBuild Max Heap def build_max_heap(A): n = len(A) for i in range(n//2, -1, -1): max_heapify(A, i) def max_heapify(A, i): l = 2*i + 1 r = 2*i + 2 largest = i if l \u0026lt; len(A) and A[l] \u0026gt; A[largest]: largest = l if r \u0026lt; len(A) and A[r] \u0026gt; A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] max_heapify(A, largest) ","permalink":"https://imflash217.github.io/portfolio/algorithms/sorting/","summary":"Insertion sort, binary insertion sort, merge sort, and heap sort with pseudocode and complexity analysis.","title":"Sorting Algorithms"},{"content":"NLP notes: Text Normalization Normalizing text means converting them into a more convenient, standard form. For example, most of what we are going to do with language relies on first separating out or tokenizing words from running text (which is called as tokenization).\nTokenization Lemmatization Stemming Sentence Segmentation Edit Distance ","permalink":"https://imflash217.github.io/portfolio/nlp/nlp_book/","summary":"Notes on text normalization, tokenization, and NLP fundamentals from Jurafsky and Martin.","title":"Speech \u0026 Language Processing"},{"content":"Most of you may know that about 90% of the technology available is idle; in fact even more. Probably more than 95% of computing power is idle.\nIn last class we discussed two important ideas:\nRole of speech technology Signal -- to -- Symbol Scope of Speech Technology for this course ❌ Language Translation ❌ Coding ❌ Transmission (Tx) ❌ Communication Technology in Speech (here it deals with coding \u0026amp; Tx) ✅ Signal to Symbol Transformation ✅ Language Knowledge ✅ Suprasegmental Knowledge (aka prosody) ","permalink":"https://imflash217.github.io/portfolio/speech/lecture-3/","summary":"Speech processing lecture  -  signal-to-symbol transformation, prosody, and language knowledge.","title":"Speech Processing - Lecture 3"},{"content":"Tensorflow Tutorial In this session you will learn to do the following in TensorFlow v1.0\nInitialize Variables Start your own session Train Algorithms Implement a Neural Network Exploring the Tensorflow Library Example-1: General Overview import tensorflow as tf y_hat = tf.constant(36, name=\u0026#34;y_hat\u0026#34;) ## Defines a \u0026#34;y_hat\u0026#34; constant. Sets its value to 36 y = tf.constant(39, name=\u0026#34;y\u0026#34;) ## Defines a \u0026#34;y\u0026#34; constant. Sets its value to 39 loss = tf.Variable((y-y_hat)**2, name=\u0026#34;loss\u0026#34;) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) sess.run(loss) print(loss) Writing and running programs in Tensorflow has the following steps:\nCreate tensors (variables) that are not yet evaluated/executed. Write operations between those tensors. Initialize the tensors. Create a Session. Run the session. This will run the operations written in step-2. Example-2: tf.Session() a = tf.constant(2) b = tf.constant(10) c = tf.multiply(a, b) print(c) T e n s o r ( \" M u l : 0 \" , s h a p e = ( ) , d t y p e = i n t 3 2 ) As expected we will not see 20. We got a tensor saying that the result of the tensor does not have the shape attribute and is of the type int32. All we did was to put in the computation graph; but we haven\u0026rsquo;t run this computation yet!\nsess = tf.Session() print(sess.run(c)) 2 0 Example-3: tf.placeholder() A placeholder is an object whose value we can specify ONLY later.\nsess = tf.Session() x = tf.placeholder(tf.int64, name=\u0026#34;x\u0026#34;) print(sess.run(2*x, feed_dict={x:9})) sess.close() 1 8 Using one-hot encodings: def one_hot_matrix(labels, num_classes): num_classes = tf.constant(num_classes, name=\u0026#34;num_classes\u0026#34;) one_hot_matrix = tf.one_hot(indices=labels, depth=num_classes, axis=0) sess = tf.Session() one_hot = sess.run(one_hot_matrix) sess.close() return one_hot Building a Neural Network Building the model def model(X_train, Y_train, X_test, Y_test, lr=1e-3, num_epochs=1500, bs=32, verbose=True): \u0026#34;\u0026#34;\u0026#34; Implements a 3-layer Tensorflow Neural Network: [Linear]-\u0026gt;[Relu]-\u0026gt;[Linear]-\u0026gt;[Relu]-\u0026gt;[Linear]-\u0026gt;[Softmax] \u0026#34;\u0026#34;\u0026#34; ops.reset_default_graph() tf.set_random_seed(217) (n_x, m) = X_train.shape n_y = Y_train.shape[0] costs = [] X, Y = create_placeholders(n_x, n_y) parameters = initialize_parameters() Z3 = forward_propagation(X, parameters) cost = compute_cost(Z3, Y) optimizer = tf.train.AdamOptimizer(lr).minimize(cost) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for epoch in range(num_epochs): epoch_cost = 0.0 num_batches = m // bs minibatches = random_mini_batches(X_train, Y_train, bs) for (Xb, Yb) in minibatches: _, minibatch_cost = sess.run([optimizer, cost], feed_dict={X:Xb, Y:Yb}) epoch_cost += minibatch_cost epoch_cost /= num_batches parameters = sess.run(parameters) correct_preds = tf.equal(tf.argmax(Z3), tf.argmax(Y)) accuracy = tf.reduce_mean(tf.cast(correct_preds, \u0026#34;float\u0026#34;)) return parameters ","permalink":"https://imflash217.github.io/portfolio/blogs/deep_learning/blog_tf_v1/","summary":"Hands-on TensorFlow v1.0 tutorial  -  variables, sessions, training algorithms, and building a neural network.","title":"Tensorflow Tutorial"},{"content":"CDN अर्थात् Content Delivery Network CDN अर्थात् servers का एक ऐसा network जो पृथ्वी पर अलग-अलग भूगौलिक क्षेत्रों में होता है; और इनका एक ही कार्य होता है: \u0026ldquo;static content जैसे images, videos, CSS, JavaScript, etc. को माँगने वाले client (जैसे apps, end-users, etc) तक पहुँचाना\u0026rdquo;। इस कार्य को करने के लिए CDN के द्वारा प्रयुक्त होने वाले technology का नाम है: Dynamic Content Caching जो request path, query strings, cookies और request headers के आधार पर HTML pages को cache करने की योग्यता प्रदान करता है।\nसरल भाषा में CDN मुख्यतः दो कार्यों को करता है:\nClient को static content देना नज़दीकी client की सर्वप्रथम सेवा करना (अर्थात्, अगर हमारा CDN चेन्नई में है और दो client, एक बैंगलुरु व दूसरा दिल्ली से CDN से एक website माँग रहे हैं तो सर्वप्रथम बैंगलुरु के request को पूरा किया जाएगा फिर दिल्ली को क्योंकि दिल्ली की अपेक्षा बैंगलुरु हमारे CDN के नज़दीक है जो चेन्नई में है)। ","permalink":"https://imflash217.github.io/portfolio/blogs/system_design/cdn/","summary":"Content Delivery Networks explained in Hindi  -  caching, static content delivery, and CDN architecture.","title":"🌩️ CDN (Content Delivery Network)"},{"content":"Deep Learning Interviews Introduction Q1: Distribution of maximum entropy What is the distribution of maximum entropy; i.e. the distribution that has the maximum entropy among all distributions in a bounded interval [a, b], (-\\inf, +\\inf)?\nSolution: In a bounded interval [a, b], the UNIFORM DISTRIBUTION has the maximum entropy. The variance of the Uniform Distribution $\\mathcal{U}(a, b)$ is $\\sigma^2 = \\frac{(b-a)^2}{12}$. Therefore, the maximum entropy in a bounded interval [a, b] is $\\left(\\frac{\\log{12}}{2} + \\log(\\sigma)\\right)$\nQ2: What\u0026rsquo;s the purpose of this code-snippet? Describe in your own words, what is the purpose of this code snippet?\nself.transforms = [] if rotate: self.transforms.append(RandomRotate()) if flip: self.transforms.append(RandomFlip()) Solution: Overfitting is a common problem that occurs during training of machine learning systems. Among various strategies to overcome the problem of overfitting; data-augmentation is a very handy method. Data Augmentation is a regularization technique that synthetically expands the data-set by utilizing label-preserving transformations to add more invariant examples of the same data samples.\nUsually, the data-augmentation process is done in the CPU before uploading the batched-data for training the model on the GPU.\nLogistic Regression Q3: Drawbacks of model fitting For a fixed number of observations in a dataset, introducing more number of variables normally generate a model that has a better fit to the data. What may be drawbacks of such a model fitting strategy?\nSolution: Introducing more number of variables increases the capacity of the model. If the number of data points in the dataset is kept fixed, and then increasing the number of model parameters (variables) leads to OVERFITTING. Overfitting is a scenario where the trained model performs very well on the training data but performs poorly on the test dataset due to lack of generalization capabilities as the overly sized model just remembered the data points instead of understanding the features \u0026amp; data distribution in the training set.\nQ4: Odds of Success Define the term odds of success, both qualitatively and formally.\nSolution: Odds of Success of an event in an experiment is the ratio of probability of the event occurring and the probability of the event not occurring i.e. $\\left(\\frac{\\text{probability of occurrence of an event E}}{1 - \\text{(probability of the occurrence of the event E)}}\\right)$\n","permalink":"https://imflash217.github.io/portfolio/blogs/deep_learning/dl_interviews/","summary":"DL interview prep  -  maximum entropy, data augmentation, overfitting, and probability of success problems.","title":"🧑🏻‍💻 DL Interviews"}]