<?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 Zahrizhal Ali (Cal) on Medium]]></title>
        <description><![CDATA[Stories by Zahrizhal Ali (Cal) on Medium]]></description>
        <link>https://medium.com/@zahrizhalali?source=rss-c5565aa21575------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DikBhI3Fn17QzKEVpLvAEA.jpeg</url>
            <title>Stories by Zahrizhal Ali (Cal) on Medium</title>
            <link>https://medium.com/@zahrizhalali?source=rss-c5565aa21575------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:53:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zahrizhalali/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[Learned Memory > RAG: Building Agents that Actually Learn.]]></title>
            <link>https://medium.com/@zahrizhalali/learned-memory-rag-building-agents-that-actually-learn-19867ced299a?source=rss-c5565aa21575------2</link>
            <guid isPermaLink="false">https://medium.com/p/19867ced299a</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Zahrizhal Ali (Cal)]]></dc:creator>
            <pubDate>Fri, 26 Dec 2025 02:45:41 GMT</pubDate>
            <atom:updated>2025-12-26T02:45:41.037Z</atom:updated>
            <content:encoded><![CDATA[<h3>TL;DR</h3><ul><li><strong>Problem:</strong> Standard RAG agents are stateless; they have to re-derive the same insights from raw documents every single time.</li><li><strong>The Solution:</strong> <strong>Learned Memory</strong>. Instead of just reading, we give the agent a “write” tool.</li><li><strong>The Stack:</strong> Agno (Orchestration) + Supabase (Postgres/PgVector).</li><li><strong>The Mechanism:</strong> The agent proposes an insight -&gt; Human confirms it -&gt; Insight is vector-embedded for future retrieval.</li><li><strong>The Result:</strong> An agent that gets smarter and faster the more you use it.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PP1BJ9rqLU9aFNefcpimAA.png" /><figcaption>AI Generated that I desperately need.</figcaption></figure><p>Retrieval Augmented Generation (RAG) is the industry darling right now, but let’s be honest about what it actually is: a glorified search engine attached to a parrot.</p><p>RAG is fantastic for <strong>fact retrieval, Ngl</strong>. If you need an agent to query a 500-page PDF to find a warranty clause, RAG is your boy. But RAG might perform miserably at <strong>learning</strong>. Here is the scenario: You ask your agent to debug a complex race condition in your C++ code (ew). It spends 30 seconds reasoning, analyzing the stack trace, and finally spits out the correct fix. You thank it (meh), close the session, and go to lunch.</p><p>When you come back and ask a similar question, a standard RAG agent has to start from zero. It has to re-read the docs and re-derive the logic. It didn’t learn how to solve the bug; it just looked up the syntax again. It’s like a student who aces an open-book exam but forgets everything the moment they walk out of the room (not me).</p><blockquote>Your agent doesn’t need more context windows. It needs experience.</blockquote><blockquote>-random dude on X.</blockquote><h3>Enter Learned Memory</h3><p>We are going to move beyond passive retrieval. We are going to give the agent “write” access to its own brain.</p><p>In this architecture, instead of indiscriminately chunking and embedding every document you have (and turning your vector DB into a semantic garbage dump), we give the agent a tool to explicitly <strong>save</strong> insights.</p><ul><li><strong>RAG:</strong> “I found this text in a document that looks relevant.”</li><li><strong>Learned Memory:</strong> “I solved this problem, and I am writing down (remember) the solution so I don’t have to think this hard next time.”</li></ul><p>If you want to learn more about Agent Memory, check out this really course source: <a href="https://www.ashpreetbedi.com/articles/memory">https://www.ashpreetbedi.com/articles/memory</a>.</p><p>We are building this using <strong>Agno</strong> for the orchestration and <strong>Supabase (Postgres + PgVector)</strong> for the storage. We’ll also throw in a “Human-in-the-Loop” mechanism because, let’s face it, LLMs are confident liars, and we don’t want hallucinations poisoning our database.</p><p>Let’s build an agent that actually gets smarter the more you use it.</p><h4>Stack and Setup</h4><p>We aren’t here to debate package managers. Whether you use uv, poetry, or raw pip like a chaotic neutral, just make sure you have the dependencies installed to support the imports.</p><p>You need agno (really cool startup I think, providing lightweight agents SDK), openai for the brains (or any models u’d prefer), rich for the pretty terminal output, and the Postgres/Vector drivers. pgvector and sqlalchemy.</p><h3>The Database: Supabase</h3><p>We aren’t spinning up local Docker containers today. We need a persistent Postgres instance that stays online when you close your laptop. We’re using Supabase because it gives us Postgres + pgvector out of the box with zero friction.</p><ol><li><strong>Create a Project:</strong> Go to <a href="https://supabase.com/">Supabase</a>, sign in, and hit “New Project.”</li><li><strong>Get the URL:</strong> Go to <strong>Project Settings -&gt; Database -&gt; Connection String</strong>. Copy the URI. It should look like postgresql://postgres.[ref]:[password]@aws-0-region.pooler.supabase.com:6543/postgres.</li><li><strong>Config:</strong> Paste that into a .env file as DATABASE_URL.</li><li>Oh, make sure you have OpenAI (in my case) under OPENAI_API_KEY too.</li></ol><h3>Two Types of Memory</h3><p>Most tutorials (mine too maybe) confuse these. Let’s clarify.</p><pre># 1. Session Memory (chat memory)<br>agent_db = PostgresDb(<br>    db_url=os.getenv(&quot;DATABASE_URL&quot;),<br>    session_table=&quot;my_session_table&quot;, # Name your chat table here<br>)<br><br># 2. Learned Memory (as the name, learned memory).<br>learnings_kb = Knowledge(<br>    name=&quot;Agent Learnings&quot;, <br>    vector_db=PgVector(<br>        db_url=os.getenv(&quot;DATABASE_URL&quot;),<br>        table_name=&quot;learnings&quot;,  # Name your learning table here<br>        search_type=SearchType.hybrid,<br>    ),<br>)</pre><p>Shamelessly copied-pasted from Gemini 👇:</p><ul><li><strong>agent_db (Session Memory):</strong> This is your standard chat history. It allows the agent to remember what you said <em>five minutes ago</em> in the current conversation. as most session memory, it will store session_id for your context history. (Definitly store this in db but in this demo, im just using arbitrary session id)</li><li><strong>learnings_kb (Learned Memory):</strong> This is the permanent vault. We use PgVector here because we want semantic search. When the agent saves an insight, it lives here forever, accessible across <em>different</em> sessions.</li></ul><h3>The Agent’s Pen (The Tool)</h3><p>This is the main part of the stack. Most RAG pipelines are read-only; this tool gives the agent <strong>write access</strong>. With your permission! (Also copied-pasted from blog i’ve shared above).</p><pre>@tool(requires_confirmation=True)<br>def save_learning(title: str, learning: str) -&gt; str:<br>    &quot;&quot;&quot;<br>    Save a reusable insight to the knowledge base.<br><br>    Args:<br>        title: Short descriptive title<br>        learning: The insight — specific and actionable<br>    &quot;&quot;&quot;<br>    payload = {<br>        &quot;title&quot;: title.strip(),<br>        &quot;learning&quot;: learning.strip(),<br>        &quot;saved_at&quot;: datetime.now(timezone.utc).isoformat(),<br>    }<br><br>    learnings_kb.add_content(<br>        name=payload[&quot;title&quot;],<br>        text_content=json.dumps(payload),<br>    )<br><br>    return f&quot;Saved: &#39;{title}&#39;&quot;</pre><p>Notice the requires_confirmation=True. We aren&#39;t letting the agent write to our learning database unsupervised. If you let an LLM auto-save to its memory without checks, you end up with a vector store full of hallucinations and polite fluff like &quot;The user is very nice.&quot; (which im not).</p><h3>Trust, But Verify (Execution Loop)</h3><p>Standard agent.run() calls are dangerous when database writes are involved. We need to intercept the tool call.</p><pre><br># Session Id<br>session_id = &quot;learned-memory-demo-chat&quot; <br><br>run_response = agent.run(&quot;Analyze TSLA and save any insights&quot;, session_id=session_id)<br><br>for requirement in run_response.active_requirements:<br>    if requirement.needs_confirmation:<br>        print(f&quot;Tool: {requirement.tool_execution.tool_name}&quot;)<br>        print(f&quot;Args: {requirement.tool_execution.tool_args}&quot;)<br><br>        choice = (<br>            Prompt.ask(<br>                &quot;Do you want to continue?&quot;,<br>                choices=[&quot;y&quot;, &quot;n&quot;],<br>                default=&quot;y&quot;,<br>            )<br>            .strip()<br>            .lower()<br>        )<br><br>        if choice == &quot;y&quot;:<br>            requirement.confirm()<br>        else:<br>            requirement.reject()<br><br>if run_response.requirements:<br>    run_response = agent.continue_run(<br>        run_id=run_response.run_id,<br>        requirements=run_response.requirements,<br>        session_id=session_id<br>    )<br><br>    pprint_run_response(run_response)<br>else:<br>    pprint_run_response(run_response)</pre><p>This loop is the difference between a “prototype” and “production.” The agent <em>proposes</em> a memory update (“I learned that TSLA is volatile around earnings”), and you, the human, <em>ratify</em> it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qOdbm5b_cZwsxpUVwyY2Sg.png" /><figcaption>You have control</figcaption></figure><p>Once ratified, that insight is embedded and stored in Supabase. Next time you ask about “TSLA strategy,” the agent doesn’t search Google (say you have other tools/MCP servers), it searches its own memory first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SwZR3WIUS0a0u6-mIu62mw.png" /><figcaption>Oh wow there’s our Agent brain. Yay.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GlezYFRY535RQmm79nYF9w.png" /><figcaption>And below “learned-memory-demo-chat” is our Chat History Session Id. Yay too ?</figcaption></figure><p>Now let’s do test our chat memory and learned memory:</p><pre><br># Dont forget to pass our session-id<br>session_id = &quot;learned-memory-demo-chat&quot;<br><br>run_response = agent.run(&quot;what&#39;s your insights from both stocks you know?&quot;, <br>                session_id=session_id)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xUcZE156BU0IkX3_3b_LjQ.png" /><figcaption>Hell yeah.</figcaption></figure><h3>Addressing the Elephant: “Isn’t this literally just RAG?”</h3><p>I can hear the comments section already: <em>“Bro, you just described RAG but with extra steps.”</em></p><p>Technically? Yes. We are embedding text and retrieving it with cosine similarity. The mechanics are identical.</p><p><strong>But the intent is completely different.</strong></p><p>Standard RAG is <strong>passive consumption</strong>. You dump 5,000 PDF pages into a vector store and hope the chunking strategy wasn’t terrible. The agent has no agency over what is stored; it just reads what it’s fed. It’s the equivalent of an open-book test where the student has to read the entire textbook every time to answer a question.</p><p>Learned Memory is <strong>active synthesis</strong>. The agent (with your help) decides what is worth remembering. It doesn’t save the raw stock data for NVDA; it saves the <em>conclusion</em> it drew from that data.</p><ul><li><strong>RAG:</strong> Retrieves the raw 10k filing.</li><li><strong>Learned Memory:</strong> Retrieves the insight: <em>“NVDA tends to dip 2% immediately after earnings beats due to high retail sell-offs.”</em></li></ul><p>RAG gives you data. Learned Memory gives you wisdom.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FQukdjwP-mNOIvJL" /><figcaption>Your agent after receiving Wisdom from Learned Memory instead of RAG.</figcaption></figure><pre>import json<br>from datetime import datetime, timezone<br><br>from agno.agent import Agent, RunOutput<br>from agno.db.postgres import PostgresDb<br>from agno.knowledge import Knowledge<br>from agno.models.openai import OpenAIChat<br>from agno.vectordb.pgvector import PgVector<br>from agno.vectordb.search import SearchType<br>from dotenv import load_dotenv<br>import os<br>from agno.tools import tool<br>from rich.prompt import Prompt<br>from agno.utils.pprint import pprint_run_response<br><br>load_dotenv()<br><br>agent_db = PostgresDb(<br>    db_url=os.getenv(&quot;DATABASE_URL&quot;),<br>    session_table=&quot;my_session_table&quot;, # Specify the table to store sessions<br>)<br><br><br>learnings_kb = Knowledge(<br>    name=&quot;Agent Learnings&quot;,<br>    vector_db=PgVector(<br>        db_url=os.getenv(&quot;DATABASE_URL&quot;),<br>        table_name=&quot;learnings&quot;,<br>        search_type=SearchType.hybrid,<br>    ),<br>)<br><br>@tool(requires_confirmation=True)<br>def save_learning(title: str, learning: str) -&gt; str:<br>    &quot;&quot;&quot;<br>    Save a reusable insight to the knowledge base.<br><br>    Args:<br>        title: Short descriptive title<br>        learning: The insight — specific and actionable<br>    &quot;&quot;&quot;<br>    payload = {<br>        &quot;title&quot;: title.strip(),<br>        &quot;learning&quot;: learning.strip(),<br>        &quot;saved_at&quot;: datetime.now(timezone.utc).isoformat(),<br>    }<br><br>    learnings_kb.add_content(<br>        name=payload[&quot;title&quot;],<br>        text_content=json.dumps(payload),<br>    )<br><br>    return f&quot;Saved: &#39;{title}&#39;&quot;<br><br>agent = Agent(<br>    model=OpenAIChat(id=&quot;gpt-4o-mini&quot;),<br>    tools=[save_learning],<br>    knowledge=learnings_kb,<br>    search_knowledge=True,<br>    db=agent_db,<br>)<br><br><br>session_id = &quot;learned-memory-demo-chat&quot;<br><br>run_response = agent.run(&quot;Analyze TSLA and save any insights.&quot;, <br>session_id=session_id)<br><br>for requirement in run_response.active_requirements:<br>    if requirement.needs_confirmation:<br>        print(f&quot;Tool: {requirement.tool_execution.tool_name}&quot;)<br>        print(f&quot;Args: {requirement.tool_execution.tool_args}&quot;)<br><br>        choice = (<br>            Prompt.ask(<br>                &quot;Do you want to continue?&quot;,<br>                choices=[&quot;y&quot;, &quot;n&quot;],<br>                default=&quot;y&quot;,<br>            )<br>            .strip()<br>            .lower()<br>        )<br><br>        if choice == &quot;y&quot;:<br>            requirement.confirm()<br>        else:<br>            requirement.reject()<br><br>if run_response.requirements:<br>    run_response = agent.continue_run(<br>        run_id=run_response.run_id,<br>        requirements=run_response.requirements,<br>        session_id=session_id<br>    )<br><br>    pprint_run_response(run_response)<br>else:<br>    pprint_run_response(run_response)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19867ced299a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Agents from LLMs? Set up and Code our own LLM-based agents using Streamlit and Langchain!.]]></title>
            <link>https://medium.com/@zahrizhalali/agents-from-llms-set-up-and-code-our-own-llm-based-agents-using-streamlit-and-langchain-b3ebf4d1dcef?source=rss-c5565aa21575------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3ebf4d1dcef</guid>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Zahrizhal Ali (Cal)]]></dc:creator>
            <pubDate>Sun, 29 Oct 2023 12:04:56 GMT</pubDate>
            <atom:updated>2023-10-29T12:07:36.068Z</atom:updated>
            <content:encoded><![CDATA[<h3>Agents from LLMs 😱? Set up and Code our own LLM-based agents using Streamlit and Langchain 🚀!</h3><p>In the subfield of Artificial Intelligence, we have Reinforcement Learning (RL). RL literally used what we know as an agent to learn and interact to the environment corresponds to specific policy to get rewards based on the internal state occurence. LLM-based agents, on the other hand, are akin to having an encyclopedia combined with a calculator. They don’t just compute; they understand, reason, and then act, drawing from a vast reservoir of information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6RUiCibOK-H_XjAT" /><figcaption>Photo by <a href="https://unsplash.com/@the_shantanu_kumar?utm_source=medium&amp;utm_medium=referral">Shantanu Kumar</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So, what is Agents?. Here’s a beautiful definition I got from AutoGPT repositories (they have a neat work there lol) . “<em>autonomous agents operated with limited knowledge, often confined to specific tasks or environments. They were like calculators — efficient but limited to predefined functions.” </em>In simple terms, agent is a program that can work intelligently by interacting with its environment using its knowledge. Imagine you’re in the class, then each individual ‘teacher’, ‘student’, etc is 1 single agent. So in that case, an Agent is an entities capable of perceiving its environments, making decisions, and taking actions.</p><p>Now we have talked about the short brief of agent, why the heck is it very important and (to me) can be a very promising fields?. Well, we know that AI is a field dedicated to develop a system that can replicate human-like intelligence and abilities, and our new big dog, the LLMs has already capable obtaining human-like decision-making and in-context understanding from specific set of knowledge and tasks. LLMs literally brought a significant advantages which makes them highly desirable for building intelligent agents. The idea is using LLMs to make them behave like humans and complete various tasks effectively.</p><p>Referring to a very cool paper “A Survey on Large Language Model based Autonomous Agents” by Wang, Lei et. al., large language models (LLMs) have achieved notable successes, demonstrating significant potential in attaining human-like intelligence. Basically, LLM-based agents has three key parts: brain, perception and action.</p><p>Although the recent advancements in LLMs, there is still some gap, LLMs are still far from fulfilling specific roles and autonomously perceive and learn from an environment to perform specific tasks like humans, therefore, in order to bridge the gap between traditional LLMs and autonomous agents, they specify and design an architecture for the agent to assist LLMs in maximizing their capabilities. These architectures and modules are mainly composed as profile, memory, planning, and action.</p><p>Enough talking today we’re going to build our own Agent using LLM model like GPT-3.5 with Langchain. The aim is basically just to make this agent to act as your research assistant. The possibilities almost unlimited but for now let’s just make it as it is. This is also a good and fresh opportunity to deep dive into the amazing realm of Agent based model especially incorporating with LLMs.</p><h3>Setup</h3><p>First, let’s create a New Project and make 1 python scritpt called main.py . Next is to setup your environment, I would recommend use PyCharm and to use package manager such as Anaconda. Anyway here’s some requirements or package you need to install on your local machine:</p><ul><li>Python 3.9</li><li>Langchain</li><li>Streamlit</li><li>BeautifulSoup4</li></ul><p>And setup your own API Key below:</p><ul><li><a href="https://openai.com/">OpenAI API Keys</a></li><li><a href="https://serper.dev/">Serper</a> API</li><li><a href="https://www.browserless.io/">Browserless API Key</a></li></ul><p>Im not going into detail on how to install these packages, and set up this API, for packages you just need to install on your local and for API just sign up to their platform and create your API.</p><p>Once you’re done, in your project directory, make .env file. This is where we’re going to save our API Keys. As such:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*O1MjZMV9QQ4uz1XNTyFO9Q.png" /></figure><p>Remember, to keep your API Key safe and dont share it publicly (like i did above, lol).</p><h3>Let’s Code</h3><p>First, lets import some of our packages that we’re going to use:</p><pre>import os<br>from dotenv import load_dotenv<br><br>from langchain import PromptTemplate<br>from langchain.agents import initialize_agent, Tool<br>from langchain.agents import AgentType<br>from langchain.chat_models import ChatOpenAI<br>from langchain.prompts import MessagesPlaceholder<br>from langchain.memory import ConversationSummaryBufferMemory<br>from langchain.text_splitter import RecursiveCharacterTextSplitter<br>from langchain.chains.summarize import load_summarize_chain<br>from langchain.tools import BaseTool<br>from pydantic import BaseModel, Field<br>from typing import Type<br>from bs4 import BeautifulSoup<br>import requests<br>import json<br>import streamlit as st<br>from langchain.schema import SystemMessage<br>from fastapi import FastAPI</pre><p>Yo. what the hell that’s a whole lot of code.</p><p>Well I know. bear with me we’ll use all of them.</p><p>Next is to import our API key from .env that we’ve created earlier. For that you must have python-dotenv already pre-installed.</p><pre>load_dotenv()<br><br>OPENAI_API_KEY= os.getenv(&quot;OPENAI_API_KEY&quot;)<br>SERPER_API = os.getenv(&#39;SERPER_API_KEY&#39;)<br>BROWSERLESS_API = os.getenv(&#39;BROWSERLESS_API_KEY&#39;)</pre><h3>Build our first Tool</h3><p>We’re going to create our Research Assistant which has the capabilities to browse through the internet and grab essential information for you rather than you do it manually. For that we need to create 1 tool that our agent are going to use to browse through the internet, this is where we’re going to use Serper. Serper is a low-cost Google Search API that can be used to add answer box, knowledge graph, and organic results data from Google Search. We will of course use the information grabbed by Serper and pass them to our agent later. Here’s the code:</p><pre>def search_tool(query):<br>    base_url = &quot;https://google.serper.dev/search&quot;<br><br>    payload = json.dumps({<br>        &quot;q&quot;: query<br>    })<br><br>    headers = {<br>        &#39;X-API-KEY&#39;: SERPER_API,<br>        &#39;Content-Type&#39;: &#39;application/json&#39;<br>    }<br><br>    response = requests.request(&quot;POST&quot;, base_url,<br>                                headers=headers,<br>                                data=payload)<br><br>    return response.text<br><br># Test Serper<br>QUERY = &quot;What is Meta?&quot;<br>response = search_tool(QUERY)<br><br>print(response)</pre><p>Here’s the output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*crmB8wwQjdZYuZHS0YsSlA.png" /></figure><p>Wow that’s a lot of information.</p><p>Notice from above output we have the site link and the title of the site. These are going to be really handy in terms of having our model to open that link and grab information within that website. From above we’re only listed some related links to our query. Basically it’s like you just google search the query and then it return some information and link for you, you just need to open them 1 by 1 to find what you’re looking for. Instead of manually doing this, we want our agent to do it automatically. For that we’re going to utilize Browserless API and BeautifulSoup. What Browserless would do is to open the link for you and what the BeautifulSoup would do is to grab or extract information from within that site. Simple right? No. It’s not.</p><p>Here’s the code anyway:</p><pre>def scrape_website(objective: str, url: str):<br><br>    print(&quot;Scraping website...&quot;)<br>    # Define the headers for the request<br>    headers = {<br>        &#39;Cache-Control&#39;: &#39;no-cache&#39;,<br>        &#39;Content-Type&#39;: &#39;application/json&#39;,<br>    }<br><br>    # Define the data to be sent in the request<br>    data = {<br>        &quot;url&quot;: url<br>    }<br><br>    # Convert Python object to JSON string<br>    data_json = json.dumps(data)<br><br>    # Send the POST request<br>    post_url = f&quot;https://chrome.browserless.io/content?token={BROWSERLESS_API}&quot;<br>    response = requests.post(post_url, headers=headers, data=data_json)<br><br>    # Check the response status code<br>    if response.status_code == 200:<br>        soup = BeautifulSoup(response.content, &quot;html.parser&quot;)<br>        text = soup.get_text()<br>        print(&quot;CONTENT:&quot;, text)        <br>    else:<br>        print(f&quot;HTTP request failed with status code {response.status_code}&quot;)</pre><p>Now let’s run this function:</p><pre>scrape_website(&quot;What is langchain?&quot;, &quot;https://python.langchain.com/docs/get_started/introduction&quot;)</pre><p>Here’s what we got:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eKPBGW7W-Cff-Tsb3WrCiA.png" /></figure><p>You might not see the entire text but BeautifulSoup actually returns a lot of text from the website. This can be troublesome if we want to pass them to our LLM model because well there are limited tokens for each models. To fix this we can trim or slice the text, however this cannot be good because we might lose important information that our model will eventually failed to return a good result. Fortunately, we have OpenAI!. OpenAI has specific model to summaryze text. Using this summarized text we wont lose important information since the model are trained to shorten this long information and yet keep important text into account. (i dont even know what im trying to say here, but you’ve got the idea.)</p><p>Let’s create our summaryzer model using GPT-3.5-Turbo. For that this is where we’re going to use OpenAI API:</p><pre>def summary(objective, content):<br>    llm = ChatOpenAI(temperature=0, model=&quot;gpt-3.5-turbo&quot;)<br><br>    # llm = ChatOpenAI(temperature=0, model=&quot;gpt-3.5-turbo-16k-0613&quot;)<br>    text_splitter = RecursiveCharacterTextSplitter(<br>        separators=[&quot;\n\n&quot;, &quot;\n&quot;], chunk_size=1000, chunk_overlap=500)<br>    docs = text_splitter.create_documents([content])<br>    map_prompt = &quot;&quot;&quot;<br>    Write a summary of the following text for {objective}:<br>    &quot;{text}&quot;<br>    SUMMARY:<br>    &quot;&quot;&quot;<br>    map_prompt_template = PromptTemplate(<br>        template=map_prompt, input_variables=[&quot;text&quot;, &quot;objective&quot;])<br><br>    summary_chain = load_summarize_chain(<br>        llm=llm,<br>        chain_type=&#39;map_reduce&#39;,<br>        map_prompt=map_prompt_template,<br>        combine_prompt=map_prompt_template,<br>        verbose=True<br>    )<br><br>    output = summary_chain.run(input_documents=docs, objective=objective)<br><br>    return output</pre><p>Once you’ve done that, we pass this summary function to our scrape_website() function.</p><pre>..... <br><br># Send the POST request<br>    post_url = f&quot;https://chrome.browserless.io/content?token={BROWSERLESS_API}&quot;<br>    response = requests.post(post_url, headers=headers, data=data_json)<br>    <br>    if response.status_code == 200:<br>        soup = BeautifulSoup(response.content, &quot;html.parser&quot;)<br>        text = soup.get_text()<br>        print(&quot;CONTENT:&quot;, text)<br>        <br>        # Write validation<br>        if len(text) &gt; 10000:<br>            # Call summary function to summaryze text<br>            output = summary(objective, text)<br>            return output<br>        else:<br>            return text<br>    else:<br>        print(f&quot;HTTP request failed with status code {response.status_code}&quot;)</pre><h3>Initialize Agent</h3><p>Alright we’re almost done. Let’s initialize Class so that we can easily manage our tools for our agent.</p><pre>class ScrapeWebsiteInput(BaseModel):<br>    &quot;&quot;&quot;Inputs for scrape_website&quot;&quot;&quot;<br>    objective: str = Field(<br>        description=&quot;The objective &amp; task that users give to the agent&quot;)<br>    url: str = Field(description=&quot;The url of the website to be scraped&quot;)<br><br><br>class ScrapeWebsiteTool(BaseTool):<br>    name = &quot;scrape_website&quot;<br>    description = &quot;useful when you need to get data from a website url, passing both url and objective to the function; DO NOT make up any url, the url should only be from the search results&quot;<br>    args_schema: Type[BaseModel] = ScrapeWebsiteInput<br><br>    def _run(self, objective: str, url: str):<br>        return scrape_website(objective, url)<br><br>    def _arun(self, url: str):<br>        raise NotImplementedError(&quot;error here&quot;)</pre><p>Before we initialize our agents, lets do Profiling. Profiling module aims to indicate the profiles of the agent roles, which are usually written into the prompt to influence the LLM behaviors. The choice of information to profile the agent is largely determined by the specific application scenarios. For instance, if the application aims to study human cognitive process, then the psychology information becomes pivotal.</p><pre>tools = [<br>    Tool(<br>        name=&quot;Search&quot;,<br>        func=search_tool,<br>        description=&quot;useful for when you need to answer questions about current events, data. You should ask targeted questions&quot;<br>    ),<br>    ScrapeWebsiteTool(),<br>]<br><br>system_message = SystemMessage(<br>    content=&quot;&quot;&quot;You are a world class researcher, who can do detailed research on any topic and produce facts based results; <br>            you do not make things up, you will try as hard as possible to gather facts &amp; data to back up the research<br><br>            Please make sure you complete the objective above with the following rules:<br>            1/ You should do enough research to gather as much information as possible about the objective<br>            2/ If there are url of relevant links &amp; articles, you will scrape it to gather more information<br>            3/ After scraping &amp; search, you should think &quot;is there any new things i should search &amp; scraping based on the data I collected to increase research quality?&quot; If answer is yes, continue; But don&#39;t do this more than 3 iteratins<br>            4/ You should not make things up, you should only write facts &amp; data that you have gathered<br>            5/ In the final output, You should include all reference data &amp; links to back up your research; You should include all reference data &amp; links to back up your research<br>            6/ In the final output, You should include all reference data &amp; links to back up your research; You should include all reference data &amp; links to back up your research&quot;&quot;&quot;<br>)<br><br>agent_kwargs = {<br>    &quot;extra_prompt_messages&quot;: [MessagesPlaceholder(variable_name=&quot;memory&quot;)],<br>    &quot;system_message&quot;: system_message,<br>}</pre><p>Above , inside tools, we defined 2 tools that our agents are going to utilize.</p><p>So now, lets’ initialize our agent:</p><pre>llm = ChatOpenAI(temperature=0, model=&quot;gpt-3.5-turbo&quot;)<br>memory = ConversationSummaryBufferMemory(<br>    memory_key=&quot;memory&quot;, return_messages=True, llm=llm, max_token_limit=1000)<br><br>agent = initialize_agent(<br>    tools,<br>    llm,<br>    agent=AgentType.OPENAI_FUNCTIONS,<br>    verbose=True,<br>    agent_kwargs=agent_kwargs,<br>    memory=memory,<br>)</pre><p>We’ve initialized our LLM-based agent, to make it work with our tools we passed the initialized tools to initialize_agent() from langchain. Next, let’s setup our Streamlit.</p><p><strong>Streamlit</strong> is an open-source app framework for Machine Learning where you can basically create beautiful web apps in minutes. It will works perfectly when we inject our llm agents to this framework. Now let’s do that!:</p><p>First, lets define main() :</p><pre>def main():<br>    st.set_page_config(page_title=&quot;AI research agent&quot;, page_icon=&quot;:bird:&quot;)<br><br>    st.header(&quot;AI research agent&quot;)<br>    query = st.text_input(&quot;What would you like to know?&quot;)<br><br>    if query:<br>        st.write(&quot;Doing research for &quot;, query)<br><br>        result = agent({&quot;input&quot;: query})<br><br>        st.info(result[&#39;output&#39;])</pre><p>Now we trigger the main function with this code, its a standard practice:</p><pre>if __name__ == &#39;__main__&#39;:<br>    main()</pre><p>Now. go ahead and run Streamlit. Here’s what you need to run in your terminal (Note: Make sure you are using the right environment before executing streamlit):</p><p>streamlit run main.py</p><p>Thats it :).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*YaXoMs4y_lF7xEJ--Nkaww.png" /><figcaption>This is what happen after you run the streamlit command ;)</figcaption></figure><p>Local URL can be used to access the live web app the streamlit had created for you. Network URL can be used outside your device as long as you’re in the same network. Now go to your browser and copy-paste the local URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bQc-AJtpJ_8k9oS0ue2FNg.png" /><figcaption>Voila.</figcaption></figure><p>Pretty cool right? ,. Well. Not Yet. Let’s try our Agent by trying some query :).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tJpG8ANqZBov6rXJYnU_3Q.png" /></figure><p>Now let’s see how the agent managed to retrieve some information.</p><p>Note if you’re using free tier OpenAI API you might not be able to make this model works due to usage limit :(. But if you have billing setup for your api keys then it should work :).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*5pM3BY02oGZZofuIETv8BA.png" /><figcaption>Credit: <a href="https://www.youtube.com/@AIJasonZ">https://www.youtube.com/@AIJasonZ</a></figcaption></figure><p>Cool Right?. Happy Coding :).</p><p>Recources:</p><ol><li><a href="https://openai.com/">https://openai.com/</a></li><li><a href="https://www.langchain.com/">https://www.langchain.com/</a></li><li><a href="https://serper.dev/">https://serper.dev/</a></li><li><a href="https://www.browserless.io/">https://www.browserless.io/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3ebf4d1dcef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Baby Step: Fine-Tune Falcon-7B and LLaMa-7b Large Language Models for your custom datasets.]]></title>
            <link>https://medium.com/@zahrizhalali/baby-step-fine-tune-falcon-7b-large-language-models-for-your-custom-datasets-e1df8c473f56?source=rss-c5565aa21575------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1df8c473f56</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[generative]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[gpt]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Zahrizhal Ali (Cal)]]></dc:creator>
            <pubDate>Thu, 10 Aug 2023 04:08:55 GMT</pubDate>
            <atom:updated>2023-08-15T01:27:02.142Z</atom:updated>
            <content:encoded><![CDATA[<p>With an audacious spirit, we delve into the enigmatic corridors of the Falcon-7B Large Language Model, unraveling its latent potential with the precision of a seasoned linguist and the curiosity of a starry-eyed explorer. In an era where mere words wield the power to reshape realities and ignite revolutions, we, the intrepid voyagers, dare to take but a humble stride — a “baby step,” if you will — towards the summit of linguistic finesse.</p><p>Here I will be guiding you to train your own LLM models using any State-of-the-art models available on Huggingface. Using below code template, you should be able to fine-tune any LLM model for your own scenario or dataset. But for the sake of simplicity, i will only show you on how to fine-tune Falcon-7b LLM and i will show you later how you may also try it with LLaMa. The Falcon model uses only 75 percent of GPT-3’s training compute, 40 percent of Chinchilla’s, and 80 percent of PaLM-62B’s. (Src: <a href="https://falconllm.tii.ae/">https://falconllm.tii.ae/</a>). Note, we will be using the model from this repo as well : <a href="https://huggingface.co/ybelkada/falcon-7b-sharded-bf16">https://huggingface.co/ybelkada/falcon-7b-sharded-bf16</a> so shout out to this guy ✌️.</p><p>If you follow along with me, nothing should go wrong, (i hope so). Alright, enough talking bs, lets get started.</p><p>I also want to give credit to the guy who created the repo: <a href="https://github.com/iamarunbrahma/finetuned-qlora-falcon7b-medical">https://github.com/iamarunbrahma/finetuned-qlora-falcon7b-medical</a> , i’ve made this entire code and tutorials by referring to this guy works. Shout out to this guy as well ✌️. My goal is to just reproduce this amazing works because the first time I’ve tried, it didn’t goes very well, so Iiwill show you the more complete detail on how i finally succeeded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SB6nFGt5Bx6nS_vJ" /><figcaption>Photo by <a href="https://unsplash.com/@emilianovittoriosi?utm_source=medium&amp;utm_medium=referral">Emiliano Vittoriosi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Setup Huggingface Account (+token)</h3><p>For god sake, create your huggingface account first, if you’ve already got one, then you may skip this part, if you dont, then go ahead and create a new account. Note also that we’re going to generate an authentication token for our Huggingface account so that we may use the pre-trained model available on this giant repo.</p><p>After you create your account, go ahead and click “profile” on the top-right corner, then go to &gt; <em>setting</em>. One you got the the settings page, on the left sidebar, click &gt; <em>Access Tokens</em></p><p>Note: You cannot perform this action if you haven’t verifying your account, so go ahead and verify your account first via your gmail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KsrqRors--GpP5nVHhHgEA.png" /></figure><p>This is how the page should look like. Now click “New Token” and give “Read” access to it, (its very important) you may not like what’s coming if you forget this step LOL. Now, lets write some code =D.</p><h3>Setup Google Colab</h3><p>We will be writing the entire process to google colab. so there should be no environment conflict as i have prepared the entire notebook to be just working fine hehe.</p><p>Once you’ve created “New Notebook” , insert this code. This will connect your google colab to your Google Drive. its very important since we’re going to save the model trained to your drive. Just follow the step and you should be good to go</p><pre>from google.colab import drive<br>drive.mount(&#39;/content/gdrive&#39;)</pre><h3>Install Packages</h3><p>We will use some packages to be able to run and train our model. Insert this code, and please dont ask me to explain what each of this package do. (just google search for god sake).</p><pre>!pip install trl transformers accelerate git+https://github.com/huggingface/peft.git -Uqqq<br>!pip install datasets bitsandbytes einops wandb -Uqqq</pre><h3>Import Packages</h3><p>Once you’ve done lets import the packages using the code below, again dont ask me to explain each. If it works, dont touch it.</p><pre>import torch<br>from datasets import load_dataset<br>from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TrainingArguments, GenerationConfig<br>from peft import LoraConfig, get_peft_model, PeftConfig, PeftModel, prepare_model_for_kbit_training<br>from trl import SFTTrainer<br>import warnings<br>warnings.filterwarnings(&quot;ignore&quot;)</pre><h3>Huggingface Login</h3><p>Since we will be getting the model from Huggingface, just like when you’re about to enter someone else house to grab something (which you shouldnt have if you dont know the owner) then you need keys. In huggingface we called this a <em>token. </em>Remember the token we’ve created earlier in the first step? yeah, thats it. RIP to those who skip that part :v. Anyway, insert this code, and run.</p><pre>from huggingface_hub import notebook_login<br>notebook_login()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Py6OWFClPnFbsdzUjdaO_A.png" /></figure><p>This is what happen when you run the code. Enter your newly generated token (with Read access) and click Login. This is how the output looks like if you’ve successfully authenticated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*ohuRW7LyLIjdcv7l5P2NAQ.png" /></figure><p><strong>Additional Note:</strong> Once you’ve done, create a new Model Repo by clicking your profile again and click <em>+New Model. </em>You should name the model as : <strong>falcon-7b-sharded-bf16-finetuned-mental-health-conversational. </strong>Use the same model name for your repo with the fine-tuned model, later of course you may change it after you pushing your trained model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d1JiPzE81X3TNRU1GPCInA.png" /></figure><p>After that just click “Create model” on the bottom.</p><h3>Download Dataset</h3><p>We will be using the Mental Health Conversational dataset which consists a question-and-answer text, not really large, but you can always create your custom dataset that should match your goals. I’ve hosted the dataset on my Huggingface, so insert the code below to be able to use it.</p><pre>data = load_dataset(&quot;ZahrizhalAli/mental_health_conversational_dataset&quot;)<br>data<br><br># Take a Glance on how the data looks like <br>print(data[&quot;train&quot;][0][&#39;text&#39;])</pre><h3>Load the Pre-trained Model</h3><p>We may now load the pre-trained models directly from Huggingface and make a new instance and setup configuration. If you haven’t logged in to Huggingface with your token, this should not work. This may take a while since the code will download the Model checkpoint and its weights and it also depends on how fast your internet is,</p><pre>model_name = &quot;ybelkada/falcon-7b-sharded-bf16&quot; # sharded falcon-7b model <br><br>bnb_config = BitsAndBytesConfig(<br>    load_in_4bit=True,            # load model in 4-bit precision<br>    bnb_4bit_quant_type=&quot;nf4&quot;,    # pre-trained model should be quantized in 4-bit NF format<br>    bnb_4bit_use_double_quant=True, # Using double quantization as mentioned in QLoRA paper<br>    bnb_4bit_compute_dtype=torch.bfloat16, # During computation, pre-trained model should be loaded in BF16 format<br>)<br><br>model = AutoModelForCausalLM.from_pretrained(<br>    model_name,<br>    quantization_config=bnb_config, # Use bitsandbytes config<br>    device_map=&quot;auto&quot;,  # Specifying device_map=&quot;auto&quot; so that HF Accelerate will determine which GPU to put each layer of the model on<br>    trust_remote_code=True, # Set trust_remote_code=True to use falcon-7b model with custom code<br>)</pre><p>In the model_name variable, you may change the path to LLaMa-7b HuggingFace repo, you can literally pick any model you want, just make sure you pick the right one for your use-case. But note that, to be able to use LLaMa, you must make an access request to Meta, I don’t think that’s very hard to do, just visit their repo and there will be an instruction on how you can make a request, just make sure you’re using a verfied email and the same email with your HuggingFace and the one that you’re going to submit to Meta for an access request.</p><p>After that, we can now load the Tokenizer from the model which will decode our text to a numerical representation.</p><pre>tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) # Set trust_remote_code=True<br>tokenizer.pad_token = tokenizer.eos_token # Setting pad_token same as eos_token</pre><p>Next, we will setup PEFT (Parameter Efficient Fine Tuning) and the QLoRA (Low-Rank Adaptation of Quantized LLMs by Tim Dettmers et al.) technique. This is where we will define the configuration for our fine-tuned model so that we can compare it later to the performance of Falcon-7b model.</p><pre>model = prepare_model_for_kbit_training(model)<br><br>lora_alpha = 32 # scaling factor for the weight matrices<br>lora_dropout = 0.05 # dropout probability of the LoRA layers<br>lora_rank = 32 # dimension of the low-rank matrices<br><br>peft_config = LoraConfig(<br>    lora_alpha=lora_alpha,<br>    lora_dropout=lora_dropout,<br>    r=lora_rank,<br>    bias=&quot;none&quot;,  # setting to &#39;none&#39; for only training weight params instead of biases<br>    task_type=&quot;CAUSAL_LM&quot;,<br>    target_modules=[         # Setting names of modules in falcon-7b model that we want to apply LoRA to<br>        &quot;query_key_value&quot;,<br>        &quot;dense&quot;,<br>        &quot;dense_h_to_4h&quot;,<br>        &quot;dense_4h_to_h&quot;,<br>    ]<br>)<br><br>peft_model = get_peft_model(model, peft_config)</pre><p>Now we can create our trainer configurations, you should change the output dir to your own directory on where you may want to save your model later. Also change the <em>per_device_train_batch_size</em> to some higher batch like 16 or 32 for better results, but note that you must have a higher Memory to be able to train it if you set it to higher, 2 should be work just fine for Free-tier google colab package. Hehe.</p><p><strong>Note: For LLaMa model, you have to remove <em>target_modules</em> parameter from <em>LoraConfig.</em></strong></p><pre>output_dir = &quot;/content/gdrive/MyDrive/LLM/falcon-7b-sharded-bf16-finetuned-mental-health-conversational&quot;<br>per_device_train_batch_size = 2 # reduce batch size by 2x if out-of-memory error<br>gradient_accumulation_steps = 2  # increase gradient accumulation steps by 2x if batch size is reduced<br>optim = &quot;paged_adamw_32bit&quot; # activates the paging for better memory management<br>save_strategy=&quot;steps&quot; # checkpoint save strategy to adopt during training<br>save_steps = 10 # number of updates steps before two checkpoint saves<br>logging_steps = 10  # number of update steps between two logs if logging_strategy=&quot;steps&quot;<br>learning_rate = 2e-4  # learning rate for AdamW optimizer<br>max_grad_norm = 0.3 # maximum gradient norm (for gradient clipping)<br>max_steps = 320        # training will happen for 320 steps<br>warmup_ratio = 0.03 # number of steps used for a linear warmup from 0 to learning_rate<br>lr_scheduler_type = &quot;cosine&quot;  # learning rate scheduler<br><br>training_arguments = TrainingArguments(<br>    output_dir=output_dir,<br>    per_device_train_batch_size=per_device_train_batch_size,<br>    gradient_accumulation_steps=gradient_accumulation_steps,<br>    optim=optim,<br>    save_steps=save_steps,<br>    logging_steps=logging_steps,<br>    learning_rate=learning_rate,<br>    bf16=False,<br>    max_grad_norm=max_grad_norm,<br>    max_steps=max_steps,<br>    warmup_ratio=warmup_ratio,<br>    group_by_length=True,<br>    lr_scheduler_type=lr_scheduler_type,<br>    push_to_hub=True,<br>    tf32=False,<br>)</pre><p>As we’ve stated in the previus sub sections, Here, you may get an Error that says Repository does not exist, so if you found that error, go ahead and create a new Repo by using the name that the error shows you that doesn’t exist. Once you’ve done create your repo, you may try execute the code again or start from beginning</p><p>Now lets instantiate our trainer, and load the data as well</p><pre>trainer = SFTTrainer(<br>    model=peft_model,<br>    train_dataset=data[&#39;train&#39;],<br>    peft_config=peft_config,<br>    dataset_text_field=&quot;text&quot;,<br>    max_seq_length=1024,<br>    tokenizer=tokenizer,<br>    args=training_arguments,<br>)<br><br>for name, module in trainer.model.named_modules():<br>    if &quot;norm&quot; in name:<br>        module = module.to(torch.float32)</pre><h3>Begin Training</h3><p>The entire finetuning process should took less than an hour, and btw, once you run this, it will ask you to insert wandb token, just follow the instruction and create a new <a href="https://wandb.ai/site">wandb.ai </a>account to get your token, you may now use the token to train the model just by inserting it to the input box (this will show after you run the code below).</p><pre>peft_model.config.use_cache = False<br>trainer.train()</pre><p>Once wandb token inserted, it will start training, now we wait. kinda, boring, i know. But hey, after training, the entire model will be saved to your output dir in your google drive,</p><p>You can now push your trained model to your newly created Huggingface Model Repo we’ve created earlier. using the code below:</p><pre>trainer.push_to_hub()</pre><p>This will take almost forever for me, so i did it manually by downloading the models, git clone my repo, and begin manual Push using Git :(. If this happens to you also, do let me know, i know the trick.</p><h3>Inference Model</h3><p>You may want to use your model of course since that’s what the model were made to right?, now lets load the falcon-7b model using the code below:</p><pre># Loading original model<br>model_name = &quot;ybelkada/falcon-7b-sharded-bf16&quot;<br><br>bnb_config = BitsAndBytesConfig(<br>    load_in_4bit=True,<br>    bnb_4bit_quant_type=&quot;nf4&quot;,<br>    bnb_4bit_use_double_quant=True,<br>    bnb_4bit_compute_dtype=torch.float16,<br>)<br><br>model = AutoModelForCausalLM.from_pretrained(<br>    model_name,<br>    quantization_config=bnb_config,<br>    device_map=&quot;auto&quot;,<br>    trust_remote_code=True,<br>)<br><br>tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)<br>tokenizer.pad_token = tokenizer.eos_token</pre><p>And also load our model which should now available in our Model Repo</p><pre># Loading PEFT model<br>PEFT_MODEL = &quot;ZahrizhalAli/calgpt-falcon-7b-finetuned-mental-health&quot;<br># PEFT_MODEL = &lt;Username&gt;/YOUR_MODEL_URL_REPO. <br><br>config = PeftConfig.from_pretrained(PEFT_MODEL)<br>peft_base_model = AutoModelForCausalLM.from_pretrained(<br>    config.base_model_name_or_path,<br>    return_dict=True,<br>    quantization_config=bnb_config,<br>    device_map=&quot;auto&quot;,<br>    trust_remote_code=True,<br>)<br><br>peft_model = PeftModel.from_pretrained(peft_base_model, PEFT_MODEL)<br><br>peft_tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)<br>peft_tokenizer.pad_token = peft_tokenizer.eos_token</pre><p>Now using the function below, we can begin test our model</p><pre># Function to generate responses from both original model and PEFT model and compare their answers.<br>def generate_answer(query):<br>  system_prompt = &quot;&quot;&quot;Answer the following question truthfully.<br>  If you don&#39;t know the answer, respond &#39;Sorry, I don&#39;t know the answer to this question.&#39;.<br>  If the question is too complex, respond &#39;Kindly, consult a psychiatrist for further queries.&#39;.&quot;&quot;&quot;<br><br>  user_prompt = f&quot;&quot;&quot;: {query}<br>  : &quot;&quot;&quot;<br><br>  final_prompt = system_prompt + &quot;\n&quot; + user_prompt<br><br>  device = &quot;cuda:0&quot;<br>  dashline = &quot;-&quot;.join(&quot;&quot; for i in range(50))<br><br>  encoding = tokenizer(final_prompt, return_tensors=&quot;pt&quot;).to(device)<br>  outputs = model.generate(input_ids=encoding.input_ids, generation_config=GenerationConfig(max_new_tokens=256, pad_token_id = tokenizer.eos_token_id, \<br>                                                                                                                     eos_token_id = tokenizer.eos_token_id, attention_mask = encoding.attention_mask, \<br>                                                                                                                     temperature=0.4, top_p=0.6, repetition_penalty=1.3, num_return_sequences=1,))<br>  text_output = tokenizer.decode(outputs[0], skip_special_tokens=True)<br><br>  print(dashline)<br>  print(f&#39;ORIGINAL MODEL RESPONSE:\n{text_output}&#39;)<br>  print(dashline)<br><br>  peft_encoding = peft_tokenizer(final_prompt, return_tensors=&quot;pt&quot;).to(device)<br>  peft_outputs = peft_model.generate(input_ids=peft_encoding.input_ids, generation_config=GenerationConfig(max_new_tokens=256, pad_token_id = peft_tokenizer.eos_token_id, \<br>                                                                                                                     eos_token_id = peft_tokenizer.eos_token_id, attention_mask = peft_encoding.attention_mask, \<br>                                                                                                                     temperature=0.4, top_p=0.6, repetition_penalty=1.3, num_return_sequences=1,))<br>  peft_text_output = peft_tokenizer.decode(peft_outputs[0], skip_special_tokens=True)<br><br>  print(f&#39;PEFT MODEL RESPONSE:\n{peft_text_output}&#39;)<br>  print(dashline)</pre><p>Now begin prompt something:</p><pre>generate_answer(&#39;What to do if you have mental illnes?&#39;)</pre><p>This is how the Falcon-7b responded:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VeYcZM0lO-Ba6JO6799Q3g.png" /><figcaption>Hallucinate</figcaption></figure><p>And this is how our model responded 😄:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*061eYS2CeZV-FJW5d20plw.png" /><figcaption>Better 👌</figcaption></figure><p>The entire code can be accessible through my GitHub Repo:</p><p>GitHub: <a href="https://github.com/ZahrizhalAli/calGPT/tree/master">https://github.com/ZahrizhalAli/calGPT/tree/master</a></p><p>Thanks. (I will make some changes to add more explanation).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1df8c473f56" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Crafting Your Custom Text-to-Speech Model.]]></title>
            <link>https://medium.com/@zahrizhalali/crafting-your-custom-text-to-speech-model-f151b0c9cba2?source=rss-c5565aa21575------2</link>
            <guid isPermaLink="false">https://medium.com/p/f151b0c9cba2</guid>
            <category><![CDATA[voice-cloning]]></category>
            <category><![CDATA[text-to-speech]]></category>
            <dc:creator><![CDATA[Zahrizhal Ali (Cal)]]></dc:creator>
            <pubDate>Fri, 02 Jun 2023 16:18:13 GMT</pubDate>
            <atom:updated>2023-06-02T16:18:13.053Z</atom:updated>
            <content:encoded><![CDATA[<p>Welcome to a wild journey where code and comedy collide! In this blog, we’re going to embark on a whimsical quest to build our very own custom model for text-to-speech. Buckle up and get ready to dive headfirst into a world where pixels transform into melodic magic, and words on a screen come alive with the power of voice! Your voice!. Or someone else?!</p><p>Please DO NOTE that this should be build upon ethical concern so please don’t used this project for unethical purpose. Alright, without any further a do, let’s get started.</p><p>Now, you might be wondering, “Why settle for run-of-the-mill text-to-speech when you can infuse it with your own brand of humor?” That’s right, my friends! We’re here to revolutionize the way we hear technology. Picture this: a speech synthesis so brilliantly crafted that it delivers punchlines with perfect comedic timing, leaving you in stitches of laughter without anybody else realize that it’s not you whose talking. It’s a code!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YzU6Mr8HtNd1gVdQ" /><figcaption>Photo by <a href="https://unsplash.com/@lucabravo?utm_source=medium&amp;utm_medium=referral">Luca Bravo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Brief Intro of Text-to-speech</h3><p>Text-to-speech (TTS) is a fascinating technology that converts written text into spoken words. It allows computers and devices to audibly communicate with users, transforming written content into natural-sounding speech. With TTS, the possibilities are endless: from enhancing accessibility for visually impaired individuals to creating engaging voice-overs for multimedia content. By utilizing advanced algorithms and linguistic models, TTS systems analyze text input, apply speech synthesis techniques, and generate audio output that mimics human speech patterns, intonation, and emotions. As the boundaries of TTS continue to expand, this captivating technology opens up a world where written words come alive, bridging the gap between text-based communication and immersive auditory experiences.</p><p>There are lots of Third-party libraries that provides some magical recipes for crafting your TTS model. But at this project, we will implement the model from <a href="https://github.com/coqui-ai/TTS">Coqui-TTS</a>. Coqui TTS is an exciting open-source text-to-speech synthesis system that empowers developers and enthusiasts to create their own custom speech models. With Coqui TTS, you have the flexibility to train your own models using your own data, enabling you to personalize and fine-tune the speech synthesis according to your unique requirements. Harnessing the power of deep learning and neural networks, Coqui TTS offers a user-friendly platform for generating high-quality, natural-sounding speech that can be integrated into various applications and projects.</p><h3>Let’s Code.</h3><p>I’m trying my best to explain step-by-step and trying to make the code reproducable. Hehe. So Bear with me.</p><h4>Preparing Dataset</h4><p>No Data. No Models. Yes, we’re all familiar with that thing in the field of ML/AI. According to the Coqui-tts documentation the models we’re going to implement works with several dataset such as VCTK, LJSpeech, Libro. There are lots of open dataset out there. You can use and download above dataset or create your own. But later, note that when we’re going to fit our dataset to the model it should be in a format that the model understands, in this case the easiest format is LJSpeech. Since we’re not using LJSpeech dataset we will generate our own dataset and our own format.</p><p>First, you must prepare your audio dataset, and split it into multiple audio files. These files should be in a range 5–10 seconds (works the best). If you want to split 1 audio dataset, you can use tools like Audacity. You can find the tutorial on YouTube, there’s lots of them. Make sure the audio format is in <strong>.wav </strong>format and it should be in mono stereo. You can use Audacity to achieve all of this easily. You should expect to gather 1–2 Hours audio. I’ve once tried ~40-50 Min but still works best. <strong>Most important step </strong>is to transcribe these audio dataset. You can use Google Speech-to-text and loop through your dataset and save them inside <strong>transcript.txt</strong> format. Your transcript format should looks like this:</p><pre>wav1|I have come into my conclusion that he is evil<br>wav2|The more we read the more we can gain knowledge<br>wav3|Good morning<br>...</pre><p>Above, <em>wav1 </em>etc is your audio file name <strong>make sure to set them properly. </strong>Each of this audio should has their correct transcript, otherwise you’ll produce poorly trained model. Now it is time to arrange your dataset into a folder. Your folder structure should look like these:</p><pre>MyTTSDataset/<br>-transcript.txt<br>-wavs/<br>--------&gt;wav1.wav<br>--------&gt;wav2.wav<br>--------&gt;...</pre><p>Pretty easy right ;).</p><h4>Preparing Google Colab</h4><p>Go ahead and Visit <a href="https://colab.research.google.com/">Google Colab</a>. This is where we are going to write our Code. You may of course use your own local environment like PyCharm, Anaconda, or VSCode. But you will have to perform extra steps to install the packages we’re using here. I will put the link down below for that. Don’t worry amigo. <em>If you do, then you can skip this part</em>. Anyway, Go ahead and click <strong>New Notebook</strong>.</p><ol><li>Install Packages</li></ol><p>What this code does, is installing the packages that we will need for the project:</p><pre>!pip install -U pip<br>!pip install TTS</pre><p>2. Connect to Google Drive</p><p>We dont wanna use the temp folder because it will be reset when the runtime is over. So connect to your google drive using the code below. Basically this is how we’re also going to access our dataset and save our models for future purposes.</p><pre>from google.colab import drive<br>drive.mount(&#39;/content/gdrive&#39;)</pre><p>After you run the code above, something like this will popped up:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*Yyi-wkpnBuJy1sDshEpiqA.png" /></figure><p>Just go ahead and click ‘Connect to Google Drive’, then follow the instructions. It should be easy, like come on?!.</p><p>When we’re done setting up google drive. It should now look like this on the sidebar of your screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/1*-jpqcI0K6Be6_EuGHAeIkA.png" /><figcaption>Oh god it works.</figcaption></figure><h4>Import Packages</h4><p>If you’re new into programming dont bother yourself understand the whole code. Just follow along, Bear with me. This is basically where we’re going to import packages (<em>or say other’s dude code) </em>to be used into our code. We dont wanna write the entire code of course. Remember: <em>never re-invent the wheel.</em> Here’s the code:</p><pre>import os<br><br>from trainer import Trainer, TrainerArgs<br><br>from TTS.tts.configs.shared_configs import BaseDatasetConfig, CharactersConfig<br>from TTS.tts.configs.vits_config import VitsConfig<br>from TTS.tts.datasets import load_tts_samples<br>from TTS.tts.models.vits import Vits, VitsArgs, VitsAudioConfig<br>from TTS.tts.utils.speakers import SpeakerManager<br>from TTS.tts.utils.text.tokenizer import TTSTokenizer<br>from TTS.utils.audio import AudioProcessor</pre><h4>Dataset Configuration</h4><p>The next thing we’re going to do is to set up our dataset configuration. This configuration will then be used to train our model.</p><pre>output_path = &quot;/content/gdrive/MyDrive/tts&quot;<br><br><br>dataset_config = BaseDatasetConfig(<br>    formatter=&quot;ljspeech&quot;, meta_file_train=&quot;transcript.txt&quot;, path=os.path.join(output_path, &quot;NewTTSDataset/&quot;)<br>)</pre><p>The output_path variable holds our dataset path. Remember when we’re conneting our colab to our drive? Yes this is where we can access our dataset. In my case, my dataset is inside the content/gdrive/MyDrive/tts/ Folder. so inside the tts folder in my google drive , it consists the folder structure like this:</p><pre>/tts/<br>----&gt;MyTTSDataset/<br>----------&gt;transcript.txt<br>----------&gt;wavs/<br></pre><p>I hope its clear. the metadata parameter holds our ‘transcript.txt’ file. This is important since that’s where our model learn the text and the corresponding audio inside the wavs folder where we put our audio dataset.</p><h4>Audio Config</h4><p>This config setup how our audio dataset will be augmented before while we’re training our model.</p><pre>audio_config = VitsAudioConfig(<br>    sample_rate=44100, win_length=1024, hop_length=256, num_mels=80, mel_fmin=0, mel_fmax=None<br>)</pre><p>The sample_rate is important because every set of audio have different sample rate. In my case, it is 44100. If you use Audacity to split your audio like i mentioned earlier, then you should be safe using this value as well. I dont know exactly what the rest of the params does, :v. You can get more detail in the Coqui TTS documentation. I’ll put the link down below.</p><h4>Set up Characters Configuration</h4><p>This is where we setup all the characters for our model to learn from our transcript. Note: <em>This is only applied to languages that uses these characters. If your language has a set of unique characters you need to add them here as well, I believe there are also lots of other tutorial out there to show you how to apply those step.</em></p><pre>character_config = CharactersConfig(<br>    characters_class= &quot;TTS.tts.models.vits.VitsCharacters&quot;,<br>    characters= &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890&quot;,<br>    punctuations=&quot; !,.?-&quot;,<br>    pad= &quot;&lt;PAD&gt;&quot;,<br>    eos= &quot;&lt;EOS&gt;&quot;,<br>    bos= &quot;&lt;BOS&gt;&quot;,<br>    blank= &quot;&lt;BLNK&gt;&quot;,<br>)</pre><h4>Model Configuration</h4><p>Now this is where we setup our Model. All the previous config we did will be inherited to this model. Note that there are certain models you can try from Coqui TTS or other third party libraries. In this project, I’m focusing on VITS. Because Vits already pre-configured with Vocoder. While other model such as GlowTTS does not pre-include with Vocoder. I know these terms might not sounds familiar but you can see more detail from their documentation. :p</p><pre>config = VitsConfig(<br>    audio=audio_config,<br>    characters=character_config,<br>    run_name=&quot;vits_vctk&quot;,<br>    batch_size=16,<br>    eval_batch_size=4,<br>    num_loader_workers=4,<br>    num_eval_loader_workers=4,<br>    run_eval=True,<br>    test_delay_epochs=0,<br>    epochs=1000,<br>    text_cleaner=&quot;basic_cleaners&quot;,<br>    use_phonemes=False,<br>    phoneme_language=&quot;en-us&quot;,<br>    phoneme_cache_path=os.path.join(output_path, &quot;phoneme_cache&quot;),<br>    compute_input_seq_cache=True,<br>    print_step=25,<br>    print_eval=False,<br>    save_best_after=1000,<br>    save_checkpoints=True,<br>    save_all_best=True,<br>    mixed_precision=True,<br>    max_text_len=250,  # change this if you have a larger VRAM than 16GB<br>    output_path=output_path,<br>    datasets=[dataset_config],<br>    cudnn_benchmark=False,<br>    test_sentences=[<br>        [&quot;Ini adalah tes suara&quot;],<br>        [&quot;Beberapa hal yang perlu diperhatikan ketika melatih sebuah model adalah parameter.&quot;],<br>        [&quot;Selamat pagi&quot;]<br>    ]<br>)</pre><p>Alright, i’m definitely wont explain every single parameter here. But i will explain some of the most important params.</p><ul><li>epochs Training iteration. Each training will product <em>dataset_amount / batch_size </em>steps.</li><li>batch_size How many set of audio will be trained per step. If you have higher RAM available , then you should set this up to 32 or 64. In google colab, 16 is safe (especially if you’re using the shared version / free).</li><li>eval_batch_size How many set of audio will be evaluated after training per step. This is also affecting your RAM, so set to higher values if you use more RAM.</li><li>test_sentences Customize this based on your languages. In this case i’m using Indonesian language. Make sure to follow the above syntax.</li></ul><h4>Initialize Tokenizer</h4><p>Here we will set up the next configuration before start training our model. Our model is not ready yet, we need to add some additional pipeline. Audioprocessor basically used for feature extraction for our audio, mainly serves to the data loader and the training log. Meanwhile, the Tokenizer is used to convert our text to sequences of token IDs. Remember, our model cannot understand text, so we transform our text into a numerical representation. Dont worry if you dont get it. If you know a little bit of NLP (Natural Language Processing) you should be familiar with this.</p><pre># Audio processor is used for feature extraction and audio I/O.<br># It mainly serves to the dataloader and the training loggers.<br>ap = AudioProcessor.init_from_config(config)<br><br># INITIALIZE THE TOKENIZER<br># Tokenizer is used to convert text to sequences of token IDs.<br># config is updated with the default characters if not defined in the config.<br>tokenizer, config = TTSTokenizer.init_from_config(config)</pre><p>At this stage, we haven’t done yet. Let’s write our own Formatter function</p><pre><br>def formatter(root_path, manifest_file, **kwargs):  # pylint: disable=unused-argument<br>    &quot;&quot;&quot;Assumes each line as ```&lt;filename&gt;|&lt;transcription&gt;```<br>    &quot;&quot;&quot;<br>    txt_file = os.path.join(root_path, manifest_file)<br>    items = []<br>    speaker_name = &quot;my_speaker&quot;<br>    with open(txt_file, &quot;r&quot;, encoding=&quot;utf-8&quot;) as ttf:<br>        for line in ttf:<br>            cols = line.split(&quot;|&quot;)<br>            wav_file = f&quot;/content/gdrive/MyDrive/tts/MyTTSDataset/wavs/{cols[0]}.wav&quot;<br>            text = cols[1]<br>            # print(text)<br>            items.append({&quot;text&quot;:text, &quot;audio_file&quot;:wav_file, &quot;speaker_name&quot;:speaker_name, &quot;root_path&quot;: root_path})<br>    return items</pre><p>Since we have generated our own transcript, we will then use our own formatter for translating that script into dictionary that our model will understand. If you used open dataset such as LJSpeech or VCTK, you don’t have to do this steps. But here i’m just demonstrating that we can actually generate our own formatter.</p><p>Now we will set our formatter into this code:</p><pre>train_samples, eval_samples = load_tts_samples(<br>dataset_config, <br>eval_split=True, <br>formatter=formatter)</pre><p>This is where we split our training and test set for the model. And now finally, we’re ready to train our models :).</p><p>First, initialize the model along with the Trainer we have imported earlier:</p><pre># init model<br>model = Vits(config, ap, tokenizer, speaker_manager=None)<br><br># init the trainer and 🚀<br>trainer = Trainer(<br>    TrainerArgs(),<br>    config,<br>    output_path,<br>    model=model,<br>    train_samples=train_samples,<br>    eval_samples=eval_samples,<br>)</pre><p>If it is successfull, we will look an output like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qz57TOlC_tz2rzSuhCOYtw.png" /></figure><p>Here we can see that our models will be saved to /content/gdrive/tts/ where the model will generate their own folder named vits_vctk... you can also see it defined the time when we train our model.</p><p>And finally, the magical line comes:</p><pre>trainer.fit()</pre><p>Once you run this code, the output will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/899/1*w8wvAEvMGVD3mmWIpMj7OQ.png" /></figure><p>This is where we shall wait until it finished training. Remember though, that colab has certain limit to their service, you can only use the GPU for like 4–5 hours, once this time out, then our colab will stopped training (and so is our model)…sad.. but dont worry, I got you.</p><p>If you faced this issue, you can follow the steps below to continue train your model:</p><p>First, clone the CoquiTTS Repository using the code down below:</p><pre>!git clone https://github.com/coqui-ai/TTS.git</pre><p>Once done you should see the TTS folder in the left side along with our drive folder.</p><p>Remember when we use our own formatter? Well, unfortunately we have to do extra steps for that so that TTS understands our formatter. We need to set up this manually inside the folder. Here’s how,</p><ul><li>Go ahead and open the file train_tts.py inside this folder: TTS/TTS/bin/train_tts.py double click, and now you can edit the scripts.</li><li>Right below the TrainTTSArgs copy and paste your our previous formatter function</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*tPSlcfGzjg_m-4ppobbHLQ.png" /></figure><p>And next, set our formatter function to the train test split inside that script as well. You can find it if you scroll a little bit more below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*fpZT-KLtuE_XaqugGFN-EA.png" /></figure><p>set the formatter, and now the code should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*OlC1ydAX8vS0QzhVbcNHJw.png" /></figure><p>Thats it. we’re all set up. And now we’re ready to continue training our models which was interrupted earlier using the code below:</p><pre>!python /content/TTS/TTS/bin/train_tts.py --continue_path /content/gdrive/MyDrive/tts/vits_vctk-May-06-2023_11+41AM-0000000</pre><p>See that after --continue_path we set our model directory inside our google drive. Thats how the code knows which models we should be continue training.</p><h4>Begin Inference</h4><p>Now let’s take a look at inside our model folder after we had been finished training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Dl3ex5kCmS7k3L_PChY5g.png" /></figure><p>that’s how the saved model directory should look like after you train your model. you can use the best_model.pth to start making some inference or to produce a voice of a person you have in your dataset based on your input text. Cool right?. Here’s an example of voice cloning of a model trained with Deddy Corbuzier’s <a href="https://drive.google.com/file/d/1hnmYAhm9NQjUUYIha0xYxwgxqcUCTclg/view?usp=share_link">voice</a>. 👌.</p><p>Important Links:</p><p><a href="https://github.com/coqui-ai/TTS/tree/dev#install-tts">How to Install Coqui TTS in your Local Env</a></p><p><a href="https://tts.readthedocs.io/en/latest/index.html">Coqui TTS documentation</a></p><p><a href="https://www.youtube.com/watch?v=jE-lKBKfxJw">Training Hindi Language Voice Cloning Vits</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f151b0c9cba2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Common Text and Categorical data problems]]></title>
            <link>https://medium.com/@zahrizhalali/common-text-and-categorical-data-problems-d033c4a47431?source=rss-c5565aa21575------2</link>
            <guid isPermaLink="false">https://medium.com/p/d033c4a47431</guid>
            <category><![CDATA[feature-engineering]]></category>
            <category><![CDATA[data-cleaning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Zahrizhal Ali (Cal)]]></dc:creator>
            <pubDate>Fri, 10 Feb 2023 12:36:07 GMT</pubDate>
            <atom:updated>2023-02-10T12:36:07.359Z</atom:updated>
            <content:encoded><![CDATA[<p>Categorical and text data can often be some of the messiests parts of a dataset due to their unstructured and inconsistent nature. Let’s see some of the problem with Categorical data and how to fix them:</p><h3>Membership Contraints</h3><p>Membership contraints problem happens when we found inconsistent category in our data column. For example , suppose we have data for airlines dataset and in cleanliness column we have values such as <em>somewhat clean, average, somewhat dirty, dirty</em>, <em>clean</em>, <em>very clean. </em>To check whether or not we have uncommon values in our categorical data. we can utilize .unique pandas method to our data frame.</p><pre>print(airlines[&#39;cleanliness&#39;].unique())<br><br># Output:  [&#39;Clean&#39;, &#39;Average&#39;, &#39;Unacceptable&#39;, &#39;Somewhat clean&#39;, &#39;Somewhat dirty&#39;, &#39;Dirty&#39;]</pre><p>Notice what’s wrong here? We have <em>unacceptable </em>value in our cleanliness dataset column. How do we approach to this problem?</p><p>We can use Anti join. Suppose we have 2 dataframe A and B. When we’re using anti join, we basically said, Return Dataframe A that is not on the Dataframe B. From this analogy, we can put our initial categories in dataframe B and our dirty data in dataframe A. When we perform this operations, it should return all inconsistent values, that is not on the dataframe B. To do this in Python its pretty simple:</p><pre># Find the cleanliness category in airlines not in categories<br>cat_clean = set(airlines[&#39;cleanliness&#39;]).difference(categories[&#39;cleanliness&#39;])<br><br># Find rows with that category<br>cat_clean_rows = airlines[&#39;cleanliness&#39;].isin(cat_clean)<br><br># Print rows with inconsistent category<br>print(airlines[cat_clean_rows]) # This will return all inconsistent data<br><br># Note: If you want to return only the consistent data use `~`<br>print(airlines[~cat_clean_rows]) # Return all consistent data only</pre><h3>Categorical Variables</h3><p>In categorical data, one of the most common problem is value inconsistency.</p><p>For example, if our airline[&#39;destination&#39;] column has values such: <em>europe, EUROPE, east us, EAST US, east asia. </em>This clearly shows inconsistency and could lead to a problem where each categories are mapped based on their pre-defined category. We could fix this in 2 ways:</p><h4>Lowercase/Uppercase method</h4><pre>airline[&#39;destination&#39;] = airline[&#39;destination&#39;].str.lower() # or .upper()<br><br>#Output : europe, east us, east asia</pre><p>Other cases is that our column might look like this : [&#39; europe&#39;, &#39;europe&#39;, &#39;east us&#39;,&#39; east us&#39;]. This is also a problem since it will treated as different category from another. To fix this we could utilize .strip() method.</p><h4>Strip</h4><pre>airline[&#39;destination&#39;] = airline[&#39;destination&#39;].str.strip()<br># output: europe, east us<br><br></pre><p>Or,</p><h3>Make our Categories falls into range</h3><p>Sometimes, we want to narrow our categories to certain ranges of category and labels each of the range. For example, data[&#39;income&#39;] specify the total income of every sample in a city. Some people might have income $100.000, $15.000, $150.000, or $500.000. To map our data and categories into certain range of the income we can use pd.cut() method from pandas.</p><pre>labels_range = [0,15000,100000,150000,500000,np.inf]<br>labels_names = [&quot;0-15000&quot;, &quot;15000-100000&quot;, &quot;150000-500000&quot;, &quot;500000+&quot;]<br><br># Create a new column<br>data[&#39;income_type&#39;] = pd.cut(data[&#39;income&#39;], bins=labels_range, <br>                      labels=labels_names])</pre><p>I hope you find it useful. Thank You.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d033c4a47431" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Summary Statistics 101.]]></title>
            <link>https://medium.com/@zahrizhalali/the-summary-statistics-101-b15465cdd0ef?source=rss-c5565aa21575------2</link>
            <guid isPermaLink="false">https://medium.com/p/b15465cdd0ef</guid>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[summary-statistics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Zahrizhal Ali (Cal)]]></dc:creator>
            <pubDate>Tue, 31 Jan 2023 17:39:19 GMT</pubDate>
            <atom:updated>2023-02-13T05:23:29.419Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This blog will be one of my sequences and also the first part of my “statistics for machine learning” blog</em>.</p><p>What is the difference between Machine Learning and Statistics?</p><p>In general, here’s why we should consider Statistics:</p><ul><li>We want to test our hypothesis using data</li><li>Where we think that <em>Outlier </em>is important</li><li>When we are Focusing on data collection and data analysis</li><li>When We work with small datasets</li></ul><p>Example of using Statistics is to answer “<strong>How likely is someone to purchase a product? Are people more likely to purchase it if they can use a different payment system?</strong>”</p><p>And we use Machine Learning because:</p><ul><li>We want to focus on prediction or classification</li><li>We have classes of algorithms that is flexible to handle complex data</li><li>We work with larger datasets</li></ul><p>Case where we use machine learning is to answer “<strong>We have this Boston housing price datasets, use this data as an input to predict the housing price!</strong>”. Hope this analogue is easy to understand ;).</p><h3>What is Summary Statistics?</h3><p>Quoting from the course Datacamp: “Introduction to Statistics in Python” by <em>Maggie Matsui</em>. Summary statistics gives you the tools you need to boil down massive datasets to reveal the highlights. This can be very useful skills a data scientist should have, thus making it easy to interpret our dataset to take one better step forward for dealing with it using our powerful weapon: Machine Learning.</p><p>Here, I am going to summarize everything you should know about summary statistics. Lets take a baby step from now on.</p><h3>Types of Statistics</h3><ol><li><strong>Descriptive Statistics</strong></li></ol><p>Descriptive statistics aims to describe and summarize data. For Example in one of a column in our dataset we summarize that : <em>50% of friends drive to work.</em></p><p>Dataset is simply a collection of data which usually consists of multiple columns to represent each rows , where each rows is considered as 1 observations of data. For example , a dataset of <em>Housing price </em>have a column <em>: Price, Number of rooms, location, number of bathrooms, number of bedrooms. </em>These are the columns and now , each rows below will represent 1 observations corresponds to each of these column. In data science or ML, we usually called this <em>features or independent variables. </em>Meanwhile, the Price is usually called <em>labels </em>or <em>targets </em>or <em>dependent variable</em> since their value might depends on all <em>features </em>or some. I hope you understand. There are lots of great resources out there to understand this better.</p><p>2. <strong>Inferential Statistics</strong></p><p>To use sample of data to make inferences about a larger population. Honestly, this is very important especially if we were dealing with larger dataset. Example: <em>What percent of people drive to work?</em></p><p>Its important to note, that Statistics must be paired with data. Thus, for different data, we have different types of data to work with. Lets learn some of these types of data (Note: <em>There are many types of data, we will only talk about most used</em>).</p><h3>Types of data</h3><ol><li><strong>Numeric (Quantitative)</strong></li></ol><p>Its a data that is numeric. Yap.</p><ul><li><strong>Continuous (Measured). </strong>This type of data basically any continuous number such as <em>airplane speed, time spent waiting in a line.</em></li><li><strong>Discrete (Counted). </strong>This type of data is any number that cannot be fraction. For example: <em>Number of pets. </em>We can say there are 3 cats, but we cannot say there are 2.5 cats<em>.</em></li></ul><p>2. <strong>Categorical (Qualitative)</strong></p><p>This type of data is categorical though sometimes we can represent it as a number but does not constitute that its a numeric data. This fancy terms is used since Machine Learning works with numbers, not <em>text</em>. Type of Categorical data:</p><ul><li><strong>Nominal (Unordered). </strong>Example:<strong> </strong><em>Region in country. Student/Workers.</em> Nominal do not have interpretable degree of category. So it means that this category importance are the same.</li><li><strong>Ordinal (Ordered). </strong>Example: <em>Feedback</em>. Yap. Usually feedback are measured based on the importance of their chosen answer. For example, <em>bad, neutral, good, and very good. </em>Here, we have degree of importance since <em>very good</em> is definitely not relevant to <em>bad.</em></li></ul><p>This is extremely important to understand so that you know how you should transform your categorical data to numeric type.</p><h3>Why data type matter?</h3><p>If you want to understand your data to make insightful decision or better understanding, visualizing it is one way to do this. Visualizing means representing our data into visuals. We often use different visuals for different types of data. For example, it is not makes any sense to visualizing numeric data as bar plot since bar plot is good for representing categorical data. Imagine if we have lots of numbers and assigned lots of bar chart to represent that number. Exactly.</p><p>We will talk more about this later. Stay with me ;).</p><h3>Measures of Center</h3><p>Say for example we have the dataset of mammals and their sleep hours per day. To answer a question <em>What’s a typical hours of sleep? </em>or <em>Where is the center of the data</em>? We must know how to count the center, assuming that this can be representative for the <em>typical</em> type of questions.</p><h4>Mean</h4><p>We can measure the mean by summing all the sleep hours divided by the number of observations. Note that mean is the same with average. In order to find the mean in python, we can utilize data science library such as <em>numpy</em>.</p><pre>import numpy as np<br><br>np.mean([2,3,15,23,21,19]) # this will perform the calculation for us</pre><h4>Median</h4><p>Another type of measure of center is median. Median is the center of our list in one observations.</p><p>To find the median of a list, we can utilize another function fron <em>numpy</em></p><pre>import numpy as np<br><br>np.median([1.9,2.7,2.9,3.0,3.1])</pre><h4>Mode</h4><p>Mode is the most frequent value appears in a list of data. If you worked with dataframe or series, we can just use <em>.value_counts() </em>to find the most frequent data in a column.</p><pre>msleep[&#39;vore&#39;].value_counts()<br><br># output: <br>&#39;&#39;&#39;<br>herbi  32<br>omni   20<br>carni  19<br>&#39;&#39;&#39;<br># Here the mode is &#39;herbi&#39;<br></pre><h3>Which measure to use?</h3><p>Finding the right measure of center is important if we want to fill missing values in dataset, perform sampling, or to understand your data better. For example, if we are dealing with data that consists of salaries of individuals in a country, then finding the mean is not the correct technique. Since, there are some people who has like 10x salary then most people did. This is called an outlier. <em>Outlier </em>is simply an extreme data that is far different from the rest of the data we have. Thus, Mean is simply sensitive to outlier, since we could get far range of result if we have outlier or if we dont. The easiest way to know which type of measure to use is to know your data distribution. For example, if your data is skewed when you plotting it, then the best step is to use median. Here is an example of skewed data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*W4EowYN88M89U3PrZQPcQQ.png" /><figcaption>From Datacamp (Introduction to Statistics in Python)</figcaption></figure><p>Using <em>mean </em>is very appropriate if we are dealing with un-skewed (i dont know if i spell it correctly) distribution. This is called <em>Normal Distribution </em>type of data or in a form of symmetrical data Like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*iAxlA3_uBpnIN-M_T3Dc-Q.png" /><figcaption>Datacamp (Introduction to Statistics in Python)</figcaption></figure><h3>Measures of Spread</h3><p>The spread is simply describes how spread aparts the data points are in our dataset. There are different types of spread</p><h4>Variance</h4><p>Variance is a distance of each data point to the data’s mean/average.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/489/1*UcqhKiiD9Zm7QfzGjXd2NA.png" /><figcaption>Each data point’s distance is sum up and then squared, then divide by number of data points -1.</figcaption></figure><p>To calculate the variance using <em>python</em></p><ol><li>Substract mean from each data point</li></ol><pre>import numpy as np<br># returns list of distance<br>distance = df[&#39;sleep_total&#39;] - np.mean(df[&#39;sleep_total&#39;]) <br>print(distance)</pre><p>2. Square each distance</p><pre>...<br>dist_sq = distance ** 2<br>print(dist_sq)</pre><p>3. Add them all together then divide by number of data points-1</p><pre>...<br>sum_sq_dist = np.sum(dist_sq)<br>print(sum_sq_dist) <br><br>variance = sum_sq_dist / (80 - 1)<br>print(variance) # return single value of variance.</pre><p>The higher the variance, the higher the spread of the data is. Also, Note that the single variance data is squared. Thus, if our variance is 19.3, then our variance is 19.3².</p><h4>Standard Deviation</h4><p>Standard deviation is simple the square root of the variance. However, the single result is not squared anymore, making it easy to interpret.</p><p>To find the standard deviation:</p><pre>import numpy as np<br><br>np.sqrt(variance)<br><br># or<br><br>np.std(variance)</pre><h4>Mean Absolute Deviation (MAD)</h4><p>MAD is almost the same as standard deviation except that the distance is then count as mean. Note that, Standard deviation squared distances, thus penalizing longer distances more than shorter ones. Meanwhile, MAD penalizes each distance equally by taking its average value. There is no better than the other, but commonly, we use SD rather than MAD.</p><pre>import numpy as np<br>distance = df[&#39;sleep_total&#39;] - mean(df[&#39;sleep_total&#39;])<br><br>np.mean(np.abs(distance))</pre><p>Above, we use <strong>np.abs() </strong>to make all of our value in our distance is absolute or positive value. Thus they called it absolute deviation. Maybe.</p><h4>Interquartile Range (IQR)</h4><p>Before we understand what is IQR, we have to understand what is <em>quantile</em> and <em>quartile </em>is.</p><p><strong>Quantiles </strong>or often called <strong>Percentiles </strong>splits up our data into some number of equal parts. For example:</p><pre>np.quantile(df[&#39;sleep_total&#39;], 0.5) # output: 10.1</pre><p>Above line means 50% of our data is sleep less than 10.1 hours, while the rest sleep above 10.1 hours. 50% is called the <em>quartiles. </em>So, quantiles is splits up into the quartiles. We can also explicitly state the number of quartile we want:</p><pre>np.quantile(df[&#39;sleep_total&#39;, [0, 0.25, 0.5, 0.75, 1])<br><br># output: array([1.9, 7.85, 10.1, 13.75, 19.9])</pre><p>Here, we can see that 25% of our data is between 1.9 and 7.85. Another 25% is between 7.85 and 10.1. And so on. One way to visualize the IQR of a data is to visualize it using boxplot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*FujIjDwMSMC8l8wIDERM2A.png" /><figcaption>Source: Datacamp (Intro to statistics in python)</figcaption></figure><p>IQR means the Height of the box in a boxplot. Finding the IQR makes it easy for us to remove or identify the outliers because we can identify the point where certain data falls into that position.</p><p>Recall that <em>outlier </em>is a data point that is substantiatlly different from the other data points. It means that if our data falls below the specific quartile, that it means its an outlier.</p><p>To sum up, the outlier is specified if:</p><ul><li>data &lt; Q1–1.5 x IQR , or</li><li>data &gt; Q3+1.5 X IQR</li></ul><p>To find the outliers using <em>python:</em></p><pre>import numpy as np<br>from scipy.stats import iqr<br><br>iqr = iqr(df[&#39;bodywt&#39;])<br>min_threshold = np.quantile(df[&#39;bodywt&#39;], 0.25) - 1.5 * iqr<br>max_threshold = np.quantile(df[&#39;bodywt&#39;], 0.75) + 1.5 * iqr<br><br># subset our dataframe series<br>df_outlier = df[(df[&#39;bodywt&#39;] &lt; min_threshold) | (df[&#39;bodywt&#39;] &gt; max_threshold)]<br><br># return the outliers<br>print(df_outlier)</pre><p>If you are familiar with pandas library, we can actually know some important Summary Statistics of our dataset in one line below:</p><pre>df[&#39;bodywt&#39;].describe()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*Wdj59Et4Mze0j5xX5ra1ag.png" /><figcaption>Source: Datacamp (Intro to statistics in python)</figcaption></figure><p>Thats all. Thank you. Keep Learning!</p><p>Special credits to <strong>Datacamp course : Introduction to Statistics in Python </strong>by<strong> </strong><em>Maggie Matsui. </em>I would definitely recommend anyone reading this to take this course, they explain it very wonderful. Go ahead and check it out.</p><p>Link: <a href="https://www.datacamp.com/courses/introduction-to-statistics-in-python">https://www.datacamp.com/courses/introduction-to-statistics-in-python</a></p><p><em>See ya!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b15465cdd0ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Elliptic Envelope to find the outliers.]]></title>
            <link>https://medium.com/@zahrizhalali/how-to-find-the-outliers-da2d92585c88?source=rss-c5565aa21575------2</link>
            <guid isPermaLink="false">https://medium.com/p/da2d92585c88</guid>
            <category><![CDATA[scikit-learn]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-preprocessing]]></category>
            <category><![CDATA[outliers]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Zahrizhal Ali (Cal)]]></dc:creator>
            <pubDate>Wed, 12 Oct 2022 12:38:46 GMT</pubDate>
            <atom:updated>2023-06-02T16:53:31.586Z</atom:updated>
            <content:encoded><![CDATA[<p>Alright, you wanna build a machine learning models. You have your laptop sets up, your coffee, and even better the data. But here’s how things get scary. You’ve been told that the data might contain <em>outliers </em>or it is uncleaned. You can’t just fit the data into the model because most of the time as a machine learning practitioners, you would’ve spent like 60–70% of your time doing <em>data preprocessing. </em>What is data preprocessing?.</p><p>To the best of my knowledge as I’m trying to use as simple as possible words here is that, <em>data preprocessing</em> is basically a technique to get your data ready to fit into your machine learning model. Alright, that’s it. That should be enough. Well it is not. In fact, doing data preprocessing are actually kinda hard but easy (what?) things to do. Why? Well, obviously your machine learning performance really is depend on how well your data are being <em>preprocessed</em>.</p><p>Say for example, you want to build a model which can predict how much this houses prices given a data input. Thus, we called this data , a <em>features. </em>How well your model can predict the housing price is really depend on how many data your model has learned from. For example, your model knows that 3 bathrooms or 10 rooms might cost the house say like, $90,000 (arbitrary number). Now, that’s only one data, what if you have like 100 data?. Nah, my friend, in real world you would probably even ended up with 1 Million data for your machine learning models to learned from. Obviously, you cannot allow some ugly data to fit into your model because it will causing your model performed ugly too. Like, allowing a data for bathrooms using NaN value, or even worse a value that doesn’t makes any sense. For example, some of your bathrooms data has exactly 1000 bathrooms for a house. Who in this world would have built bathrooms that many?. The data that I am talking about here in, you can say <em>machine learning lingo </em>refers to as an<em> outliers. </em>To be honest with you, all the techniques that I’m gonna be showing you is coming from one of my favorite machine learning books with the title <strong>Python Machine Learning Cookbook </strong>written by <strong>Chris Albon</strong>. You can get it down below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*azTIrnzcwCiL3YIzo6bVwg.jpeg" /><figcaption>Link: <a href="https://www.amazon.com/Machine-Learning-Python-Cookbook-Preprocessing-ebook/dp/B07BC3LFKT">https://www.amazon.com/Machine-Learning-Python-Cookbook-Preprocessing-ebook/dp/B07BC3LFKT</a></figcaption></figure><p>Alright, enough talking, let’s get our hands dirty. For simplicity, I am not gonna be using a lot of data because I believe the data that we’re gonna be using is easy to understand (Actually not). So, Trust me (no).</p><pre>#1. Import libraries<br>import numpy as np<br>from sklearn.covariance import EllipticEnvelope<br>from sklearn.datasets import make_blobs</pre><pre>#2. Create a simulated data<br>features, _ = make_blobs(n_samples=10,n_features=2,centers=1,random_state=1)</pre><pre>#3. Replace one value from our features with some extreme numbers<br>features[0,0] = 10000</pre><p>The code above basically just created a features or you can call it a data. A random data to be exact. In step 3, We do also replace the first index value to some large numbers, so we created our own <em>outlier</em> here.</p><p>Notice also, what libraries we’re just imported. We will be using Numpy later for another technique. For now, let’s focus on other two. The EllipticEnvelope. Now assuming that our data is normally distributed, we will draw some ellipse around the data and labelled the data as 1 and an outlier as -1. That’s how we’re going to find the outlier given the previous features. the other one, make_blobs just a simple method from sklearn.datsets to generate some random samples of data, there are many types of them, you can find more here: <a href="https://scikit-learn.org/stable/modules/classes.html#samples-generator">https://scikit-learn.org/stable/modules/classes.html#samples-generator</a></p><pre># Create our outlier detector<br>outlier_detector = EllipticEnvelope(contamination=.1)</pre><pre># Fit the detector<br>outlier_detector.fit(features)</pre><pre># Predict the outliers<br>outlier_detector.predict(features)</pre><p>Here’s how the output should look like:</p><pre>Output:<br>[-1,  1,  1,  1,  1,  1,  1,  1,  1,  1]</pre><p>Notice the first element inside the output is set to -1. Which means that the value in the first element of the features are labelled as outlier.</p><p>However, there are limitation to this technique. Notice how we set a <em>contamination</em> parameter inside the EllipticEnvelope instance. This basically sets the proportion of observations that our data are going to be considered as outliers. You can think of <em>contamination </em>as the estimate of the cleanliness of the data. If you’re expecting to have few outliers, you can set the contamination to some small values. Similar to that if you’re believe that your data might contain more outliers, you can experiments with some large values. I believe you already familiar with the terms of <em>experiments, experiments, experiments….</em>. Very important for a machine learning practitioners (YOU).</p><p>Alright, we’re done.</p><p>There are some other technique we can use to identify these extreme values in our features. You can try using interquartile range (IQR). IQR is basically define the difference between the first and third quartile of a set of data. Outliers will be easy to detect since it will far from the main concentration of data given that the IQR defines the spread of the bulk of the data.</p><p>However, it is a good practice to always understand your data first before doing this technique. Handling outliers have their own advantages and disadvantages. I mean, who knows, maybe your data is simply just misclassified given that it has outliers or extreme values in one of its features.</p><p>Just keep learning folks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da2d92585c88" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>