<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Go Far AI: AI Topics]]></title><description><![CDATA[takling anything AI]]></description><link>https://www.gofar.ai/s/hero</link><image><url>https://substackcdn.com/image/fetch/$s_!TcLY!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe285d57e-63be-445b-9789-cf840c78c446_802x802.png</url><title>Go Far AI: AI Topics</title><link>https://www.gofar.ai/s/hero</link></image><generator>Substack</generator><lastBuildDate>Fri, 01 May 2026 20:25:23 GMT</lastBuildDate><atom:link href="https://www.gofar.ai/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Ali Atiah Alzahrani]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[aliatiah@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[aliatiah@substack.com]]></itunes:email><itunes:name><![CDATA[Ali Atiah Alzahrani]]></itunes:name></itunes:owner><itunes:author><![CDATA[Ali Atiah Alzahrani]]></itunes:author><googleplay:owner><![CDATA[aliatiah@substack.com]]></googleplay:owner><googleplay:email><![CDATA[aliatiah@substack.com]]></googleplay:email><googleplay:author><![CDATA[Ali Atiah Alzahrani]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[LLMs vs. Human Mind: Understanding the Creativity Gap.]]></title><description><![CDATA[Exploring What Sets Human Imagination Apart from LLMs and AI's Logic.]]></description><link>https://www.gofar.ai/p/llms-vs-human-mind-understanding</link><guid isPermaLink="false">https://www.gofar.ai/p/llms-vs-human-mind-understanding</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Fri, 19 Jul 2024 20:20:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!lHpR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lHpR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lHpR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lHpR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:532364,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lHpR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Introduction</strong></h3><p>In the exciting world of artificial intelligence, autoregressive large language models (LLMs) like GPT-4 have changed the way we think about machines talking and writing like humans. These models can create text that feels incredibly human-like, from stories to computer code, making it hard at times to tell if a human or a machine wrote something.</p><p>Even with these amazing advances, there's a big difference between what LLMs can do and what the human mind can achieve, especially in planning for the future, taking risks, and being truly creative. This blog post will look at these differences. We'll see how, although LLMs can copy the way we use words, they don't quite match the human mind's ability to think deeply and come up with new ideas.</p><p>The story of LLMs shows how clever human beings have been in creating these models. But as we get closer to what seems like the dream of creating a machine as smart as a person, we also see the big gaps that remain. These models show us that making a machine that truly thinks and feels like a human is not only hard but might require us to think in new ways.</p><p>In this blog post, we'll take a closer look at how LLMs work and what they've achieved. We'll also talk about where they don't quite measure up to human thinking and creativity. We hope to shed some light on what the future might hold for AI, the ethical questions it brings up, and how human creativity and decision-making fit into a world where machines can write and speak. Let's explore the world of LLMs together, looking at the good, the challenges, and what lies ahead.</p><p><em><strong>Note: Detailed equations and experiments will be shared in a follow-up publication. This post is non-technical and includes simplified equations to help illustrate the core concepts.</strong></em></p><h3><strong>Understanding Autoregressive Language Models</strong></h3><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = p(y_t | y_{t-1}, y_{t-2}, ..., y_1) = \\text{softmax}\\left( \\frac{QK^T}{\\sqrt{d_k}} \\right)V\n&quot;,&quot;id&quot;:&quot;GUKANCOOLS&quot;}" data-component-name="LatexBlockToDOM"></div><p>Let's start by talking about what autoregressive language models, or LLMs for short, actually are. Imagine you're writing a sentence and you pause, not sure what word should come next. If you've ever used a phone or computer that suggests the next word for you, you've got a basic idea of what these models do. But LLMs like GPT-3 are like those suggestions on steroids. They don't just look at the last few words you typed; they consider everything you've written so far to guess what comes next. And they're really good at it.</p><p>These models are trained on a massive amount of text from the internet&#8212;books, articles, websites, you name it. This training helps them learn how words and phrases naturally fit together. Because they've seen so many examples, they can write text that sounds quite human. They can finish a story, write an essay, or even generate new ideas for a movie script.</p><p>One of the coolest things about LLMs is their flexibility. They're not just stuck on one topic; they can write about anything from space travel to baking cakes because they've learned from a wide range of sources. This ability has made them super popular for a bunch of different tasks, like helping writers come up with ideas, assisting students with homework, or even writing code for programmers.</p><p>But even though they can do all these things, it's important to remember that LLMs are still just guessing what word comes next based on what they've seen before. They don't really "understand" what they're writing in the way we do. They're like parrots, repeating things they've heard without really getting the meaning behind them.</p><p></p><h3><strong>Taking LLMs Further with Knowledge Retrieval</strong></h3><p></p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p(y_t | y_{t-1}, y_{t-2}, ..., y_1, D) = \\text{softmax}\\left( \\frac{Q(K^T + R^T)}{\\sqrt{d_k}} \\right)V&quot;,&quot;id&quot;:&quot;TYOHXPXZRW&quot;}" data-component-name="LatexBlockToDOM"></div><p>The evolution of autoregressive language models (LLMs) like GPT has taken a significant leap forward with the addition of knowledge retrieval capabilities. This enhancement allows models not just to generate text based on internal data but also to access external information in real-time, much like consulting a vast online library for facts or detailed insights.</p><p>Enter Retrieval-Augmented Generation (RAG) models, which marry the linguistic skill of LLMs with the ability to pull in specific facts from a large database or the internet. When faced with questions requiring detailed knowledge, these models search for relevant information outside their training data, then integrate this into their responses.</p><p>This blending of creativity and precision transforms how AI understands and generates text, making it not only more relevant but also more informative. It's a step closer to AI that truly interacts with the breadth of human knowledge.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\n&quot;,&quot;id&quot;:&quot;VACWRHNXKX&quot;}" data-component-name="LatexBlockToDOM"></div><p></p><h3><strong>Limitations of LLMs Compared to the Human Mind</strong></h3><h4>Lack of Forward Planning</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p(y_t | s_t, D) = \\text{softmax}\\left( \\frac{Q(s_t, y, D)}{\\sqrt{d_k}} \\right)V \\quad \\\n&quot;,&quot;id&quot;:&quot;SYYHSPQCNZ&quot;}" data-component-name="LatexBlockToDOM"></div><p>When we talk or write, we often have a goal or an end point in mind. We think about what we want to say next, sometimes planning several steps ahead to make our point clearly. But LLMs don't work like this. They focus on the moment, choosing the next word based only on what's been said before. They don't plan ahead or think about the end goal of a conversation or a piece of writing.</p><p>For example, if you're writing a story with an LLM, it can help you write the next sentence or paragraph, but it doesn't have an overall plot or message in mind. It's like writing without knowing how the story will end. This is a big difference from how humans think and create, where we often start with an end goal or a message we want to convey.</p><h4>Risk-Aversion and Predictability</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = \\arg \\max_{y} \\left[ Q(s_t, y) + \\lambda \\cdot U(y) \\right]\n&quot;,&quot;id&quot;:&quot;QXJXTSYMKI&quot;}" data-component-name="LatexBlockToDOM"></div><p>LLMs tend to play it safe. Since they're built to predict the most likely next word based on their training, they usually go for the option that's been seen most often. This means their responses can be quite predictable and sometimes boring. They're not great at taking risks or trying something new and unexpected.</p><p>Humans, on the other hand, can decide to take a creative leap or introduce a twist that no one sees coming. We value originality and the ability to surprise, which is something LLMs struggle with. When we make art, write stories, or solve problems, we often do so by stepping into the unknown, taking risks, and experimenting. This is how new ideas and innovations come about.</p><h4>The Creative Gap</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = \\arg \\max_{y} \\left[ Q(s_t, y) + \\lambda \\cdot U(y) + \\mu \\cdot I(y) \\right]\n\n\n&quot;,&quot;id&quot;:&quot;QJNEWDCIGF&quot;}" data-component-name="LatexBlockToDOM"></div><p>Creativity involves coming up with something new, whether it's an idea, a solution to a problem, or a piece of art. While LLMs can generate text that might seem creative because it puts words together in new ways, they're really just remixing bits and pieces of what they've been trained on. They don't have the ability to think outside the box or come up with truly novel ideas from scratch.</p><p>Humans are capable of imagination&#8212;thinking of things that don't exist yet or that they've never experienced. We can dream up entirely new worlds, invent new technologies, or create art that expresses unique emotions and perspectives. This level of creativity is something LLMs currently can't match because they lack the ability to generate truly original ideas or feel emotions.</p><p></p><h3><strong>The Role of Training Data</strong></h3><p>One of the biggest reasons LLMs have limitations comes down to their training data. Think of training data like the books, conversations, movies, and all other kinds of information a person might learn from throughout their life. But instead of a lifetime, LLMs get crammed with this information all at once during their training phase. They learn from texts found online, which means they're learning from things that have already been created by humans.</p><p>This reliance on existing data means LLMs are great at giving back what they've seen in new combinations, but they can't really come up with something totally new. They're like a chef who can only cook with ingredients they've used before, unable to invent new ingredients or imagine new flavors beyond what they've tasted.</p><p>Moreover, because they learn from what's already out there, they can also pick up and repeat biases or errors found in their training materials. This is a challenge for people who make and use LLMs because it means they have to be very careful about the data they use to train these models. They want to make sure LLMs are helpful and fair, not just repeating the mistakes of the past.</p><h4>Challenges in Emulating Risk-Taking and Forward-Thinking</h4><p>Trying to get LLMs to think ahead, take risks, or be truly creative is hard because these actions often require understanding context, having goals, and being able to imagine outcomes that don't exist yet. LLMs don't have personal experiences or desires; they don't want anything. They don't get excited about a risky idea or feel proud of a creative solution. Because of this, designing LLMs to emulate such complex human behaviors without directly copying from specific examples in their training data is a big challenge.</p><p>Researchers are working on ways to improve LLMs, like teaching them to follow certain rules or goals, or using feedback from humans to guide them toward more creative and varied responses. But there's still a long way to go before they can truly mimic the depth of human creativity and foresight.</p><p></p><h3><strong>Ethical and Practical Implications</strong></h3><p>Using autoregressive language models (LLMs) raises some important questions about ethics and practicality, especially as these tools become more integrated into our daily lives and work. The limitations of LLMs, such as their inability to plan ahead, take risks, or create genuinely new ideas, have implications for how we use them and what we expect from them.</p><h4>Ethical Considerations</h4><p>One major ethical concern is the potential for LLMs to propagate biases found in their training data. Since LLMs learn from a vast array of online texts, they can inadvertently learn and replicate societal biases. This raises ethical questions about fairness and representation, especially when LLMs are used in decision-making processes or creating content that reaches a wide audience.</p><p>Furthermore, as LLMs become more capable of generating human-like text, there's a risk of misinformation or impersonation. Ensuring that generated content is accurately represented as machine-generated is crucial to maintaining trust and integrity in information.</p><h4>Practical Implications for Industries</h4><p>For industries that rely on innovation and creativity, the limitations of LLMs mean that they can't fully replace human creativity. While LLMs can assist in brainstorming sessions, content creation, and even some aspects of design and engineering, they still require human oversight to ensure originality and alignment with goals.</p><p>However, LLMs also offer opportunities to automate repetitive tasks, provide inspiration for human creators, and process large amounts of information more quickly than a human could. This can lead to more efficient workflows and free up human workers to focus on tasks that require genuine creativity, emotional intelligence, and strategic planning.</p><h3><strong>Future Directions</strong></h3><p>Despite their limitations, the development of LLMs is ongoing, and researchers are constantly looking for ways to overcome these challenges. Future advancements may include models that can better understand context, simulate planning, or more effectively incorporate feedback to generate truly novel and valuable outputs.</p><h4>Incorporating Models for Planning and Risk Assessment</h4><p>One area of research focuses on integrating models that can simulate planning or assess potential outcomes based on certain actions. This could help LLMs better mimic the human ability to plan ahead and consider the implications of their choices.</p><h4>Enhancing Creativity Through Diverse Training and Feedback Loops</h4><p>Improving the creativity of LLMs might involve diversifying the training data and developing more sophisticated feedback mechanisms. By exposing LLMs to a wider range of creative outputs and allowing them to learn from human feedback, there's potential for these models to produce more varied and innovative content.</p><h3><strong>Conclusion</strong></h3><p>As we've explored the capabilities and limitations of autoregressive language models, it's clear that while they represent a significant technological advancement, they are not yet close to replicating the full scope of human intelligence and creativity. Understanding these limitations is crucial as we continue to integrate AI into various aspects of our lives and work.</p><p>The journey toward more advanced AI, possibly even artificial general intelligence, is ongoing. By acknowledging the gaps in current models and focusing on ethical and innovative research, we can move closer to creating AI that complements human capabilities, encourages creativity, and benefits society as a whole.</p><p>Let's remain curious and open-minded, appreciating the advancements made so far while striving for the breakthroughs that lie ahead. In the ever-evolving relationship between humans and machines, the future holds endless possibilities for collaboration, innovation, and discovery.</p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[RAG, REALM, RETRO & Beyond: The Evolution of Retrieval-Augmented Models]]></title><description><![CDATA[Breaking Down the Basics: A Simple Guide to RAG, REALM, and RETRO AI Models.]]></description><link>https://www.gofar.ai/p/rag-realm-retro-and-beyond-the-evolution</link><guid isPermaLink="false">https://www.gofar.ai/p/rag-realm-retro-and-beyond-the-evolution</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Sun, 12 Nov 2023 08:15:14 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!VeQK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Language models have really changed recently. Now, they use extra information from external sources to become smarter. This makes models like RAG, REALM, and RETRO better than the usual large language models we used before. In this post, we'll look at how RAG, REALM, and RETRO work, what's good and bad about them, how they're different from each other, and how they're better than traditional LLMs. We'll also think about what might come next in this area.</p><h3><strong>RAG (Retrieval-Augmented Generation):</strong> </h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VeQK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VeQK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 424w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 848w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 1272w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VeQK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png" width="1072" height="334" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/db382620-ad44-444f-b148-3b5460405bcc_1072x334.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:334,&quot;width&quot;:1072,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:136074,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VeQK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 424w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 848w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 1272w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Paper: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks <a href="https://arxiv.org/abs/2005.11401">[Here]</a></figcaption></figure></div><p>RAG is a special kind of language model. It combines a language model with a system that finds information from a large group of documents. This helps RAG give better and more accurate answers, especially when you need specific facts. But, RAG's answers depend on how good its information sources are. Sometimes, it struggles to mix the information it finds into its answers smoothly.</p><h4><strong>RAG Mechanism:</strong></h4><p>Understanding the Retrieval-Augmented Generation (RAG) model requires diving into its unique mechanism that combines retrieval and generation based on conditional probability. At its core, RAG operates by calculating the probability of generating an appropriate answer <em>A</em> given a specific query Q and a set of retrieved documents D1, D2, &#8230;, Dn. This process is represented mathematically as:</p><p></p><p>This equation signifies the model&#8217;s effort to find the likelihood of the answer <em>A</em> when we have a query <em>Q</em> and relevant documents D1, D2, &#8230;, Dn. The RAG model achieves this in two primary steps:</p><ol><li><p><strong>Document Retrieval</strong>: For a given query <em>Q</em>, the model first retrieves a set of documents that are likely to contain relevant information. This step involves estimating:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p_n(D_1, D_2, ..., D_n|q)&quot;,&quot;id&quot;:&quot;RZOBLLCIPS&quot;}" data-component-name="LatexBlockToDOM"></div><p>which is the probability of each document being relevant to the query.</p></li></ol><p></p><ol start="2"><li><p><strong>Response Generation</strong>: Following retrieval, the model uses both the query and the gathered documents to generate an answer (A). This crucial step involves approximating</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p_\\theta(A|q,D_1,D_2, ..., D_n) &quot;,&quot;id&quot;:&quot;NPTVQFSNNK&quot;}" data-component-name="LatexBlockToDOM"></div><p>the probability of generating the answer considering both the query and the retrieved documents. So the overall expression of the RAG process:</p></li></ol><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p_{RAG}(y|x) \\approx \\sum_{d \\in top-k(p(\\cdot|x))} p_n(d|q)p_\\theta(A|q,d)&quot;,&quot;id&quot;:&quot;TODMKKPBUU&quot;}" data-component-name="LatexBlockToDOM"></div><p>Through this sophisticated approach, the RAG model effectively maximizes the chances of producing the most relevant and accurate answer <em>A</em> based on the query <em>Q</em> and the information extracted from the selected documents D1, D2, &#8230;, Dn.  This mechanism showcases the innovative blend of retrieval and generation, pushing the boundaries of what language models can achieve.</p><h4><strong>A Simple Way to Use RAG with Hugging Face:</strong></h4><p>For those looking to get started with the RAG model, a straightforward approach is to use the implementation provided by Hugging Face's Transformers library. This popular library simplifies the process, allowing you to leverage RAG's capabilities with just a few lines of code.</p><p>Hugging Face has pre-trained the RAG model on a vast dataset, making it readily available for various tasks, especially question-answering. The model combines a powerful retriever and a language generation model, offering high-quality responses by fetching relevant information from a large dataset.</p><p>To use RAG from Hugging Face, you first need to install the Transformers and Torch libraries. Once installed, you can initialize the RAG components &#8211; the tokenizer, retriever, and the model itself &#8211; directly from Hugging Face's model repository.</p><p>The process is as follows:</p><ol><li><p><strong>Initialize the Tokenizer</strong>: This component converts text inputs into a format that the model can understand.</p></li><li><p><strong>Set Up the Retriever</strong>: The retriever fetches relevant documents based on the input query.</p></li><li><p><strong>Load the RAG Model</strong>: The model uses the retrieved documents and the input query to generate an answer.</p></li></ol><p>Here's a basic example of how you can use RAG for a simple question:</p><pre><code># Import necessary classes
from transformers import RagTokenizer, RagTokenForGeneration, RagRetriever
import torch

# Function to generate an answer to a question
def generate_answer(question):
    # Initialize tokenizer, retriever, and model
    tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
    retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
    model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)

    # Process the question
    inputs = tokenizer(question, return_tensors="pt")

    # Generate the answer
    with torch.no_grad():
        generated_ids = model.generate(input_ids=inputs["input_ids"])

    # Decode and return the answer
    return tokenizer.decode(generated_ids[0], skip_special_tokens=True)

# Example usage
question = "What is the capital of Saudi Arabia?"
print(generate_answer(question))</code></pre><p>This code demonstrates how easily you can implement RAG for question-answering tasks. When you run this with a question like "What is the capital of Saudi Arabia?", the model should return "Riyadh" as the answer.</p><h3><strong>REALM (Retrieval-Augmented Language Model):</strong></h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aIOb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aIOb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 424w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 848w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 1272w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aIOb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png" width="1456" height="432" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:432,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:169637,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!aIOb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 424w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 848w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 1272w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Paper: REALM: Retrieval-Augmented Language Model Pre-Training <a href="https://arxiv.org/abs/2002.08909">[Here]</a></figcaption></figure></div><p>REALM is similar to RAG but works a bit differently. It learns to find useful documents and make answers at the same time. This makes REALM really good at giving relevant and correct answers. The downside is that it can be complex and needs a lot of computing power. Like RAG, it also depends on having good sources of information.</p><h4><strong>REALM Mechanism:</strong></h4><p>REALM, much like RAG, carries out its retrieval and generation processes in a sequential manner:</p><ol><li><p><strong>Retrieval Phase</strong>: In the first phase, REALM identifies relevant documents or information snippets that can assist in answering a query. This process is rooted in the concept of information retrieval, where the model is trained to search a vast database of texts to find the most pertinent pieces of information relative to the input query.</p></li><li><p><strong>Language Modeling Phase</strong>: Once relevant documents are retrieved, REALM uses this information, combined with the original query, to generate a response. This step involves a language model that synthesizes information from both the query and the retrieved texts to construct an answer.</p></li></ol><p>The retrieval phase can be represented in a probabilistic framework as follows:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(D|q)&quot;,&quot;id&quot;:&quot;SPSJJHFVJL&quot;}" data-component-name="LatexBlockToDOM"></div><p>This equation calculates the probability of a document <em>D</em> being relevant given a query <em>Q</em>. REALM optimizes this retrieval probability to ensure that only the most pertinent documents are considered in the next phase.</p><p>The language modeling phase, which follows the retrieval, can be represented as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(A&#8739;Q,D)&quot;,&quot;id&quot;:&quot;YDJOUBBUZM&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here, <em>A</em> is the generated answer, <em>Q</em> is the input query, and <em>D</em> represents the retrieved documents. This equation signifies how the model calculates the probability of generating a specific answer based on both the query and the retrieved information.</p><p>Together, these two phases enable REALM to not only understand and process the input query but also to enhance its responses by integrating external, contextually relevant information. This makes REALM particularly effective in scenarios where an in-depth understanding and up-to-date information are crucial for generating accurate responses.</p><p>REALM and RAG differ in their process integration: REALM uses a two-step approach, first retrieving documents with <em>P</em>(<em>D</em>&#8739;<em>Q</em>) and then generating an answer with <em>P</em>(<em>A</em>&#8739;<em>Q</em>,<em>D</em>), while RAG combines these steps into one <em>P</em>(<em>A</em>&#8739;<em>Q</em>,<em>D</em>1&#8203;,<em>D</em>2&#8203;,&#8230;,<em>Dn</em>&#8203;), blending retrieval and generation more directly.</p><h4><strong>Using REALM in a Simple Way with Hugging Face:</strong></h4><p>Incorporating the REALM into your projects can be straightforward, especially when utilizing resources from Hugging Face's Transformers library. Hugging Face simplifies the process of applying advanced models like REALM, making it accessible even to those new to the field of AI.</p><p>REALM stands out for its ability to retrieve relevant information as part of its learning process, enhancing the quality of its responses. This is a significant step up from traditional language models, as it allows REALM to provide more accurate and context-aware answers.</p><p>To get started with REALM using Hugging Face, follow these basic steps:</p><ol><li><p><strong>Install the Necessary Libraries</strong>: Make sure you have Transformers and Torch installed in your environment. You can install them using pip if you haven't already.</p></li><li><p><strong>Initialize the Components</strong>: Similar to using RAG, you will need to initialize the tokenizer and the model. Hugging Face provides pre-trained versions that you can use directly.</p></li><li><p><strong>Load the REALM Model</strong>: Hugging Face's model repository includes versions of REALM that are pre-trained and ready to use. This saves you the time and effort of training the model from scratch.</p></li></ol><p>Here&#8217;s a simple code snippet demonstrating how to use REALM for a basic task:</p><pre><code># Import the required classes
from transformers import RealmTokenizer, RealmForOpenQA

# Initialize the tokenizer and model
tokenizer = RealmTokenizer.from_pretrained("google/realm-cc-news-pretrained-bert")
model = RealmForOpenQA.from_pretrained("google/realm-cc-news-pretrained-bert")

# Your query
query = "What is the tallest mountain in the world?"

# Tokenize and process the query
inputs = tokenizer(query, return_tensors="pt")
output = model(inputs)

# Process and display the result
answer = tokenizer.decode(output, skip_special_tokens=True)
print(f"Answer: {answer}")</code></pre><p>This example is a basic illustration of using REALM for question answering. When you input a query, the model retrieves information and generates an answer. Keep in mind that REALM's performance is highly dependent on the quality of the retrieval and the relevance of the pre-trained model to your task.</p><p></p><h3><strong>RETRO (Retrieval-Enhanced Transformer):</strong></h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3I38!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3I38!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 424w, https://substackcdn.com/image/fetch/$s_!3I38!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 848w, https://substackcdn.com/image/fetch/$s_!3I38!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 1272w, https://substackcdn.com/image/fetch/$s_!3I38!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3I38!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png" width="1258" height="526" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:526,&quot;width&quot;:1258,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:156576,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3I38!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 424w, https://substackcdn.com/image/fetch/$s_!3I38!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 848w, https://substackcdn.com/image/fetch/$s_!3I38!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 1272w, https://substackcdn.com/image/fetch/$s_!3I38!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Paper: Improving language models by retrieving from trillions of tokens <a href="https://arxiv.org/abs/2112.04426">[Here]</a></figcaption></figure></div><p> RETRO also uses a language model and a system to find information. But it tries to be efficient, meaning it works well without using too much computing power. RETRO is great for giving detailed answers to complicated questions. The trade-off is that it sometimes doesn't go as deep into the information as it could because it wants to stay efficient.</p><h4><strong>RETRO Mechanism:</strong></h4><p>Unlike models that solely rely on pre-trained knowledge or integrate retrieval in a multi-step process, RETRO embeds retrieval into the core of its language generation mechanism.</p><p>At its core, RETRO operates through a mechanism that involves two key components:</p><ol><li><p><strong>Retrieval Component</strong>: This part of RETRO is responsible for scanning through a vast database of text segments to find pieces of information relevant to a given query. The retrieval process is designed to be highly efficient, focusing on extracting the most pertinent information with minimal computational overhead.</p></li><li><p><strong>Language Generation Component</strong>: Following the retrieval of relevant text segments, the language model component of RETRO takes over. It uses the retrieved information along with the original query to construct a coherent and contextually appropriate response.</p></li></ol><p>The operational equation for RETRO can be represented as follows:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(A|Q,R_1,R_2, ..., R_n) &quot;,&quot;id&quot;:&quot;RMXCNMDNUL&quot;}" data-component-name="LatexBlockToDOM"></div><p>In this equation, <em>A</em> represents the answer generated by the model, <em>Q</em> is the input query, and <em>R</em>1&#8203;,<em>R</em>2&#8203;,&#8230;,<em>Rn</em>&#8203; are the retrieved text segments relevant to the query. This formulation underscores RETRO's focus on efficiently leveraging external information (the retrieved segments) in concert with the query to produce a relevant response.</p><p>RETRO&#8217;s mechanism is distinct in its emphasis on the efficiency of retrieval and the integration of this retrieval directly into the response generation process. By doing so, RETRO aims to deliver highly contextual and accurate answers, even for complex queries, while maintaining computational efficiency. This balance makes RETRO a unique and valuable addition to the landscape of advanced language models.</p><h4><strong>Effortlessly Implementing RETRO with Hugging Face</strong></h4><p>For those interested in experimenting with the RETRO model, a convenient and effective approach is to utilize the resources offered by Hugging Face's Transformers library. </p><p>To start using RETRO with Hugging Face, here are some basic steps:</p><ol><li><p><strong>Installation</strong>: Ensure that the Transformers and Torch libraries are installed in your Python environment. You can easily install these using pip.</p></li><li><p><strong>Initialization</strong>: As with other models, you will need to initialize the necessary components &#8211; the tokenizer and the RETRO model itself. Hugging Face provides pre-trained models that are ready for immediate use.</p></li><li><p><strong>Load the RETRO Model</strong>: Accessing Hugging Face's model repository, you can load a version of RETRO that has been pre-trained on a wide range of data. This pre-training makes the model robust and versatile for various applications.</p></li></ol><p>Here's a simple example to illustrate basic usage:</p><pre><code># Import necessary classes
from transformers import RetroTokenizer, RetroForCausalLM

# Initialize the tokenizer and model
tokenizer = RetroTokenizer.from_pretrained("google/retro-large")
model = RetroForCausalLM.from_pretrained("google/retro-large")

# Example query
query = "What are the benefits of renewable energy?"

# Tokenize and process the query
inputs = tokenizer(query, return_tensors="pt")
output = model.generate(inputs.input_ids)

# Decode and display the response
response = tokenizer.decode(output[0], skip_special_tokens=True)
print(f"Response: {response}")
</code></pre><p></p><p></p><p><strong>Comparison and Future Directions:</strong></p><p> RAG, REALM, and RETRO all try to improve language models by using outside information. They do it in different ways: RAG adds extra info to what it already knows, REALM learns to find info and answer questions simultaneously, and RETRO focuses on being efficient. In the future, we might see models that can use the latest info, utilizing semantic search methodologies to understand context better.</p><p>RAG, REALM, and RETRO are important steps in making language models that understand and use information better. Each one has its own strengths. They show us how AI can use outside information to improve. Later in another post, I will write about the coding part (which is easy, since it will use HuggingFace libraries)</p>]]></content:encoded></item><item><title><![CDATA[Connecting Stochastic Calculus and Deep Learning in Finance]]></title><description><![CDATA[One Way to Explore the Magic of Combining Fields for Better Understanding]]></description><link>https://www.gofar.ai/p/connecting-stochastic-calculus-and</link><guid isPermaLink="false">https://www.gofar.ai/p/connecting-stochastic-calculus-and</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Tue, 01 Aug 2023 08:36:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Meub!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Meub!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Meub!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Meub!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Meub!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Meub!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Meub!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg" width="1456" height="1092" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1092,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3095985,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Meub!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Meub!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Meub!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Meub!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Image by Author.</figcaption></figure></div><p>One effective way to grasp complex fields is by applying them in conjunction with other disciplines, and vice versa. The process of exploring two different fields can lead to a more efficient understanding of both. In my case, I endeavored to establish a connection between stochastic calculus (specifically, quantitative finance) and machine learning (specifically, deep learning).</p><p>This journey proved to be enriching, and I relied on just three reference books throughout the process. I highly recommend these books to anyone interested in the quantitative field, even if they are not specifically interested in stochastic calculus. Interestingly, these references can directly relate to the Bayesian approach in deep reinforcement learning and natural language processing as well. The exciting part is experimenting jointly, like estimating volatility in the Black-Scholes model using GARCH and using it as input for an LSTM network.</p><p>Let me briefly introduce the three books:</p><ol><li><p><strong>Brownian Motion Calculus:</strong> After exploring various books, I found this to be the most accessible one, allowing for easy absorption of knowledge in all aspects of stochastic calculus, including topics like change of measure and change of numerire in probability theory.</p></li><li><p><strong>The Volatility Smile:</strong> This is an advanced yet practical book suitable for experienced quants, covering both fundamental concepts and derivations of the Jump-Diffusion model, along with solutions for advanced stochastic volatility models. I would suggest apporach this book after finishing the first book &#8220;Browninan motion calculus.</p></li><li><p><strong>ML with SciKit-Learn, Keras, and TensorFlow: </strong>This amazing book serves as a great reference for both Machine Learning and Deep Learning in TensorFlow. It comprehensively covers ML basics and even extends to transformer architectures. I used this book alongside stochastic calculus as a coding reference, helping me develop algorithmic codes for my work.</p></li></ol><p>Overall, these three books provided invaluable insights and contributed significantly to my understanding and practical applications.</p>]]></content:encoded></item><item><title><![CDATA[Moving Beyond Language Models: Why Deep Reinforcement Learning is Key to Achieving AGI]]></title><description><![CDATA[Moving Beyond Pre-Trained Models and Fostering Independent Thought through Deep Reinforcement Learning, with DRL example.]]></description><link>https://www.gofar.ai/p/moving-beyond-language-models-why</link><guid isPermaLink="false">https://www.gofar.ai/p/moving-beyond-language-models-why</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Tue, 02 May 2023 04:14:50 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!WSPu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WSPu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WSPu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 424w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 848w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 1272w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WSPu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png" width="1456" height="701" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:701,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2333756,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WSPu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 424w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 848w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 1272w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Artificial Intelligence (AI) has come a long way since its inception. We have witnessed remarkable advances in natural language processing (NLP) and computer vision, among other areas. However, despite these achievements, AI still has a long way to go before it can truly be considered intelligent. In particular, while AI models such as GPT, BARTm, and their ilk have been able to perform impressive tasks, they are limited by their reliance on past experiences and their inability to generate truly original ideas. In order to attain true artificial general intelligence (AGI), we must look beyond these models and explore other avenues that can enable machines to develop independent thinking and creativity. One such avenue is deep reinforcement learning (DRL).</p><p>DRL is a type of machine learning that enables an agent to learn by interacting with its environment. In contrast to pre-trained models like GPT, which have a fixed set of parameters, DRL-based agents can adapt and learn from their experiences, enabling them to perform complex tasks that require a degree of creativity and independent thought. DRL is based on the principle of reward-based learning, where an agent is rewarded for taking actions that lead to positive outcomes, and penalized for taking actions that lead to negative outcomes. Over time, the agent learns to take actions that maximize its reward, thereby achieving its goals.</p><p>One of the key advantages of DRL is its ability to generate truly novel ideas. Unlike pre-trained models, which are limited by their dependence on past experiences, DRL-based agents can explore new and uncharted territory. By interacting with their environment and experiencing unique situations, DRL-based agents can develop a level of creativity and independent thought that is not possible with pre-trained models.</p><p>Another advantage of DRL is its ability to learn from its mistakes. In traditional machine learning, agents are trained using a large dataset of examples. While this approach can be effective, it does not allow for the agent to learn from its own mistakes. DRL, on the other hand, enables the agent to learn from its own experiences, allowing it to adapt and improve over time.</p><p>DRL has already been applied successfully in a number of domains, including robotics, gaming, and finance. In robotics, DRL-based agents have been able to perform complex tasks such as grasping objects and navigating through environments. In gaming, DRL-based agents have been able to outperform human players in games such as Go and chess. In finance, DRL-based agents have been able to make investment decisions that outperform human traders.</p><p>Despite the potential of DRL, there are still significant challenges that need to be overcome before it can be used to develop AGI. One of the biggest challenges is the need for large amounts of data. DRL requires a significant amount of data to train the agent effectively. This data must be diverse and representative of the real-world environment that the agent will be operating in. Another challenge is the need for efficient algorithms that can learn from this data in a reasonable amount of time. Finally, there is the challenge of designing a reward function that accurately reflects the agent's goals and objectives.</p><p>In conclusion, while pre-trained models like GPT have brought us closer to the goal of AGI, they are only a small step on the path towards true intelligence. To achieve AGI, we must look beyond these models and explore other avenues that can enable machines to develop independent thinking and creativity. DRL is one such avenue. By allowing machines to interact with their environment and experience unique situations, DRL-based agents can develop a level of creativity and independent thought that is not possible with pre-trained models. While there are still significant challenges that need to be overcome, DRL has the potential to revolutionize the field of AI and bring us closer to achieving true AGI.</p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[Customizing Deep Neural Networks with TensorFlow: Creating a Custom Layer]]></title><description><![CDATA[In this post, we will explore how to create a custom layer in TensorFlow, which can be useful when we need to add a specific functionality that is not available in the existing layers.]]></description><link>https://www.gofar.ai/p/customizing-deep-neural-networks</link><guid isPermaLink="false">https://www.gofar.ai/p/customizing-deep-neural-networks</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Mon, 19 Sep 2022 05:02:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!geLi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this post, we will explore how to create a custom layer in TensorFlow, which can be useful when we need to add a specific functionality that is not available in the existing layers.</p><p>TensorFlow is a powerful deep learning library that provides a wide range of pre-built layers such as convolutional, pooling, and dense layers. However, in some cases, we might need to implement a custom layer with a specific functionality.</p><p>To create a custom layer, we need to define a class that inherits from the <code>tf.keras.layers.Layer</code> class. This class provides several methods that need to be implemented:</p><ul><li><p><code>__init__()</code>: This method initializes the layer and defines its parameters.</p></li><li><p><code>build()</code>: This method creates the layer's variables, which are the weights and biases that will be learned during training.</p></li><li><p><code>call()</code>: This method performs the forward pass of the layer, which computes the output of the layer given its input.</p></li></ul><p>Let's dive into an example of creating a custom layer in TensorFlow.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!geLi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!geLi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 424w, https://substackcdn.com/image/fetch/$s_!geLi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 848w, https://substackcdn.com/image/fetch/$s_!geLi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 1272w, https://substackcdn.com/image/fetch/$s_!geLi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!geLi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png" width="1348" height="1008" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1008,&quot;width&quot;:1348,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:227275,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!geLi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 424w, https://substackcdn.com/image/fetch/$s_!geLi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 848w, https://substackcdn.com/image/fetch/$s_!geLi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 1272w, https://substackcdn.com/image/fetch/$s_!geLi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In this code, we define a custom layer in TensorFlow called <code>CustomLayer</code>. The layer takes an input tensor and returns the result of multiplying it by a weight matrix. The size of the weight matrix is specified by the <code>output_dim</code> parameter, which is passed to the layer's constructor.</p><p>The layer's <code>build</code> method is called when the layer is first used in a model. In this method, we create the weight matrix by calling the <code>add_weight</code> method of the layer, and set its shape and initializer. We also call the <code>build</code> method of the parent class to finish building the layer.</p><p>The layer's <code>call</code> method is called when the layer is applied to an input tensor. In this method, we multiply the input tensor by the weight matrix using TensorFlow's <code>matmul</code> function.</p><p>Finally, the layer's <code>compute_output_shape</code> method is called to determine the output shape of the layer based on the input shape.</p><p>Overall, this custom layer allows us to define a new type of layer in TensorFlow that can be used in deep neural networks. We can customize the behavior of the layer by modifying its <code>build</code> and <code>call</code> methods to suit our needs.</p>]]></content:encoded></item><item><title><![CDATA[Quantifying the Uncertainty in Deep Bayesian Q-Networks for Robust Decision Making]]></title><description><![CDATA[Deep Q-Networks (DQN) are a powerful class of reinforcement learning algorithms that have been successfully used in various applications, such as robotics, game playing, and finance.]]></description><link>https://www.gofar.ai/p/quantifying-the-uncertainty-in-deep</link><guid isPermaLink="false">https://www.gofar.ai/p/quantifying-the-uncertainty-in-deep</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Wed, 29 Jun 2022 04:52:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!dmGC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dmGC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dmGC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 424w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 848w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 1272w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dmGC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png" width="1456" height="531" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:531,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:979753,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dmGC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 424w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 848w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 1272w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Deep Q-Networks (DQN) are a powerful class of reinforcement learning algorithms that have been successfully used in various applications, such as robotics, game playing, and finance. However, one challenge with DQNs is their lack of robustness to uncertainties in the environment, which can result in suboptimal or unsafe decisions. In this blog post, we will discuss how to quantify the uncertainty in DQNs using Bayesian deep learning, and how to use this uncertainty to make more robust decisions.</p><p><strong>Bayesian Deep Learning</strong></p><p>Bayesian deep learning is a framework that combines deep learning with Bayesian inference to quantify the uncertainty in neural network models. In Bayesian deep learning, we treat the weights of the neural network as random variables, and we define a prior distribution over these weights. We then use Bayes' rule to update the prior distribution to a posterior distribution, given the observed data. The posterior distribution represents our updated belief about the weights, given the data.</p><p><strong>Uncertainty in DQNs</strong></p><p>In DQNs, the uncertainty arises from two sources: the stochasticity of the environment, and the uncertainty in the neural network model. The stochasticity of the environment refers to the randomness in the outcomes of the actions taken by the agent, due to the inherent randomness in the environment. The uncertainty in the neural network model refers to our uncertainty about the optimal actions given the current state of the environment, which is represented by the Q-values predicted by the neural network.</p><p><strong>Bayesian DQN</strong></p><p>To quantify the uncertainty in DQNs, we can use Bayesian deep learning to model the uncertainty in the neural network weights. Specifically, we can use a Bayesian neural network (BNN), which is a neural network with weights treated as random variables. We can then use Monte Carlo dropout (MC dropout) to approximate the Bayesian inference process. MC dropout involves adding dropout at test time and sampling multiple predictions from the network to estimate the distribution of the predictions.</p><p>The loss function for training the Bayesian DQN is the negative log-likelihood of the observed data, which includes the rewards received and the transitions between states. The loss function is modified to include a penalty term for the entropy of the distribution over the Q-values, which encourages exploration and reduces overconfidence in the predictions.</p><p><strong>Python Implementation</strong></p><p>To implement a Bayesian DQN in Python using TensorFlow, we can start with the standard DQN implementation and modify it to use a BNN and MC dropout. The following code shows an example of how to modify the Q-network in a DQN to use a BNN and MC dropout:</p><pre><code>class BayesianQNetwork(tf.keras.Model):
    def __init__(self, num_actions, num_hidden_units):
        super(BayesianQNetwork, self).__init__()
        self.num_actions = num_actions
        self.dense1 = tf.keras.layers.Dense(num_hidden_units, activation='relu')
        self.dense2 = tf.keras.layers.Dense(num_hidden_units, activation='relu')
        self.logits = tf.keras.layers.Dense(num_actions)

    def call(self, inputs):
        x = self.dense1(inputs)
        x = self.dense2(x)
        logits = self.logits(x)
        return logits

    def sample_predictions(self, inputs, num_samples=10):
        outputs = []
        for _ in range(num_samples):
            outputs.append(self(inputs))
        return tf.stack(outputs)</code></pre><div><hr></div><p>In this code, the Q-network is defined as a BNN with two hidden layers and a softmax output layer. The <code>sample_predictions</code> function is used to sample predictions from the network using MC dropout.</p><p>To modify the loss function to include the penalty term for entropy, we can use the following code:</p><pre><code>def bayesian_loss(model, states, targets, num_samples=10):
    """
    Computes the Bayesian loss of a model given the states and targets.
    
    Arguments:
    model -- the deep Q-network model
    states -- a batch of input states (numpy array of shape (batch_size, state_size))
    targets -- a batch of target Q-values (numpy array of shape (batch_size, num_actions))
    num_samples -- the number of samples to draw from the posterior distribution (default 10)
    
    Returns:
    The Bayesian loss (scalar).
    """
    # Compute the predicted Q-values and log variance for each state-action pair
    q_values = []
    log_variances = []
    for i in range(num_samples):
        q_values_i, log_variances_i = model(states, sample=True)
        q_values.append(q_values_i)
        log_variances.append(log_variances_i)
    q_values = tf.stack(q_values)  # shape: (num_samples, batch_size, num_actions)
    log_variances = tf.stack(log_variances)  # shape: (num_samples, batch_size, num_actions)
    
    # Compute the mean and variance of the predicted Q-values and log variances
    q_mean = tf.reduce_mean(q_values, axis=0)  # shape: (batch_size, num_actions)
    q_var = tf.math.reduce_variance(q_values, axis=0)  # shape: (batch_size, num_actions)
    log_var_mean = tf.reduce_mean(log_variances, axis=0)  # shape: (batch_size, num_actions)
    
    # Compute the Bayesian loss
    precision = tf.exp(-log_var_mean)
    loss = 0.5 * precision * tf.reduce_sum(tf.square(targets - q_mean), axis=-1) + \
           0.5 * tf.math.log(1 + q_var * precision)  # shape: (batch_size,)
    loss = tf.reduce_mean(loss)  # take the mean over the batch
    
    return loss
</code></pre><div><hr></div>]]></content:encoded></item></channel></rss>