<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matt Tengtrakool on Medium]]></title>
        <description><![CDATA[Stories by Matt Tengtrakool on Medium]]></description>
        <link>https://medium.com/@matt.tengtrakool?source=rss-720b7ac13f06------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xwfXe9sgggoag_vtmpVRxw.jpeg</url>
            <title>Stories by Matt Tengtrakool on Medium</title>
            <link>https://medium.com/@matt.tengtrakool?source=rss-720b7ac13f06------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 06:36:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matt.tengtrakool/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to build tool-using agents with LangChain]]></title>
            <link>https://medium.com/artificial-corner/how-to-build-tool-using-agents-with-langchain-d9fa9674b6c7?source=rss-720b7ac13f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9fa9674b6c7</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Matt Tengtrakool]]></dc:creator>
            <pubDate>Sun, 04 Jun 2023 10:04:40 GMT</pubDate>
            <atom:updated>2023-06-04T10:04:40.644Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn to use LangChain and Pinecone to build LLM agents that can access the internet and knowledge bases</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bjAZNzVGuBm1xiI-" /><figcaption>Photo by <a href="https://unsplash.com/@deepmind?utm_source=medium&amp;utm_medium=referral">Google DeepMind</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>AI agents are revolutionizing how we approach problem-solving. Building LLMs that can understand, analyze, and leverage diverse data sources, we are beginning to scrape the surface of integrating agents into our routines. These tool wielding agents are essential to make deeper interations between us and our models.</p><p>Consider an LLM that can converse thoughtfully, utilizing its tools to offer insightful information. As an example, we can build extrapolating functions to access outside data including realtime searches and knowledge base queries.</p><blockquote><em>Thought: This is an intriguing query. I might need to gather more information.</em></blockquote><blockquote><em>Action: Search Action</em></blockquote><blockquote><em>Input: “What is the history of artificial intelligence?”</em></blockquote><blockquote><em>Observation: The concept of artificial intelligence has its roots in the ancient world, but it wasn’t until the mid-20th century that AI, as we know it today, began to take shape. Scientists like Alan Turing and John McCarthy are credited with pioneering the field. It’s clear that the development of AI has been a gradual process.</em></blockquote><blockquote><em>Action: Knowledge Base</em></blockquote><blockquote><em>Input: “What are the key milestones in the development of artificial intelligence?”</em></blockquote><blockquote><em>Observation: Some of the key milestones in AI’s history include Alan Turing’s development of the Turing Machine, the Dartmouth Conference where John McCarthy coined the term ‘Artificial Intelligence’, the invention of the first neural network machine ‘Perceptron’ by Frank Rosenblatt, the introduction of Machine Learning algorithms, and more recently, the development of advanced AI models like OpenAI’s GPT-3. Final Answer: The history of artificial intelligence is vast and fascinating, evolving from theoretical beginnings to its present-day advanced capabilities. Notable milestones include Alan Turing’s Turing Machine, the coining of the term ‘Artificial Intelligence’ at the Dartmouth Conference, and the development of advanced AI models such as OpenAI’s GPT-3.</em></blockquote><p>This ability to access a variety of information sources allows us to interact with our models in novel ways. We can extract more comprehensive, nuanced answers and build tailored agents for consumer use cases. In this tutorial, we’ll walk you through the process of building an LLM Agent using LangChain and Pinecone.</p><h3>Step 1: Setting Up Your Environment</h3><p>To start off, we’ll need to install the required Python libraries. If you haven’t already, install LangChain and Pinecone using pip:</p><pre>pip install langchain pinecone-client</pre><p>Now we have access to the libraries we need to construct our LLM agent.</p><h3>Step 2: The LLM Base</h3><p>The LLM base is the engine of our agent — it’s where the main thinking happens. For this, we’ll use OpenAI’s GPT-4.</p><pre>from langchain.models import OpenAI<br><br>llm = OpenAI(temperature=0.7)</pre><p>The temperature parameter influences the randomness of the model&#39;s output. A lower value, such as 0.2, results in more focused and deterministic outputs, while a higher value, such as 0.8, produces more diverse and unpredictable outputs. You can tune this according to your needs.</p><h3>Step 3: Creating a Custom Prompt Template</h3><p>Now that we have our LLM base ready, it’s time to customize our prompt template. This template allows us to frame the conversation in the way we want. For instance, we might want our agent to first think, then act, and finally observe the outcome.</p><pre>from langchain.templates import CustomPromptTemplate<br><br>template_with_history = &quot;&quot;&quot;<br>Thought: {{thought}}<br>Action: {{action}}<br>Action Input: {{action_input}}<br>{% for name, result in action_outputs.items() %}<br>Observation: {{result.observation}}<br>{% endfor %}<br>Final Answer: {{answer}}<br>&quot;&quot;&quot;<br><br>prompt_with_history = CustomPromptTemplate(<br>    template=template_with_history,<br>    tools=[],<br>    input_variables=[&quot;input&quot;, &quot;intermediate_steps&quot;, &quot;history&quot;]<br>)</pre><p>With this prompt template, our LLM will provide a thoughtful observation, decide on an action, generate an action input, make an observation, and finally, provide an answer.</p><h3>Step 4: Creating Tools and Setting Up Knowledge Base with Pinecone</h3><p>Tools give our agent the ability to access diverse information sources. We’ll create two tools: a search tool and a knowledge base retrieval tool using Pinecone.</p><p>Let’s start by setting up the search tool. We’ll use the Bing Search API for this purpose:</p><pre>from langchain.tools import Bing<br><br># Create an instance of the Bing search tool<br>search_tool = Bing(api_key=&quot;&lt;your-bing-api-key&gt;&quot;)</pre><p>The knowledge base tool is slightly more involved. We’ll use Pinecone for this purpose. Pinecone is a vector database service that enables us to perform efficient similarity search on high-dimensional data.</p><p>Now to create a new Pinecone service, you need to set up your Pinecone account and retrieve the API key.</p><pre>import pinecone<br><br># Initialize Pinecone<br>pinecone.init(api_key=&quot;&lt;your-pinecone-api-key&gt;&quot;)<br><br># Create a new Pinecone service<br>index_name = &quot;knowledge-base&quot;<br>if index_name not in pinecone.list_indexes():<br>    pinecone.create_index(name=index_name, dimension=300, metric=&quot;cosine&quot;)</pre><p>Now, let’s create a Pinecone retriever tool to interact with this service. We’ll use a chain called RetrievalQA from langchain:</p><pre>from langchain.chains import RetrievalQA<br>from langchain import OpenAI<br><br># Initialize an OpenAI agent<br>llm = OpenAI(temperature=0)<br><br># Create a Pinecone retriever tool<br>knowledge_base_tool = RetrievalQA.from_chain_type(llm=llm, chain_type=&quot;stuff&quot;, retriever=index_name)</pre><p>Now that we have our tools ready, we can add them to the toolset:</p><pre>expanded_tools = [<br>    Tool(<br>        name=&quot;Search&quot;,<br>        func=search_tool.run,<br>        description=&quot;useful for when you need to answer questions about current events&quot;<br>    ),<br>    Tool(<br>        name=&#39;Knowledge Base&#39;,<br>        func=knowledge_base_tool.run,<br>        description=&quot;Useful for general questions about how to do things and for details on interesting topics. Input should be a fully formed question.&quot;<br>    )<br>]</pre><h3>Step 5: Constructing the LLM Chain</h3><p>With the LLM base, prompt template, and tools ready, it’s time to create our LLM chain. The chain is essentially the flow of thought and action that our agent will follow.</p><p>Here’s how to set it up:</p><pre>from langchain import LLMChain<br><br># Create the LLM Chain<br>llm_chain = LLMChain(llm=llm, prompt_template=prompt_template, tools=expanded_tools)The LLMChain constructor takes three arguments: the LLM base, the prompt template, and a list of tools.</pre><h3>Step 6: Defining the Output Parser</h3><p>Our agent now has a solid base and a set of powerful tools. However, to understand the output of our agent effectively, we need to parse it properly. We’ll do this by defining an output parser:</p><pre>from langchain.agents import LLMSingleActionAgent<br>from langchain.executors import AgentExecutor<br>from langchain.memory import ConversationBufferWindowMemory<br><br># List of tool names<br>tool_names = [tool.name for tool in expanded_tools]<br><br># Create the custom agent<br>custom_agent = LLMSingleActionAgent(<br>    llm_chain=llm_chain, <br>    output_parser=output_parser,<br>    stop=[&quot;\nObservation:&quot;], <br>    allowed_tools=tool_names<br>)<br><br># Define the memory for the agent<br>agent_memory = ConversationBufferWindowMemory(k=2)<br><br># Build the Agent Executor<br>agent_executor = AgentExecutor.from_agent_and_tools(<br>    agent=custom_agent, <br>    tools=expanded_tools, <br>    verbose=True, <br>    memory=agent_memory<br>)</pre><p>The OutputParser takes the raw output from the LLM base and formats it according to the prompt template. This makes it easy to read and understand the final output.</p><h3>Step 7: Building the Agent Executor</h3><p>Finally, it’s time to bring it all together and build the agent executor. The executor is the central component that integrates the LLM base, the tools, and the output parser to facilitate meaningful conversations.</p><p>Here’s how to initialize it:</p><pre>from langchain.agents import LLMSingleActionAgent<br>from langchain.executors import AgentExecutor<br>from langchain.memory import ConversationBufferWindowMemory<br><br># List of tool names<br>tool_names = [tool.name for tool in expanded_tools]<br><br># Create the custom agent<br>custom_agent = LLMSingleActionAgent(<br>    llm_chain=llm_chain, <br>    output_parser=output_parser,<br>    stop=[&quot;\nObservation:&quot;], <br>    allowed_tools=tool_names<br>)<br><br># Define the memory for the agent<br>agent_memory = ConversationBufferWindowMemory(k=2)<br><br># Build the Agent Executor<br>agent_executor = AgentExecutor.from_agent_and_tools(<br>    agent=custom_agent, <br>    tools=expanded_tools, <br>    verbose=True, <br>    memory=agent_memory<br>)</pre><p>Congratulations, your LLM agent is now ready to converse intelligently!</p><h3>Conclusion</h3><p>In this tutorial, we’ve shown how you can use LangChain to build a sophisticated LLM agent capable of utilizing various tools to provide nuanced and accurate responses. From setting up your environment to creating the agent executor, we’ve walked you through each step to create an agent that can navigate complex conversational landscapes.</p><p>Let’s take an example of how our agent would operate in a complex discussion about the ethical implications of artificial intelligence:</p><blockquote><em>Thought: This is a complex topic requiring a balanced understanding. Action: Knowledge Base</em></blockquote><blockquote><em>Input: “What are the arguments for and against AI ethics?”</em></blockquote><blockquote><em>Observation: The arguments for AI ethics include the necessity to prevent bias in AI systems, protect privacy, and ensure transparency in AI decision-making. The arguments against center around the stifling of innovation, the potential for over-regulation, and the difficulty in defining universal ethical standards.</em></blockquote><blockquote><em>Action: Search</em></blockquote><blockquote><em>Input: “What are some notable incidents where AI ethics were questioned?”</em></blockquote><blockquote><em>Observation: A few instances include the Cambridge Analytica scandal where personal data was used for targeted political advertising, or the controversy around facial recognition technology being potentially biased and infringing on personal privacy. These incidents underscore the importance of ethics in AI. Final Answer: AI ethics is a complex field with arguments on both sides. It’s crucial to consider these perspectives and recent incidents before forming an opinion.</em></blockquote><p>Building an AI agent is an iterative process. There is a lot of room to experiment with different tools, adjust the prompt template, and fine-tune the LLM base to suit your specific needs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9fa9674b6c7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/artificial-corner/how-to-build-tool-using-agents-with-langchain-d9fa9674b6c7">How to build tool-using agents with LangChain</a> was originally published in <a href="https://medium.com/artificial-corner">Artificial Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Knowledge Bound” your LLM without fine-tuning]]></title>
            <link>https://levelup.gitconnected.com/knowledge-bound-your-llm-without-fine-tuning-731355e88994?source=rss-720b7ac13f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/731355e88994</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Matt Tengtrakool]]></dc:creator>
            <pubDate>Tue, 30 May 2023 12:26:57 GMT</pubDate>
            <atom:updated>2023-05-30T12:26:57.981Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to embed and index a knowledge base to personalize any Large Language Model</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsp22rKwFDjiwwCcUly56Q.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@mvdheuvel?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Maarten van den Heuvel</a> on <a href="https://unsplash.com/s/photos/documents?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>When working with large language models (LLMs), such as GPT-4, a common challenge is ensuring the output is based on a specific knowledge set, rather than its vast training data. This is known as &#39;knowledge bounding&#39;. A practical way to implement this is by indexing a set of documents that represents the desired knowledge base. In this guide, we&#39;ll explain how to accomplish this in a simple, efficient, and scalable manner, even if you don&#39;t have an extensive background in machine learning or indexing.</p><h3>What is Indexing and Why is it Useful for Knowledge Bounding?</h3><p>Indexing is a technique that allows for efficient retrieval of information from a large set of data. Imagine trying to find a specific word in a book. Without an index, you’d have to go through each page one by one until you find it. With an index, you can directly find the pages where the word appears. This process is much quicker and easier.</p><p>Applying this to LLMs, if we want the model to provide answers based on a specific set of documents, we can create an index of these documents. This allows the model to efficiently search through these documents for relevant information when asked a query.</p><p>This technique can be highly beneficial in many scenarios. For example, when creating a course, an instructor may want to ensure that the AI tutor’s responses are based on the course material only. Similarly, businesses could use this approach to train a customer service bot using their own data, ensuring the bot’s responses align with their policies.</p><h3>Step-by-Step Guide to Knowledge Bounding with Indexing</h3><p>Before we start, make sure you have the following:</p><ul><li>Basic understanding of Python programming.</li><li>Access to a Large Language Model (This guide uses a GPT-4 based model, but the principles can be applied to other models).</li><li>Python libraries such as FAISS (Facebook AI Similarity Search), Numpy, and Pandas installed. If not, they can be easily installed via pip.</li></ul><h3>Step 1: Collect and Prepare Your Documents</h3><p>Start by collecting the documents you want to use for bounding the knowledge of the model. This could be a set of course materials, company policy documents, or any text-based information relevant to your needs.</p><p>We can for example use PyPDF2 to extract text from pdf documents directly.</p><pre>import PyPDF2<br><br>def extract_text_from_pdf(file_path):<br>    pdf_file_obj = open(file_path, &#39;rb&#39;)<br>    pdf_reader = PyPDF2.PdfFileReader(pdf_file_obj)<br>    text = &#39;&#39;<br>    for page_num in range(pdf_reader.numPages):<br>        page_obj = pdf_reader.getPage(page_num)<br>        text += page_obj.extractText()<br>    pdf_file_obj.close()<br>    return text</pre><p>Once you have your documents, clean them up to remove any irrelevant information. If some documents are too long, consider breaking them into smaller pieces, known as ‘chunks’. The size of these chunks can vary, but it’s often a good idea to keep them small enough to be manageable.</p><h3>Step 2: Building the Index</h3><p>The next step is creating an index from your documents. This involves transforming each document or chunk into a numerical representation, often referred to as a ‘vector’, using the language model. The process of converting text into vectors is sometimes referred to as ‘embedding’. The collection of these vectors forms the ‘index’.</p><p>Below is a simple Python function that goes through each document, breaks it into chunks, embeds each chunk into a vector using the LLM, and adds that vector to an index:</p><pre>import os<br>import numpy as np<br>import faiss<br><br>def build_index(document_folder, model, chunk_size=512):<br>    # Create a new index<br>    index = faiss.IndexFlatL2(model.get_dim())<br>    <br>    # Iterate over each document in the specified folder<br>    for filename in os.listdir(document_folder):<br>        with open(os.path.join(document_folder, filename), &#39;r&#39;) as file:<br>            if filename.endswith(&#39;.pdf&#39;):<br>              text = extract_text_from_pdf(os.path.join(document_folder, filename))<br>            else:<br>              text = file.read()<br>            # Break the document into chunks<br>            chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]<br>            # Convert each chunk into a vector<br>            for chunk in chunks:<br>                            # Get vector representation for each chunk using the language model<br>                            vector = model.encode(chunk)<br>                            # Normalize the vector. This step is crucial for some types of indexes in FAISS.<br>                            faiss.normalize_L2(vector)<br>                            # Add the vector to the index<br>                            index.add(vector)<br>    return index</pre><p>After running this function, you’ll have an index that represents your set of documents. Each vector in the index corresponds to a chunk of text from your documents, and its position in the high-dimensional space reflects the semantic and syntactic properties of that chunk.</p><h3>Step 3: Querying the Index</h3><p>Now, with the index built, you can use it to provide bounded knowledge responses to queries. When the LLM receives a query, it converts the query into a vector using the same process as during indexing. The index then finds the closest vectors, i.e., the most relevant chunks.</p><p>Here’s a Python function that queries the index:</p><pre>def query_index(index, model, query, top_k=5):<br>    # Convert the query into a vector<br>    vector = model.encode(query)<br>    # Normalize the vector<br>    faiss.normalize_L2(vector)<br>    # Search the index for the top_k closest vectors<br>    distances, indices = index.search(vector, top_k)<br>    return distances, indices</pre><p>This function returns the indices and distances of the top_k closest vectors to the query. The indices can be used to retrieve the corresponding chunks from your documents. The distances give a measure of how closely related the chunks are to the query.</p><h3>Optimizing the Index</h3><p>While the basic mechanism of indexing is relatively straightforward, there are several ways to fine-tune it for better performance:</p><ol><li>Choosing the right parameters: The size of the input sequence, the number of vectors (chunks) per document, and the type of index in FAISS can greatly affect performance and accuracy. Experiment with these parameters to find the best configuration for your specific use case.</li><li>Document representation: Instead of breaking documents into chunks, you might choose to represent each document as a single vector. This can be done by taking the mean of all chunk vectors in a document. It’s a trade-off between granularity and computational efficiency.</li><li>Index size and search speed: If your document set is extremely large, consider using a different type of index in FAISS that supports compression, like the IndexIVFFlat. It allows for faster searches but with a slight decrease in accuracy.</li></ol><p>Remember, the goal is to create an index that efficiently and accurately represents your set of documents, providing the LLM with a bounded knowledge source to answer queries. Experimentation and adaptation will be key to achieving optimal results.</p><h3>Summary</h3><p>Knowledge bounding through indexing serves as a powerful tool in the hands of anyone who wishes to leverage the power of LLMs while ensuring the responses are tailored to a specific set of information.</p><p>In this guide, we’ve introduced an simple exmple into “knowledge bounding” your Large Language Model without requiring fine-tuning: embedding and indexing. These techniques allow us to guide the model’s output, ensuring that it reflects a specific set of knowledge corresponding to our own selected documents.</p><p>Implementing indexing for knowledge bounding allows your LLM to efficiently sift through a vast amount of information, referencing only the documents you’ve selected. This becomes incredibly powerful when you want the model to be familiar with specific content, such as educational material, a collection of technical manuals, a body of literature, or any text data that represents your desired knowledge base.</p><p>Despite the process requiring careful consideration in selecting parameters and potential trade-offs between speed, accuracy, and resources, the investment is worthwhile. By using indexing, you’re enhancing your model’s capacity to deliver more accurate and context-specific responses based on the knowledge boundaries you set.</p><p>Always remember to validate the performance of your model and explore other indexing tools if FAISS doesn’t meet your specific needs. Through understanding and implementing this approach, you’re not only personalizing your Large Language Model but also maximizing its potential to provide value in your use case.</p><h3>Level Up Coding</h3><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://levelup.gitconnected.com/?utm_source=pub&amp;utm_medium=post">Level Up Coding publication</a></li><li>💰 Free coding interview course ⇒ <a href="https://skilled.dev/?utm_source=luc&amp;utm_medium=article">View Course</a></li><li>🔔 Follow us: <a href="https://twitter.com/gitconnected">Twitter</a> | <a href="https://www.linkedin.com/company/gitconnected">LinkedIn</a> | <a href="https://newsletter.levelup.dev/">Newsletter</a></li></ul><p>🚀👉 <a href="https://jobs.levelup.dev/talent/welcome?referral=true"><strong>Join the Level Up talent collective and find an amazing job</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=731355e88994" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/knowledge-bound-your-llm-without-fine-tuning-731355e88994">“Knowledge Bound” your LLM without fine-tuning</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding What We Lose]]></title>
            <link>https://medium.com/data-science/understanding-what-we-lose-b91e114e281b?source=rss-720b7ac13f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/b91e114e281b</guid>
            <category><![CDATA[editors-pick]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Matt Tengtrakool]]></dc:creator>
            <pubDate>Tue, 23 May 2023 18:21:44 GMT</pubDate>
            <atom:updated>2023-06-09T04:09:46.516Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>How We Tackle Catastrophic Forgetting in LLMs</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*F5_d6D3D4CLPyvq7zWvDpA.png" /><figcaption><em>Figure 1: The shared experience of forgetting. </em>Image generated by DALL·E, developed by OpenAI.</figcaption></figure><p>Forgetting is an intrinsic part of the human experience. We all misplace our keys, stumble on a familiar name, or draw a blank on what we had for dinner a couple of nights ago. But this apparent lapse in our memory isn’t necessarily a failing. Rather, it highlights a sophisticated cognitive mechanism that enables our brains to prioritize, sift through, and manage a deluge of information. Forgetting, paradoxically, is a testament to our ability to learn and remember.</p><p>Just as people forget, so do machine learning models — in particular, Large Language Models. These models learn by adjusting internal parameters in response to data exposure. However, if new data contrasts with what the model has previously learned, it might overwrite or dampen the old information. Even corroborating data can finagle and turn the wrong knobs on otherwise good learning weights. This phenomenon, known as “catastrophic forgetting,” is a significant challenge in training stable and versatile artificial intelligence systems.</p><h4><strong>The Mechanics of Forgetting in LLMs</strong></h4><p>At the core, an LLM’s memory lies in its weights. In a neural network, each weight essentially constitutes a dimension in the network’s high-dimensional weight space. As the learning process unfolds, the network navigates this space, guided by a select gradient descent, in a quest to minimize the loss function.</p><p>This loss function, usually a form of cross-entropy loss for classification tasks in LLMs, compares the model’s output distribution to the target distribution. Mathematically, for a target distribution y and model output ŷ, the cross-entropy loss can be expressed as:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fmath.embed.fun%2Fembed%2F56QBE1VZtZkqfY5tnF3Fxo&amp;display_name=Embed+Fun&amp;url=https%3A%2F%2Fmath.embed.fun%2F56QBE1VZtZkqfY5tnF3Fxo&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=embed" width="200" height="96" frameborder="0" scrolling="no"><a href="https://medium.com/media/d176601801a4c7f59bdff9477ffd9b7a/href">https://medium.com/media/d176601801a4c7f59bdff9477ffd9b7a/href</a></iframe><p>During training, the network tweaks its weights to minimize this loss. Now, the central aspect governing how much a weight should change is the learning rate. In the stochastic gradient descent update rule:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fmath.embed.fun%2Fembed%2FcgtR3FUDqBdZ428vVE3Ckh&amp;display_name=Embed+Fun&amp;url=https%3A%2F%2Fmath.embed.fun%2FcgtR3FUDqBdZ428vVE3Ckh&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=embed" width="200" height="96" frameborder="0" scrolling="no"><a href="https://medium.com/media/154f104c5a952f1827c3e84d41ce55e6/href">https://medium.com/media/154f104c5a952f1827c3e84d41ce55e6/href</a></iframe><p>η is the learning rate. However, the choice of this learning rate can be tricky and holds implications for catastrophic forgetting. If η is high, the model is highly plastic and can rapidly learn new tasks but risks losing prior knowledge. A small η preserves old knowledge but might compromise the learning of new tasks.</p><p>Moreover, the complexity rises when we realize that weight updates are not independent. Adjusting a weight associated with one feature may inadvertently affect the performance of other features, leading to a complex, tangled web of dependencies.</p><p>We must also consider the curricular order of tasks or data during training. Sequentially introducing tasks could lead to dominance of later tasks, making the model biased towards the latest learned task, a direct manifestation of catastrophic forgetting.</p><h4><strong>Strategies to Counter Catastrophic Forgetting</strong></h4><p>We want our LLMs to remember exponentially beyond what we can ourselves. Thus, we are striving to build systems that are efficient with their memory yet not confined necessarily to our biological standards. In the quest to combat catastrophic forgetting in LLMs, researchers have developed several innovative strategies. Three of the most prominent strategies include Elastic Weight Consolidation, Progressive Neural Networks , and Optimized Fixed Expansion Layers. Each technique incorporates a unique mathematical approach to mitigate the forgetting problem.</p><h4><strong>Elastic Weight Consolidation (EWC): Remembering the Importance of Each Weight</strong></h4><p>EWC is inspired by neuroscience and Bayesian inference, and it aims to quantify the importance of each weight to the tasks the model has previously learned. The fundamental idea is that the weights critical to prior tasks should be altered less when new data is encountered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jisjhTM0TqPD1QyXUZdmuA.jpeg" /><figcaption><em>Figure 2 : EWC Schematic Parameter Space,</em> <a href="https://www.pnas.org/doi/full/10.1073/pnas.1611835114">https://www.pnas.org/doi/full/10.1073/pnas.1611835114</a></figcaption></figure><p>In Figure 2, we can clearly see the pivotal role that Elastic Weight Consolidation plays in preventing catastrophic forgetting when we train on task B, without losing the knowledge we’ve gained from task A. This diagram shows parameter space, with the grey areas signifying optimal performance for task A, and cream-colored regions indicating good performance for task B. After we’ve learned task A, our parameter values are labeled as θ*A.</p><p>If we concentrate only on task B and take steps in the direction of its gradient (as shown by the blue arrow), we’ll minimize the loss for task B, but potentially wipe out our knowledge of task A — this is the problem of catastrophic forgetting. On the other hand, if we constrain all weights with the same coefficient (as illustrated by the green arrow), we place a harsh restriction that lets us retain our memory of task A, but makes learning task B difficult.</p><p>This is where EWC steps in — it finds the sweet spot by identifying a solution for task B (indicated by the red arrow) that doesn’t drastically impact our knowledge of task A. It accomplishes this by specifically determining the importance of each weight in relation to task A.</p><p>EWC introduces a quadratic penalty to the loss function, constraining the modification of important weights. This penalty term is proportional to the square of the difference between the current and initial weight values, scaled by an importance factor. This importance factor, calculated from the Fisher Information Matrix, serves as a heuristic for a weight’s significance to the previously learned tasks.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fmath.embed.fun%2Fembed%2FmasieHquhViCgvtkztuQXp&amp;display_name=Embed+Fun&amp;url=https%3A%2F%2Fmath.embed.fun%2FmasieHquhViCgvtkztuQXp&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=embed" width="200" height="96" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c11e97544bb70f572104f0ddddfdb41/href">https://medium.com/media/5c11e97544bb70f572104f0ddddfdb41/href</a></iframe><p>In Elastic Weight Consolidation, a neural network is first trained on Task A, after which the Fisher Information Matrix (FIM) is computed and saved along with the learned weights. When training the network on Task B, EWC modifies the loss function to include a penalty term, computed using the saved FIM and weights, which discourages drastic changes to the weights critical for Task A, thus balancing learning the new task with preserving knowledge from the previous task. The quadratic nature of the penalty ensures that larger deviations from the initial weights incur a higher penalty. By assigning greater penalties to weights that contribute more to prior tasks, EWC aims to retain their learned knowledge while accommodating new information.</p><h4><strong>Progressive Neural Networks (ProgNet): Building Neural Network Towers</strong></h4><p>ProgNets introduce a new architecture that allows the network to expand when encountering new tasks. Instead of altering the weights of a single network, it adds a new network (or column) for each task, stacking these columns akin to building a tower. Each new column is connected to all the previously added columns but not the other way around, preserving the knowledge in the older columns.</p><p>Behind ProgNet, each task is learned by a separate column, and the output is a function of the inputs from all previous and current columns. The weights of previous columns remain frozen, preventing any catastrophic forgetting, while the weights of the new column are trained normally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*l4VzmBKvrGAyXYzV2ZpFcw.png" /><figcaption><em>Figure 3 : A Block-based ProgNet Model, </em><a href="https://arxiv.org/abs/1606.04671">https://arxiv.org/abs/1606.04671</a></figcaption></figure><p><em>​​</em>Imagine Progressive Neural Networks as a constellation of separate processing units, each having the ability to discern and harness the most pertinent inputs for the tasks they are assigned. Let’s consider an example from Figure 3, where output₃ not only interacts with its directly connected hidden layer, h₂, but also interfaces with the h₂ layers of prior columns, modifying their outputs through its unique lateral parameters. This output₃ unit scans and evaluates the available data, strategically omitting inputs that are unnecessary. For instance, if h₂¹ encapsulates all the needed information, output₃ may choose to neglect the rest. On the other hand, if both h₂² and h₂³ carry valuable information, output₃ could preferentially focus on these while ignoring h₂¹. These side connections empower the network to effectively manage the flow of information across tasks while also enabling it to exclude irrelevant data.</p><h4><strong>Optimized Fixed Expansion Layers (OFELs): A New Room for Each Task</strong></h4><p>The concept behind OFELs is like building a new room in a house for each new family member. In the context of neural networks, OFELs add a new layer for each task the LLM encounters. This layer expansion allows the network to accommodate new information without disrupting what it has already learned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*32vHbwE7BV-LQyCqlqj9BA.png" /><figcaption><em>Figure 4 : OFEL diagram, </em><a href="https://www.mdpi.com/2073-4425/10/7/553">https://www.mdpi.com/2073-4425/10/7/553</a></figcaption></figure><p>OFELs involve modifying the architecture of the network itself. Here, for each new task, a new layer is added to the neural network instead of retraining the entire network. This modification in architecture helps to encapsulate the knowledge required for the new task within that specific layer, minimising the impact on the pre-existing weights of the old layers.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fmath.embed.fun%2Fembed%2F7AGhk5LYY9QaxWquCkudhj&amp;display_name=Embed+Fun&amp;url=https%3A%2F%2Fmath.embed.fun%2F7AGhk5LYY9QaxWquCkudhj&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=embed" width="200" height="96" frameborder="0" scrolling="no"><a href="https://medium.com/media/899b60a88e1d7828d7e47c36d6b1cf63/href">https://medium.com/media/899b60a88e1d7828d7e47c36d6b1cf63/href</a></iframe><p>where g is the activation function. The architecture of OFELs is designed such that it allows for the inclusion of a new layer dedicated to the new task, which means that the network can process new inputs (x_new) independently of the old inputs (x_old). In essence, while the equation presents a comprehensive view of the underlying process in the architecture, during inference or prediction for a new task, we would typically use only x_new and not require x_old.</p><p>By selectively optimizing the new layers, OFELs strike a delicate balance between acquiring knowledge related to the new task and preserving the previously learned information. This meticulous optimization process allows the model to adapt to novel challenges while retaining its ability to leverage prior knowledge, ultimately facilitating more robust and versatile learning.</p><h4><strong>Forward Learnings</strong></h4><p>Forgetting — whether in humans or LLMs — is a fascinating paradox. On one hand, it can be an obstacle to continuous learning and adaptability. On the other, it’s an inherent part of how our brains and AI models manage and prioritize information. Strategies to counter catastrophic forgetting — Elastic Weight Consolidation, Progressive Neural Networks, and Optimized Fixed Expansion Layers — provide insightful yet diverse methodologies to preserve the retention capabilities of Large Language Models. Each offering distinct solutions, they reflect the resourcefulness and adaptability that the field of artificial intelligence must consistently embody. However, it is crucial to understand that the problem of catastrophic forgetting is not fully solved; there are still untapped avenues in this area demanding rigorous exploration, innovation, and creativity.</p><p>Addressing the challenge of catastrophic forgetting propels us not just towards more efficient AI systems, but towards a deeper understanding of learning and forgetting — a cognitive function shared by humans and machines alike. Therefore, it becomes an actionable imperative for researchers, scientists, practitioners, and anyone fascinated by the workings of intelligence, to contribute to this ongoing dialogue. The quest to tame the phenomenon of catastrophic forgetting is not merely an academic pursuit, but a journey that promises to redefine our relationship understanding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b91e114e281b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/understanding-what-we-lose-b91e114e281b">Understanding What We Lose</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The duty of the educated]]></title>
            <link>https://medium.com/@matt.tengtrakool/the-duty-of-the-educated-dd4f628808da?source=rss-720b7ac13f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd4f628808da</guid>
            <category><![CDATA[educational-equity]]></category>
            <category><![CDATA[education-reform]]></category>
            <category><![CDATA[educational-leadership]]></category>
            <category><![CDATA[philanthropy]]></category>
            <category><![CDATA[education]]></category>
            <dc:creator><![CDATA[Matt Tengtrakool]]></dc:creator>
            <pubDate>Wed, 04 Mar 2020 00:49:50 GMT</pubDate>
            <atom:updated>2023-06-04T19:30:37.575Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Democracy’s Glasses</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BBANrtQ73EToWV-A" /><figcaption>Photo by <a href="https://unsplash.com/@travelnow_or_crylater?utm_source=medium&amp;utm_medium=referral">travelnow.or.crylater</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In the complex dance of modern democracy, we’ve stumbled upon a troubling issue: our fixation on the immediate, the urgent, the now. This short-termism is a byproduct of our democratic machinery, a system that seeks re-election and appeals to the most obvious concerns. However, this shortsightedness becomes particularly concerning when we consider our nation’s incentive systems as they relate to our future.</p><p>Our democratic ethos, while championing freedom, equality, and individual agency, often morphs into an urgency to resolve pressing issues that are immediate and visible. Yet, this urgency sidelines long-term planning, putting future generations at risk. This form of hyperbolic discounting, a tendency to prefer smaller immediate payoffs to larger future ones, becomes a colossal roadblock when dealing with generational challenges like climate change that require a long-term outlook.</p><p>Consider the United States, often held up as a beacon of democracy. Its representatives are caught in a perpetual cycle of re-election, their political survival hinging on delivering tangible, immediate results within their term limits. Issues such as poverty, healthcare, and unemployment, which can be addressed within a neat two to six-year package, take precedence. These are the pressing concerns that sway voters, make headlines, and secure re-elections.</p><p>The Pew Research Center study in 2020 illustrated this perfectly. It found that the economy, healthcare, and Supreme Court appointments were the top concerns for voters in the U.S. presidential election. Climate change? It was fifth on the list, lagging significantly behind these immediate issues. This isn’t an exception. It’s a repeating pattern, replicated election after election. These pressing issues, while undeniably important, frequently push long-term environmental policies into the political shadows. The problem isn’t the issues being addressed; it’s the system that incentivizes short-term wins over long-term sustainability.</p><p>On the other hand, let’s examine China. This centralized society often outpaces democratic nations in its capacity for long-term planning. They’ve made significant investments in green energy, high-speed rail infrastructure, and sustainable projects that demonstrate a clear commitment to long-term governance. China’s 14th Five-Year Plan (2021–2025) provides a prime example, setting ambitious targets for reducing carbon intensity, increasing energy efficiency, and expanding the forest stock volume.</p><p>The Chinese Communist Party, not subject to electoral cycles, doesn’t face the same pressure to deliver immediate results. On the contrary, it’s in the regime’s best interests to prioritize long-term and existential threats, as its leaders worry more about the sustainability of their power in timeframes of decades and centuries. This, in theory, allows it to pursue more ambitious, long-term goals. For instance, in 2020 alone, China invested over $83 billion in renewable energy, outpacing Europe and the U.S. combined. Furthermore, the CCP’s top-down approach enables it to mobilize resources and implement policies at a speed and scale often challenging in more democratic societies.</p><p>But again, this structure is not without its drawbacks. The same centralization and concentration of power that allows for efficient action can also lead to potential human rights abuses, lack of transparency, and minimal public participation in decision-making processes. For one, China’s rapid urban development often displaces rural communities, and its strict population control measures have faced widespread criticism for infringing on human rights.</p><p>Thus, neither the democratic system exemplified by the United States nor the centralized approach of China has perfected the balance between addressing immediate needs and planning for the long term. Each offers valuable insights and cautionary tales. As we steer towards a sustainable future, it is crucial that we take these lessons into account, learning not just from our successes but also from our missteps.</p><p>So, how do we bridge the gap between long-term policy planning and democratic governance to create a more balanced, sustainable approach?</p><p>First, we need to change what it means to be a representative. Career politicians, who are always looking to get re-elected, tend to focus on short-term issues. This, however, can be addressed by instituting term limits. Term limits bring new people into the political arena, each carrying fresh ideas and perspectives, thereby encouraging long-term planning. Representatives should speak up for the needs of future generations, not just today’s voters. Our call to action here is clear: push for legislation that imposes term limits and discourages career politics. We need more statesmen and fewer politicians, more public servants and fewer party servants.</p><p>To promote a shift towards long-term thinking in politics, it is imperative to reconsider the role of lobbyists. While skepticism is often associated with lobbying, we should approach it with a balanced perspective that combines optimism and scrutiny. By empowering political organizations with the responsibility for long-sighted growth, we can encourage a more strategic and forward-thinking approach to policymaking. This requires an educational shift, a cultural movement. We must integrate long-term thinking into our curricula, from elementary schools to universities. Public discussions and forums should be organized to educate the citizenry about the importance of sustainability and existential challenges. Educators, media organizations, and community leaders need to foster a culture of long-term thinking.</p><p>Lastly, to foster a more sustainable future, it is imperative to critically evaluate our existing economic models that often prioritize short-term gains over long-term benefits. Economists and researchers have been pioneering alternative approaches, such as ecological economics and the concept of natural capital, which recognize the value of environmental resources and advocate for their sustainable utilization. By adopting these new economic models, we can shift towards a future that values long-term well-being and environmental stewardship for the benefit of future generations.</p><p>Lastly, our political systems need to build long-term goals. This could mean setting legally binding climate targets, as we’ve seen with the Paris Agreement. But we need to go further. We need to create institutions dedicated to long-term planning. These could be governmental bodies or independent think tanks funded by the government. And perhaps we need to rethink the structure of our political system. Maybe we need a House of the Future in our legislatures, a body of representatives whose sole responsibility is to advocate for future generations.</p><p>So, where do we go from here? We start by acknowledging that democracy and longtermism are not oil and water. They’re not incompatible by design. In fact, longtermism is as natural as democracy. It’s just that the framework of our democratic system as it stands today tends to prioritize the present over the future.</p><p>We built this system, and we can rebuild it. We can, and we absolutely must, construct democratic systems that balance the pressing needs of today with the critical considerations of tomorrow. Our very survival depends on our ability to play the long game within our democratic systems. The prosperity of future generations rests on our willingness to look beyond the next election cycle, the next fiscal quarter, the next news cycle. It’s a challenge to every citizen, every politician, every human being. We need to rethink, rebuild, and renew our commitment to a future that extends beyond our own lifespan.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd4f628808da" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>