<?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 Dohyeong Kim on Medium]]></title>
        <description><![CDATA[Stories by Dohyeong Kim on Medium]]></description>
        <link>https://medium.com/@dohyeongkim?source=rss-ceb0044f7b57------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*12ZTO13irtSu0aQDhGzQEg.jpeg</url>
            <title>Stories by Dohyeong Kim on Medium</title>
            <link>https://medium.com/@dohyeongkim?source=rss-ceb0044f7b57------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 13:51:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dohyeongkim/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[Deep Reinforcement Learning for a Real Robot — Series 1]]></title>
            <link>https://dohyeongkim.medium.com/deep-reinforcement-learning-for-a-real-robot-series-1-de80e7f57b73?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/de80e7f57b73</guid>
            <category><![CDATA[end-to-end-models]]></category>
            <category><![CDATA[domain-randomization]]></category>
            <category><![CDATA[physical-ai]]></category>
            <category><![CDATA[sim2real]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Tue, 12 May 2026 13:53:55 GMT</pubDate>
            <atom:updated>2026-05-12T13:53:55.801Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Agent training using Deep Reinforcement Learning has proven its excellent performance in virtual environments such as games.</p><ul><li><a href="https://dohyeongkim.medium.com/playing-moba-game-using-deep-reinforcement-learning-part-4-5fdfbff753df">Playing MOBA game using Deep Reinforcement Learning — part 4</a></li><li><a href="https://medium.com/nerd-for-tech/alphastar-implementation-series-part7-d28468c07739">AlphaStar implementation series — part7</a></li><li><a href="https://pub.aimind.so/playing-sonic-the-hedgehog-2-using-deep-learning-part-1-5e16c8fa802d">Playing Sonic The Hedgehog 2 using Deep Learning — Part 1</a></li></ul><p>However, to create an agent that is helpful to real people’s lives, a model trained in a virtual world must operate in the physical world.</p><p>In this series of posts, we will cover the results of experiments conducted on actual robots regarding the topic of Sim2Real in Deep Reinforcement Learning.</p><h3>Issaclab</h3><p>Issaclab, which incorporates Deep Reinforcement Learning techniques accumulated over many years, is being developed by NVIDIA.</p><p><a href="https://isaac-sim.github.io/IsaacLab/main/index.html">Welcome to Isaac Lab!</a></p><p>You can easily train a mobile robot to drive along a track using this tool.</p><p><a href="https://github.com/kimbring2/isaac_lab_jetbot_orin">GitHub - kimbring2/isaac_lab_jetbot_orin: Training Jetbot Orin through NVIDIA IsaacLab</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*6IhQvmU1NCvWO1gznr69Rw.gif" /><figcaption>Training Result of Simulator</figcaption></figure><p>Then, what would the results be if we ran the model trained in a virtual environment on a real robot?</p><p>In actual robots, you cannot expect results exactly like those in a simulation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*IVFLj40IZJsyjcBZRbq77w.gif" /><figcaption>Sim2Real without Domain Randomization</figcaption></figure><p>To solve the aforementioned problem in Sim2Real, the agent must be exposed to a wider variety of environment variations during training, while maintaining the overall framework.</p><p>We call this Domain Randomization, and let’s actually apply it in Isaaclab to see if the results improve.</p><h3>Various Domain Randomizations</h3><p>While various types of domain randomizations can be used depending on the type of agent, for an agent that moves using wheels by gathering visual information from a camera, the following could be applied.</p><ul><li>Randomize env lights</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*suc01ti7320snMe69ra5oQ.png" /></figure><ul><li>Randomize track appearance</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*aTlAuUIlXrcFmlNRg8Zzqg.png" /></figure><ul><li>Randomize curtain appearance</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*PVb4PtFIEhNAaAKU6hiaTQ.png" /></figure><ul><li>Randomize camera parameters</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*6Vwb-G5MJAiJaLGDWU52tQ.jpeg" /><figcaption>Image from <a href="https://borislavkostov.wordpress.com/articles/macro-photography-eng/focal-length-focusing-distance-working-distance/">this link</a></figcaption></figure><ul><li>Randomize motor parameters</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*dLyKj9DC5q9MY4O-G7BkdQ.png" /><figcaption>Image from <a href="https://www.researchgate.net/figure/System-Mass-Stiffness-Damping-second-order-harmonic-oscillator_fig2_280317415">this link</a></figcaption></figure><ul><li>Randomize camera pose</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*PT_2XIlxm87CwhCteh4Taw.png" /></figure><h3>Result of Domain Randomizations</h3><p>If we test the model trained after applying the above domain randomization on an actual robot, we can confirm that it performs the actions trained in the virtual environment well to some extent, even if not perfectly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*pgQ38_SKZCl8fmz-v0Mzbw.gif" /></figure><h3>Conclusion</h3><p>In this post, we examined techniques that enable agents trained using Deep Reinforcement Learning in a virtual environment to function properly in the real world. In the next post, I plan to conduct experiments on slightly more complex tasks.</p><h3>Real robot</h3><p>I conducted the experiment using a Jetbot equipped with NVIDIA Jetson Orin.</p><p><a href="https://test-bed-robot-for-ai.myshopify.com/products/jetbot-orin?variant=53153578844525">Jetbot Orin AI Kit - AI Robot Kit for Jetson Orin Nano</a></p><p>The software for the hardware was obtained from the link below.</p><p><a href="https://github.com/kimbring2/jetbot/tree/jetbot-orin">GitHub - kimbring2/jetbot at jetbot-orin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de80e7f57b73" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DeepMind Genie implementation series 1 — base structure]]></title>
            <link>https://dohyeongkim.medium.com/deepmind-genie-implementation-series-1-base-structure-47727338cc1a?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/47727338cc1a</guid>
            <category><![CDATA[generative-ai-use-cases]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[deepmind]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[tensorflow]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Sun, 17 Nov 2024 15:29:22 GMT</pubDate>
            <atom:updated>2024-11-17T15:32:52.419Z</atom:updated>
            <content:encoded><![CDATA[<h3>DeepMind Genie<a href="https://medium.com/@dohyeongkim/alphastar-implementation-series-part6-4044e7efb1ce?source=your_stories_page-------------------------------------"> implementation series 1 — </a>base structure</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oHUCD13tfagELq2HBWzfEQ.png" /></figure><p>It was revealed through <a href="https://deepmind.google/research/publications/60474/">DeepMind’s Genie: Generative Interactive Environments</a> study that generative models such as ChatGPT and Stable Diffusion can not only generate static data but can also create a dynamic environment itself based on any static image.</p><p>Thankfully, DeepMind did a good job of explaining the scaled-down experimental method in the paper so that researchers without many GPUs could experiment with the model described in the paper. In this series, we will implement the code according to the guidelines.</p><p>______________________________________________________________</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JeTb6HF1N--_dcD7SHl7_A.png" /><figcaption>Experiemnt guideline for single GPU user</figcaption></figure><p>______________________________________________________________</p><p>All codes for this post are available from the link below.</p><ul><li><a href="https://github.com/kimbring2/Genie_Implementation">https://github.com/kimbring2/Genie_Implementation</a></li></ul><h3>Dataset</h3><p>In the case of <a href="https://openai.com/index/procgen-benchmark/">Procgen Benchmark</a>, it was originally developed to measure the performance of a Deep Reinforcement Learning agent for an unseen environment.</p><p>Unlike other existing RL environments, it is lightweight because it was developed for research purposes only, not as a commercial game.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1d530b9b232c69cd40927decddb1e79/href">https://medium.com/media/a1d530b9b232c69cd40927decddb1e79/href</a></iframe><p>You can specify the level number from 0 to 10 and the start level through parameters.</p><h3>Latent Action Model</h3><p>One of the characteristics of Genie that differentiates it from existing related studies such as the <a href="https://worldmodels.github.io/">World Model</a> is that it can be trained from online random data like GPT. This method has the advantage of securing a lot of data. However, it is impossible to train with existing models that require such labels due to the absence of action labels for humans.</p><p>The paper explains that action labels could be obtained from random videos by utilizing the VQ-VAE method. However, no one has succeeded in implementing the method no matter how much I searched.</p><p>For that reason, an Inverse Dynamic Network of the <a href="https://openai.com/index/vpt/">OpenAI’s Video PreTraining (VPT) research</a>, which is an older version than the model in this paper, ​​was used to construct the basic model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bmssFqjd0RPgta-RwzlXvA.png" /><figcaption>Inverse Dynamic Model</figcaption></figure><p>In the case of the Coinrun environment, the number of actions is fixed at 7. Therefore, there is no problem with performance even if you use this method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2fb1f7dc0fcd79b9829990d9202adb5/href">https://medium.com/media/b2fb1f7dc0fcd79b9829990d9202adb5/href</a></iframe><h3>Video Tokenizer</h3><p>In the case of Genie, unlike other existing VQ-VAE-based image tokenizers, a Spatial-Temporal Transformer was used instead of a CNN-based encoder and decoder to train a large number of video data and improve speed.</p><p>As with the IDM model, this method could not be found implemented in code. Therefore, the <a href="https://keras.io/examples/generative/vq_vae/">CNN-based VQ-VAE</a> code was used to construct the basic model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*enenqHCV64Bi9FQ_HJNSGg.png" /></figure><p>Like IDM, the old model works well for the CoinRun environment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee5e9552b0cc8fe8561eed3eb2147377/href">https://medium.com/media/ee5e9552b0cc8fe8561eed3eb2147377/href</a></iframe><h3>Dynamic Model</h3><p>Unlike general Generative models, Genie requires the environment state to change dynamically according to the agent’s actions. Therefore, when state and action are input together, they must be sliced ​​appropriately and sent to each part of the model.</p><p>The method was able to be implemented accurately because code was available from previous studies such as the <a href="https://github.com/eloialonso/iris?tab=readme-ov-file">Transformers are Sample-Efficient World Models</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6a7f99d1984cbd0a9e268eb09727d4e/href">https://medium.com/media/d6a7f99d1984cbd0a9e268eb09727d4e/href</a></iframe><p>You can easily understand what role the Slicer plays when learning a model by looking at the diagram below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8BJwr0NZGd6ZLm4VvBk54Q.png" /></figure><p>Multi-head attention is the same as that used in other Transformer studies.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1ef36fc18d9812e4d7a6a19b661b962/href">https://medium.com/media/a1ef36fc18d9812e4d7a6a19b661b962/href</a></iframe><p>However, the model must generate the next data by referring only to previously generated data. Therefore, the casual attention method using a mask must be used when testing the trained model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*dBZC8p-xbmIRNcT6MeH58w.png" /></figure><p>In the case of Attention Block and Stack, additional networks such as Residual and Dropout are added. Furthermore, due to the nature of Tensorflow unlike the PyTorch, the memory kind of Tensor cannot be used inside the network. Therefore, the code is added to manage memory in a similar way to the LSTM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*4BbZswSggIV9zm0kl-MynQ.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e58a105f15ea7cda1b2fcf5814781c21/href">https://medium.com/media/e58a105f15ea7cda1b2fcf5814781c21/href</a></iframe><p>Ultimately, in the case of the World Model, which can be said to be the main body of the Genie, it can be implemented as shown in the code below by using the Slicer, Multi-Head Attention, and Attention Block&amp;Stack described above.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d1ab7753f4d846d63de5e050a44294f/href">https://medium.com/media/4d1ab7753f4d846d63de5e050a44294f/href</a></iframe><h3>Training the basic Genie model</h3><p>Training tasks can be done easily because there is no need to use an attention mask without any special techniques.</p><p>It is important to note that the action label generated from the inverse dynamic model is used instead of the action label of the actual data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c08492527c583808881192c9ef6cca45/href">https://medium.com/media/c08492527c583808881192c9ef6cca45/href</a></iframe><h3>Testing the basic Genie model</h3><p>However, when creating an environment from a trained model, various processing methods are used due to the nature of the generation model, which requires generating the next data based on previously generated data.</p><p>To be precise, the previously created Key and Value are stored in a tensor, combined with the Key and Value generated in the next generation, and then used as a new Key and Value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k_dvLQIb3LjHZzqiYY8pLg.png" /></figure><p>Next, because the next state must be created based on the action entered by the person during creation, the tokens of the action and state must be processed in order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*om4fD12piD6gEwFP0QG-QQ.png" /></figure><p>The PyGame is used in the actual code to receive the action from humans.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e73902b4ab28c705004e18cdf068c9a/href">https://medium.com/media/0e73902b4ab28c705004e18cdf068c9a/href</a></iframe><h3>Training Result</h3><p>This is the result of training and verifying the most basic Genie using the datasets and models described above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*zoIY4Jl5XgVg4Ipp0p-oWg.gif" /><figcaption>Generated Environemnt</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*0eiEylZFdsR21MJiTvdhKQ.gif" /><figcaption>Real environment</figcaption></figure><p>There are parts where the state is broken here and there, but considering that the number of training data is only 4000, it doesn’t seem to be a bad result.</p><h3>Future Work</h3><p>Once again, the models described in this post are slightly older than those in the Genie paper. Improvements are needed for each model part as follows.</p><ol><li>Latent Action Model — Utilizing VQ-VAE method for inverse dynamic</li><li>Tokenizer — Applying the Statial-Temporal transformer instead of CNN-based Encoder and Decoder</li><li>World Model — MaskGit should be used at the end point of the model for better-generating speed.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47727338cc1a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementation of A Generalist Agent of the DeepMind]]></title>
            <link>https://dohyeongkim.medium.com/implementation-of-a-generalist-agent-of-the-deepmind-b67b69828df6?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/b67b69828df6</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[transformers]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Mon, 25 Mar 2024 10:32:45 GMT</pubDate>
            <atom:updated>2024-03-25T10:32:45.392Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KXZrEntC8-cJsM8abJXVcA.png" /></figure><p>Performing multiple tasks using a single Deep Learning model can allow us to run our applications with limited resources. Therefore, this method will be necessary to sell our model.</p><p>It is known to be impossible to achieve this using ordinary DNN, CNN, and LSTM, but the <a href="https://arxiv.org/pdf/2205.06175.pdf">A Generalist Agent</a> paper proved that this is possible using the Transformer model.</p><p>In this post, let’s implement code on how to play CartPole and generate the Shakespeare script using a single Transformer model.</p><p>You can download the all datasets and codes used in this part from the below links</p><ul><li><a href="https://drive.google.com/file/d/1ycBJEef4v06oVxyVivpyTFJRBl85gvt4/view?usp=sharing">input.txt</a></li><li><a href="https://drive.google.com/drive/folders/1AfW-k1q2WqCqnjhq5Qzlop2zEyVKACOR?usp=sharing">CartPole_V1 - Google Drive</a></li><li><a href="https://drive.google.com/file/d/1oOaDIVPUCsxCeHcz7X3Q4Oj76aQcEmz_/view?usp=drive_link">Gato.ipynb</a></li><li><a href="https://drive.google.com/file/d/1PatKN6j-RsAG_JzLVZA4efX47lKOG03M/view?usp=sharing">Gato-Eval.ipynb</a></li></ul><p>I use the <strong>OpenAI Gym 0.21.0</strong> and <strong>Tensorflow 2.9.1</strong> version.</p><h3>Reference</h3><ul><li><a href="https://github.com/kamalkraj/minGPT-TF">minGPT-TF</a></li><li><a href="https://github.com/nikhilbarhate99/min-decision-transformer">min-decision-transformer</a></li></ul><h3>Dataset</h3><p>The dataset for training the Gato model must have the same length of sequences to be entered at once. Here, a length of 128 was used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/335f86f798c7747809c88f0ba0d346ee/href">https://medium.com/media/335f86f798c7747809c88f0ba0d346ee/href</a></iframe><p>Additionally, it should be arranged in the order of state, reward, and action, in the case of CartPole data. There are more labels than NLP data.</p><h3>Model</h3><p>In the case of the Gato model, the embedding network for both CartPole and NLP data needs to be together in one place.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28f3a195a544331bece22f8665a14ead/href">https://medium.com/media/28f3a195a544331bece22f8665a14ead/href</a></iframe><p>However, for each dataset, the core network should be trained using only the necessary embedding network using if.</p><h3>Training</h3><p>In order to train the prepared dataset and model, 2 separate training functions should be used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e34daf83690a798c03e0c41d1a6c0ed3/href">https://medium.com/media/e34daf83690a798c03e0c41d1a6c0ed3/href</a></iframe><p>The none value is used for data that is not related to the task we want to train, and the embedding function by the flag argument.</p><p>The loop for training is arranged as follows so that NLP and RL can be performed sequentially.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05933da7ec6c18a756d5ec70f31fab72/href">https://medium.com/media/05933da7ec6c18a756d5ec70f31fab72/href</a></iframe><p>If you train the Gato model about 500 epochs with a batch size of 64, we can see that both NLP and RL loss are appropriately reduced to some point.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hbuIgEGXbzDLWToYu3LCPw.png" /></figure><h3>Evaluating</h3><p>After training is complete, let’s play a CartPole and generate the Shakespeare script using one model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b34eed8a4caff655f1683198d960f606/href">https://medium.com/media/b34eed8a4caff655f1683198d960f606/href</a></iframe><p>We can see that the model performs both tasks successfully by visualizing it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FolQqyumpckY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DolQqyumpckY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FolQqyumpckY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9b13a7dd30b0d84c98c86d01306b547/href">https://medium.com/media/b9b13a7dd30b0d84c98c86d01306b547/href</a></iframe><h3>Conclusion</h3><p>In this post, we learned directly through code that the Transformer model can perform two completely different tasks simultaneously.</p><p>Considering that most applications in real life require performing multiple tasks at the same time such as playing the MMORPG game(You may need to communicate with your party member through chat), this method will be greatly helpful in using limited resources more efficiently.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b67b69828df6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Image to Latex using Vision Transformer]]></title>
            <link>https://dohyeongkim.medium.com/image-to-latex-using-vision-transformer-13fc4ce253d7?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/13fc4ce253d7</guid>
            <category><![CDATA[latex]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ocr]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Tue, 13 Feb 2024 04:38:53 GMT</pubDate>
            <atom:updated>2024-02-24T11:25:57.738Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Recent advancement in Transformer architecture allows us can transcribe the context of image to text easily than before.</p><p>For example, <a href="https://www.tensorflow.org/text/tutorials/image_captioning">Image captioning with visual attention</a> is one of the most popular applications of it.</p><p>Another possible usage of Image captioning is the <a href="https://github.com/harvardnlp/im2markup/tree/master">im2markup</a> application where a Latex formula image is converted to Latex formula text which can be used directly to write the paper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kJFtR2uOo_dypwDkAN10MA.png" /><figcaption>Captured from <a href="https://im2markup.yuntiandeng.com/">https://im2markup.yuntiandeng.com/</a></figcaption></figure><p>For that purpose, we can build the model using a Transformer like the below one. This model consists of two Transformer models. The first one is for feature extraction from images. The second one is for generating the latex word.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DEDC66Lhy4Xn25Xi1eaLKA.png" /></figure><p>Before running the source code of this post, please check you can run the Tensorflow code of <a href="https://www.tensorflow.org/text/tutorials/image_captioning">https://www.tensorflow.org/text/tutorials/image_captioning</a> link. Furthermore, you can find the original code of Vision Transformer from <a href="https://drive.google.com/file/d/1Br5bxRjOK9Rsq4GelWA9b7JCjbBz0FfU/view?usp=sharing">this link</a>. I combine two codes with minimal change.</p><p>Additionally, I use the preprocessed latex dataset of <a href="https://github.com/ritheshkumar95/im2latex-tensorflow/tree/master">https://github.com/ritheshkumar95/im2latex-tensorflow</a> link. You can find the code for preprocessing.</p><p>Finally, the whole notebook files and dataset that I use to train and evaluate are also available from the below links.</p><ul><li><a href="https://drive.google.com/file/d/1Ul9HjI4A_tTybPUGSAZwDvbsaWkd41ZF/view?usp=drive_link">latex_data.zip</a></li><li><a href="https://drive.google.com/file/d/1JhR8BWOqFatMVX84ZvVaMN_Dq8l2ZeN4/view?usp=sharing">image_to_latex(ViT).ipynb</a></li><li><a href="https://drive.google.com/file/d/1ZU1u3NnPduGHmIwiu8nSBoj5gYRPSjEq/view?usp=sharing">image_to_latex(ViT)-eval.ipynb</a></li></ul><p>Below is a pre-trained model. Please use the Tensorflow 2.13.1 version to run that.</p><p><a href="https://drive.google.com/drive/folders/173q7Q_qwlj6rkri4g3VFgHXu1_KTiZal?usp=drive_link">ViT - Google Drive</a></p><h3>Prepare Dataset</h3><p>After downloading the dataset and Notebook file, please run the below code to check whether every setting is prepared well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5890f99905d20840b6249ef0dbb87d50/href">https://medium.com/media/5890f99905d20840b6249ef0dbb87d50/href</a></iframe><p>After that, we can utilize the above code to build the data pipeline to train and test the Tensorflow model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a9960b18839888c00c401afa37ee849/href">https://medium.com/media/7a9960b18839888c00c401afa37ee849/href</a></iframe><p>Finally, check the image and label match each other using the below code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3518d544919600043d5e16142887bb1/href">https://medium.com/media/b3518d544919600043d5e16142887bb1/href</a></iframe><p>If the previous steps were done well, you can see the output in the below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pqbJx6g5S1yG8pf23bodCQ.png" /></figure><h3>Build the model</h3><p>In the original code of the image captioning tutorial of TensorFlow, the MobileNet-V3-Small is used as a feature extractor. Additionally, the pre-trained weight of the ImageNet is used to reduce the training time.</p><p>However, we can not use the model because our data is not the common object of real life. I find the Tensorflow 2.0 model for Latex images from <a href="https://github.com/harishB97/Im2Latex-TensorFlow-2">https://github.com/harishB97/Im2Latex-TensorFlow-2</a> link.</p><p>It consists of several Conv, MaxPool, and BatchNormalization networks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f4d0a83469c8319df909b75c3f97867/href">https://medium.com/media/0f4d0a83469c8319df909b75c3f97867/href</a></iframe><p>It would be a better idea to visualize the generated patches. Below is a sample code for that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f751c3231542468ca528450d7f7ca98b/href">https://medium.com/media/f751c3231542468ca528450d7f7ca98b/href</a></iframe><p>When the image size is 160x160 and the patch size is 6x6, there are a total of 26x26=676 patches.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*0cqG8QRaVf1Whu9VvcT1Sw.png" /><figcaption>Patch visualizing</figcaption></figure><p>After that, let’s verify that our feature extractor actually can extract the feature from the Latex image. We can use the previously defined Tensorflow Dataset object for this purpose.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc98339762432fc752139e9f78d2dca3/href">https://medium.com/media/bc98339762432fc752139e9f78d2dca3/href</a></iframe><p>If the ViT model was made well, the extracted feature size would be printed like the below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/185/1*UogFps_xRFMwuEHamdPJkg.png" /><figcaption>Encoded patch information</figcaption></figure><p>Value 1 is the batch size, 676 is the total number of patches, and 512 is the embedding size of the patch.</p><p>We can use encoded features in the CNN-feature extractor-based image captioning model with minimal code change of the Captioner class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*LQO2PPswgyiOsV9dKy_8VA.png" /></figure><h3>Train the model</h3><p>Additionally, we need to use the custom training and testing function because we can use the Keras <strong>fit</strong> function of the original code.</p><p>However, we can still use the <strong>masked_loss</strong> and <strong>masked_acc </strong>functions. Using them, we can define our custom training and testing function like the below one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7707ddb20bd75ddc60767e699107b6d/href">https://medium.com/media/f7707ddb20bd75ddc60767e699107b6d/href</a></iframe><p>Using the above functions and the previous TensorFlow dataset, we can train and test the Transformer model for Latex data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39619675e184841d805ba16f11b33f13/href">https://medium.com/media/39619675e184841d805ba16f11b33f13/href</a></iframe><p>The Tensorboard logs such as loss and perplexity are also saved in the tensorboard folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D6r4s306-fRMu_YaN-peWA.png" /><figcaption>Training loss of ViT extractor-based model</figcaption></figure><p>We can confirm that the training process works well from the above charts.</p><h3>Evaluate the model performance</h3><p>We can confirm the accuracy of the model reached around 96% from the Tensorboard chart. It is a not bad performance considering we train it from scratch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*M5j-WwxuO8CQhxIXXrwV_w.png" /><figcaption>Accuracy of ViT extractor-based model</figcaption></figure><p>Now, let’s print out the real label and predicted label to check the model performance more clearly. Below is a sample of that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b6f76eaaaaa56ed5221fcd11adb8e28/href">https://medium.com/media/5b6f76eaaaaa56ed5221fcd11adb8e28/href</a></iframe><p>Below are a few samples of evaluation. We can confirm that the model translates the latex image well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*T8XW98hfAMANIdRYAA9XQw.png" /><figcaption>Evaluate case 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*k4KdxDsbJ8R28hylFfWY2Q.png" /><figcaption>Evaluate case 2</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*M3S_5RaGN4apiFJJwBZQGg.png" /><figcaption>Evaluate case 3</figcaption></figure><p>You can also visualize the predicted result as an image. Below is the code for that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8a42804ac17f90b8eb04ba1437c6019/href">https://medium.com/media/a8a42804ac17f90b8eb04ba1437c6019/href</a></iframe><p>Below images are visualized predicted results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IP5XRt7gjy0-uF0dwxeaBg.png" /></figure><p>Like an original image captioning example, it is also a better idea to visualize the attention map to understand how the ViT-based model gives attention to each prediction.</p><p>We can use the default code of the original example. Below is a sample of the attention map. As you can see, it is a little bit hard to visualize it on one screen because the token number is higher than the original one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U1FEDGwh3sfbmo9YQQMgiQ.jpeg" /><figcaption>Attention maps of ViT-based model</figcaption></figure><p>If we pick up a few attention maps from the above image, we can see that our model gives attention appropriately to each image part when generating the latex token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dmTIe9WIZQ9URUt5E1x1iw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*soRIK8Ki20fmLdcuTrynbw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v5_uCf664X5gA9zhjT_-Hw.png" /></figure><h3>Compare the ViT extractor to the CNN extractor</h3><p>Because the original image captioning method used CNN as an image feature extractor, it would be helpful to compare the performance of the two models.</p><p>Below is the code of the CNN feature extractor. It is the same as previous released latex2image research.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64aa8c1694820d52b7fffccce1c8a633/href">https://medium.com/media/64aa8c1694820d52b7fffccce1c8a633/href</a></iframe><p>Below is the Tensorboard display of the loss and accuracy of both model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aHvCMqHXZflDgKOt2bA_ZQ.png" /><figcaption>Performance comparison of ViT-based and CNN-based model for Latex transcription.</figcaption></figure><p>We can confirm that the ViT-based model learns faster than the CNN-based model and expect that accuracy is also higher.</p><h3>Conclusion</h3><p>Vision Transformer is better than CNN for the image to latex applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13fc4ce253d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Playing Sonic The Hedgehog 2 using Deep Learning — Part 1]]></title>
            <link>https://pub.aimind.so/playing-sonic-the-hedgehog-2-using-deep-learning-part-1-5e16c8fa802d?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e16c8fa802d</guid>
            <category><![CDATA[deeplearing]]></category>
            <category><![CDATA[sonic]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Thu, 31 Aug 2023 23:50:38 GMT</pubDate>
            <atom:updated>2023-09-05T14:52:14.373Z</atom:updated>
            <content:encoded><![CDATA[<h3>Playing Sonic The Hedgehog 2 using Deep Learning — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*xuUiIiZ-TOfVF0sirzNE-Q.png" /><figcaption>Poster of Sonic TheHedgehog Movie 2020</figcaption></figure><p>The 2D side-scrolling video games like Sonic are suitable environments to test the performance of AI agents because it has various obstacles, enemies, and puzzle elements.</p><p>From April 5 to June 5, 2018, the OpenAI held an AI agent competition called a Retro Contest to create this Sonic game. Thankfully, they released an API in the form of OpenAI Gym so everybody can run the Sonic game using Python.</p><p>The result of that competition is available at <a href="https://openai.com/blog/first-retro-contest-retrospective/">https://openai.com/blog/first-retro-contest-retrospective/</a>.</p><p>I started to use the Sonic environment again for research because it requires a relatively simple action compared to Minecraft but the tasks to be handled are similar.</p><p>The source code of this post is available from <a href="https://github.com/kimbring2/SonicTheHedgehog2-Tensorflow2">my GitHub</a>.</p><h3>Network for Sonic</h3><p>The actions of Sonic consist of key combinations from[“B”, “A”, “MODE”, “START”, “UP”, “DOWN”, “LEFT”, “RIGHT”, “C”, “Y”, “X”, “Z”]. Among them, the number of combinations that are actually used in human playing is 23.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*te46XdXQcgrhCDbzKPSl3A.png" /><figcaption>Agent Network Version 1</figcaption></figure><p>Otherwise, the screen frame is used for state after being resized to (84x84x3).</p><h3>Training Method</h3><p>In the case of the spin dash action of the Sonic game, it is quite complicated to be randomly generated from nowhere.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*N1Usk8iykDDtgqqPCfvoYA.gif" /><figcaption>Spin dash action of Sonic(From the author&#39;s playing)</figcaption></figure><p>Therefore, it is also unreasonable to train the agent with only Deep Reinforcement Learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8oVZiF-XK1nKTrqngTLSTA.png" /><figcaption>Loss for Sonic agent</figcaption></figure><p>First, the agent is trained by the Supervised Learning method using human expert data. Supervised Learning is not able to deal with every situation of the game. Thus, the trained model is used as an aid when running Deep Reinforcement Learning.</p><p>In the case of Deep Reinforcement learning, the <a href="https://www.deepmind.com/publications/impala-scalable-distributed-deep-rl-with-importance-weighted-actor-learner-architectures">IMPALA</a> method in which one Learner is trained by the data passed from multiple Actors to speed up the training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-5YqsjjIsk_EYTs9zXY0qg.png" /><figcaption>Training method for Sonic agent</figcaption></figure><p>Due to the long steps of the Sonic game, which is usually over 10000 steps, the data of the Actor is divided into 100 lengths. Therefore, the Learner has the code for off-policy corrections.</p><h3>Collect the Human Expert Dataset</h3><p>Unlike <a href="https://openai.com/blog/first-retro-contest-retrospective/">previous competitions</a> where boss fights were excluded and only one level was tested, this series includes boss fights, and all available levels of the Sonic 2 game to make a complete agent.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FLnp8KadJW2U%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DLnp8KadJW2U&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FLnp8KadJW2U%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/11971ad1fec1bea284953b938d733b5f/href">https://medium.com/media/11971ad1fec1bea284953b938d733b5f/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FtXPcWOsnqkI%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DtXPcWOsnqkI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FtXPcWOsnqkI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/24eb2c803aabb58fa80289627ceb4470/href">https://medium.com/media/24eb2c803aabb58fa80289627ceb4470/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FPZlf6hVbrbU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DPZlf6hVbrbU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FPZlf6hVbrbU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7ef459313a3d16767f54e2518c7b1ac/href">https://medium.com/media/e7ef459313a3d16767f54e2518c7b1ac/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FfuBFcSBd8v8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DfuBFcSBd8v8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FfuBFcSBd8v8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a8d258d265f271b6b94bc70a15ae06f/href">https://medium.com/media/3a8d258d265f271b6b94bc70a15ae06f/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FVTyPPJThAqM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVTyPPJThAqM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVTyPPJThAqM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/967ab126be2f38f43a36172997bd3c89/href">https://medium.com/media/967ab126be2f38f43a36172997bd3c89/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1M-jv8GW4bc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1M-jv8GW4bc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F1M-jv8GW4bc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/70df55df9912a7c2746d1c78f2de05e8/href">https://medium.com/media/70df55df9912a7c2746d1c78f2de05e8/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FGo2Pb0QsGxo%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DGo2Pb0QsGxo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FGo2Pb0QsGxo%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/28412ac42f7c1a5465ee5b759ba1c887/href">https://medium.com/media/28412ac42f7c1a5465ee5b759ba1c887/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FPIgRHwKMEuQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DPIgRHwKMEuQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FPIgRHwKMEuQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/98ffaf4f2ed424c025233cc43b5576c8/href">https://medium.com/media/98ffaf4f2ed424c025233cc43b5576c8/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FhdTzAxawO1U%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DhdTzAxawO1U&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FhdTzAxawO1U%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6990fae69a23114427d8f52b3eb334ed/href">https://medium.com/media/6990fae69a23114427d8f52b3eb334ed/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fh3dkyROZRQg%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dh3dkyROZRQg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fh3dkyROZRQg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/981c2166fe9e902f67fbfd0d79c31ffa/href">https://medium.com/media/981c2166fe9e902f67fbfd0d79c31ffa/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F8EfG5IDBZGU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D8EfG5IDBZGU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F8EfG5IDBZGU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa6c1dbb08f067d70b0b3af85429fbdd/href">https://medium.com/media/aa6c1dbb08f067d70b0b3af85429fbdd/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FaNRvSk2Ylg8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DaNRvSk2Ylg8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaNRvSk2Ylg8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/afaabba645b7436554a29e4001ca0d79/href">https://medium.com/media/afaabba645b7436554a29e4001ca0d79/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FDQmAY0ekLSE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DDQmAY0ekLSE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FDQmAY0ekLSE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeb41cbfc980388d814de4f6be23ec03/href">https://medium.com/media/aeb41cbfc980388d814de4f6be23ec03/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F3iUufdoIgb0%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3iUufdoIgb0&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F3iUufdoIgb0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b98859bd08a56634f6f2749166b29ad3/href">https://medium.com/media/b98859bd08a56634f6f2749166b29ad3/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbG13CWnB3-Q%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbG13CWnB3-Q&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbG13CWnB3-Q%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7823ec11dcef49e7c31b7a8f2f6e8886/href">https://medium.com/media/7823ec11dcef49e7c31b7a8f2f6e8886/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FDDy2mG8x8kw%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DDDy2mG8x8kw&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FDDy2mG8x8kw%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3128a38e8f6bdbc2b484ffc485e46e95/href">https://medium.com/media/3128a38e8f6bdbc2b484ffc485e46e95/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F9tJEpHFq6T4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D9tJEpHFq6T4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F9tJEpHFq6T4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6dbc354fbb0d259e3e4fb69c8b9748fa/href">https://medium.com/media/6dbc354fbb0d259e3e4fb69c8b9748fa/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fxu9ctWTYGr0%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dxu9ctWTYGr0&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fxu9ctWTYGr0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c247a14bd9d51e6cfe04f13af2b04960/href">https://medium.com/media/c247a14bd9d51e6cfe04f13af2b04960/href</a></iframe><p>All levels get harder toward the end, especially the last level requires extremely hard control. All datasets are available from <a href="https://drive.google.com/drive/folders/1xmhYnhjeELmnWxPYa7oRgQmrHOldCgtJ">my Google Drive</a>.</p><h3>Parsing the Human Expert Dataset</h3><p>For ease of training, using only one action network is good for agents. In such cases, we must manually convert the key combinations used in the human expert data into single integer numbers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c04a83d2d643989ecfeda383a710a9e/href">https://medium.com/media/1c04a83d2d643989ecfeda383a710a9e/href</a></iframe><p>After analyzing all human expert data, a total of 23 actions are used as shown in the table above. Therefore, the number of outputs in the action network should be 23.</p><h3>Training result — <strong>Emerald Hill Zone.Act 1</strong></h3><p>Before training the agent versus the harder level, the easiest level is used to confirm every code works well as we plan. First, we can confirm that loss of Supervised Learning decreases well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*dFT6U8MsHMaiDG6vY35Ajw.png" /><figcaption>The Supervised Learning Result</figcaption></figure><p>The next step is playing the game from the saved model from Supervised Learning. It shows that for the simplest tasks, the Agent can finish the stage without further training by Reinforcement Learning.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FRB8BfL6ze1M%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DRB8BfL6ze1M&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FRB8BfL6ze1M%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b602be3f809e94497876eb2adbcffb36/href">https://medium.com/media/b602be3f809e94497876eb2adbcffb36/href</a></iframe><p>The following graph shows the results of training by the Reinforcement Learning method. After a certain number of episodes, the agent starts to clear the stage with a high probability of over 90%.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*_eP7yEOLpheudWBXXiz2ZQ.png" /><figcaption>The Reinforcement Learning Result</figcaption></figure><p>The Agent using the trained model from Reinforcement Learning is also able to clear the stage smoothly.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FoRga2n5_Ivo%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DoRga2n5_Ivo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FoRga2n5_Ivo%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9c9b7b24c06cfe07a55911ae490171e/href">https://medium.com/media/b9c9b7b24c06cfe07a55911ae490171e/href</a></iframe><h3>Training result — Emerald Hill Zone.Act 2</h3><p>An agent can clear Act 1 easily by learning the dash action properly. For that reason, there was no need to add more networks to the model that is composed of CNN + LSTM like a <a href="https://medium.com/@dohyeongkim/deep-q-learning-from-demonstrations-dqfd-for-minecraft-tutorial-1-4b462a18de5a">Minecraft TreeChop case</a>.</p><p>However, that method did not work well in Act 2 because the boss appeared at the end of the episode. After a long testing time, it is confirmed that more network is needed to clear this level because of the long jump action of Sonic game. To do the long jump, the jump key should be pressed continuously over a specific time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*hX8i3M4namOCLcDTKEEszg.gif" /><figcaption>Various jump actions of Sonic(From the author’s playing)</figcaption></figure><p>However current network can not learn how long the same action needs to be continued because it only uses the game screen as the state.</p><p>One additional problem is that the training speed is really slow. It took almost 7 days to learn the 10,000-step episode. As confirmed in <a href="https://medium.com/@dohyeongkim/playing-the-pong-game-using-cnn-not-dnn-c9d1ff68394">another test</a>, this was a problem that occurred when CNN was used directly as a feature extractor.</p><p>To solve the two problems mentioned above, two networks are added to the existing model. The first is a CVAE network that learns game screen features separately aside from the policy network. The other is a CNN network that uses the history of previous actions as the input state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bda6d36d58eb6a62d2c136ad32a4eeef/href">https://medium.com/media/bda6d36d58eb6a62d2c136ad32a4eeef/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nXm2bEu6sR_gSfHIu7GIJA.png" /><figcaption>Model architecture for using the action history and CVAE</figcaption></figure><p>The network code is available from <a href="https://github.com/kimbring2/SonicTheHedgehog2-Tensorflow2/blob/main/network.py">here</a>.</p><p>The results of reconstructing the game screen using the learned CVAE network are as follows. It seems like that most game screen information could be trained.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*O-qyQDNWIvu9c8qjpM4h6Q.gif" /><figcaption>Reconstructed image from CVAE</figcaption></figure><p>Below is the result of the training loss. It was confirmed that both Policy loss and CVAE loss decreased as training progressed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wC8SnfwXGEpyN0id0K9yKA.png" /><figcaption>Training result when using the action history as input</figcaption></figure><p>As a result of the evaluation using the weight of the saved model, the agent can beat up the boss well even without the help of Reinforcement Learning.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fm_7nV1yfmQ8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dm_7nV1yfmQ8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fm_7nV1yfmQ8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7374754b73aef026911fe050d4273699/href">https://medium.com/media/7374754b73aef026911fe050d4273699/href</a></iframe><p>Additionally, you can check the effect using action history as input for training by comparing it with the case with non-action history training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*68vIWx3H6b3OtQhkBzdxPQ.png" /><figcaption>Effect of using the action history as input for training loss</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbyrWu0LwCBE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbyrWu0LwCBE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbyrWu0LwCBE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf0815f905ebbd7a7c8e16b8a16fb961/href">https://medium.com/media/bf0815f905ebbd7a7c8e16b8a16fb961/href</a></iframe><p>Like an act 1 training result, the agent can learn the dash action, but it is not possible to get close to the boss at the same time as the action history case.</p><h3>Conclusion</h3><p>In this post, we learned about the basic methods of collecting data to clear all stages of the Sonic game through Deep Learning. It was confirmed that, unlike games such as Minecraft, it is essential to use action history as a state in order to perform complex actions of the Sonic game.</p><p>In the case of Sonic games, as the state level increases, more precise control is required. In the next post, we will check whether ChemicalPlantZone can be cleared using the existing method.</p><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e16c8fa802d" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/playing-sonic-the-hedgehog-2-using-deep-learning-part-1-5e16c8fa802d">Playing Sonic The Hedgehog 2 using Deep Learning — Part 1</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Playing Pong With Vision Transformer]]></title>
            <link>https://pub.aimind.so/playing-pong-with-vision-transformer-dd8818b2ccba?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd8818b2ccba</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[tensorflow]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Thu, 10 Aug 2023 08:16:30 GMT</pubDate>
            <atom:updated>2023-08-11T12:59:10.619Z</atom:updated>
            <content:encoded><![CDATA[<p>It has been proven that Transformer can be applied to images as well as NLP from <a href="https://arxiv.org/abs/2010.11929">An Image is Worth 16x16 Words</a> paper. Furthermore, there is a Tensorflow implementation version(<a href="https://keras.io/examples/vision/image_classification_with_vision_transformer/">Image classification with Vision Transformer</a>) code for that method.</p><p>The concept of this method is really simple. It divides the image into several patches and embeds them, arranges them like words in an NLP sentence, and uses them as input to the Transformer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*BGA6c0-jt5PD98zKF3CYVg.png" /><figcaption>Image patches example of Vision Transformer for Image Classification</figcaption></figure><p>However, this method has not yet been used well in the Reinforcement Learning environment, and its use can be confirmed for the first time in DeepMind’s <a href="https://openreview.net/pdf?id=1ikK0kHjvj">A Generalist Agent</a> paper. In this paper, they showed how to process the input of the Pong game with a Transformer instead of a CNN for mixed use with NLP data and the training results are also good.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TwjUJDIbkjs6eiZl6rXMIQ.png" /><figcaption>Image patches example of Vision Transformer for game</figcaption></figure><p>The training code for this tutorial is available <a href="https://drive.google.com/file/d/1xGgYSuzEBhqHZI1EbGeYNfFO9GEhV3Px/view?usp=sharing">here</a>. You need to change some paths according to your folder setting!</p><h3>Human Expert Data</h3><p>Because these methods are <a href="https://arxiv.org/abs/2005.01643">Offline Reinforcement Learning</a> that pre-train agents with Supervised Learning first with expert data before allowing them to interact with the simulation environment.</p><p>The code for generating expert data is as follows. After downloading the file, let’s download the pre-trained weight file from the <a href="https://drive.google.com/drive/folders/1_dWkXvwVy7FlOLBivZqL61lqn_d2qxzz?usp=drive_link">author’s Google Drive</a>. Or you can train the model from scratch using <a href="https://drive.google.com/file/d/1w5PuBuMZFYVX9CoEZ8JKbb7O538xZsx2/view?usp=sharing">this code</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb68f4ebbb580fa9acf8b0553c0991d6/href">https://medium.com/media/eb68f4ebbb580fa9acf8b0553c0991d6/href</a></iframe><h3>Vision Transformer for Pong game</h3><p>Only the input data stream and PatchEncoder parts of the previously published Vision Transformer code are modified to fit the Pong game, and all other parts are reused.</p><p>First, load the expert data generated above in the form of a Numpy array. Because it is Supervised Learning, only the state and action parts of data are used, and reward and done data are not needed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c81ffc7c315067bd5bb79d0ae95b4e67/href">https://medium.com/media/c81ffc7c315067bd5bb79d0ae95b4e67/href</a></iframe><p>Next, let’s divide the loaded state image data into a number of patch images. Unlike the existing image classification, the image is converted to gray and used in Pong. All other parts are the same as the original code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27f21002552235124e464ca5f167e9a4/href">https://medium.com/media/27f21002552235124e464ca5f167e9a4/href</a></iframe><p>If you run the code, you can visually see that the screen of the Pong game is divided into several patches as shown below. Each patch is embedded through MLP, the same method as the existing code, and finally used as input to the transformer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*XHFnkrQlzyS_4FVhDA6yrQ.png" /><figcaption>Splitting the game screen into multiple patches</figcaption></figure><p>Then, train the Vision Transformer model using the prepared input data. The existing code can use the Keras.model.fit function because of a built-in CIFAR-10 dataset. However, we should use a little more complex data pipeline because we created our own dataset.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb515addd732078b8ab6aef124447bfd/href">https://medium.com/media/eb515addd732078b8ab6aef124447bfd/href</a></iframe><p>In the case of loss, the previously used Sparse Categorical Crossentropy can be used as it is because the output of the model and the action of the expert are compared. Additionally, the weights of the model are saved at each epoch for evaluation after training is over.</p><p>If you run the code and look at the change in loss with Tensorboard, you can see that the loss decreases as the epoch passes as shown below. The trend of the corresponding loss means that the training went well by means of it is Supervised Learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*1aEWXfdAm76tBcPJ-ztCJA.png" /><figcaption>Training result of Vision Transformer for Pong game</figcaption></figure><p>Finally, load the saved weights and evaluate the performance of the model with a real Pong game. Create a new Pong environment using the Gym as below, and decide the action using the trained model. You can also download the training model from <a href="https://drive.google.com/drive/folders/1qjssjwLN6E_OV7sHHI8wIDM_7Z6GUYaY?usp=sharing">Google Drive</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/710939aad19e0a63d63671ef4f978ca0/href">https://medium.com/media/710939aad19e0a63d63671ef4f978ca0/href</a></iframe><p>As a result of the test, I can see that the Vision Transformer plays the Pong game from expert data as shown below. From this, we can see that the Transformer can be used not only for image classification tasks but also for games.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*tvIzpzXCi8n1lLYZwk-tNA.gif" /><figcaption>Evaluation result of Vision Transformer for Pong game</figcaption></figure><h3>Conclusion</h3><p>Of course, we do not need to use the Transformer in the case of the Pong game because CNN or MLP is enough to train the model successfully. However, there is a need to solve pixel-based state games using vision transformers since the recent trend of research is moving toward reducing the overall model size by solving various tasks simultaneously with one Transformer model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DmGITdjILpyMDIg1nnpJzA.png" /><figcaption>DeepMind Gato Architecture</figcaption></figure><p>This method can be useful in developing an agent in a high-level environment such as an <a href="https://kimbring2.github.io/uoservice/">MMORPG</a> game, where NLP and game control tasks are combined together.</p><p>Thank you for reading! I will share my work about the Gato model soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd8818b2ccba" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/playing-pong-with-vision-transformer-dd8818b2ccba">Playing Pong With Vision Transformer</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Playing the Pong game using CNN, not DNN]]></title>
            <link>https://dohyeongkim.medium.com/playing-the-pong-game-using-cnn-not-dnn-c9d1ff68394?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9d1ff68394</guid>
            <category><![CDATA[games]]></category>
            <category><![CDATA[reinforcement-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Thu, 04 May 2023 08:28:39 GMT</pubDate>
            <atom:updated>2023-05-04T08:28:39.441Z</atom:updated>
            <content:encoded><![CDATA[<p>The Pong game has been used widely to measure the performance of Reinforcement Learning. In most of that examples, the DNN policy works well even if the observation is the image frame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/1*05ExQKJ0nOoWV80SNVEyJg.png" /><figcaption>DNN policy example for the Pong game. From the <a href="http://karpathy.github.io/2016/05/31/rl/">legacy blog of Karpathy</a>.</figcaption></figure><h3>CNN vs DNN comparison at image classification task</h3><p>However, DNN is not a choice when dealing with image data because it can not learn spatial information. You can easily know it from the <a href="https://www.semanticscholar.org/paper/Tensorflow-Based-Image-Classification-using-Neural-Kumar-Dugal/b35e1154af73b9f1e36fac283cd89837f4e09f49">MNIST classification experiment</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*BZmUR4FWa7FxlIZHhDdUhA.png" /><figcaption>The comparison result of DNN and CNN</figcaption></figure><p>Based on that result, we should obtain a good result when using CNN than DNN.</p><h3>Train the RL using CNN policy</h3><p>Let’s train our CNN model and see the result using the below code.</p><p>The reason why I want to use CNN in Pong is that there is a need to learn more about various movements for the <a href="https://pettingzoo.farama.org/environments/atari/pong/">multiplayer version of Pong</a>. The opponent of single-player pong has a very simple mechanism. Therefore, it can be defeated using the DNN.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/635d3b027807fb35031815b69bd10ee1/href">https://medium.com/media/635d3b027807fb35031815b69bd10ee1/href</a></iframe><p>Interestingly, the results using CNN are really poor than DNN. Why doesn’t CNN work like classification in RL?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VMpW4c7N8XmfcfM1sY1BYQ.png" /><figcaption>The training result of CNN policy</figcaption></figure><p>Based on <a href="https://arxiv.org/pdf/1910.01741.pdf">this paper,</a> the main reason for that problem is that CNN needs to learn good representations (upon which a policy can be trained), they require large amounts of training data.</p><p>Recently, a method of adding a Variational Autoencoder when learning has been used to solve the problem of using CNNs in such RL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QZgAR4ZvUROe4EiI2IDzGg.png" /><figcaption>VAE architecture from <a href="https://data-science-blog.com/blog/2022/04/19/variational-autoencoders/">this blog post</a></figcaption></figure><p>To implement this, the encoded feature of VAE is used as an input of RL. Additionally, the reconstruction loss of VAE is added to the loss of RL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tysxdp-vhzJXBTo38VlwMw.png" /><figcaption>The network architecture of the <a href="https://openreview.net/pdf?id=wpSWuz_hyqA">DeepMind paper</a></figcaption></figure><h3>Train the RL using the CVAE policy</h3><p>Let’s add the CVAE network to the previous code. The <a href="https://www.tensorflow.org/tutorials/generative/cvae">official tutorial code</a> of Tensorflow is used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23cce47781d36b664bdd824e46fbd5bb/href">https://medium.com/media/23cce47781d36b664bdd824e46fbd5bb/href</a></iframe><p>Interestingly, the result using CVAE is much better than those of CNN.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HkX7pwC-cKVRboC3zJL6jw.png" /><figcaption>The training result of the CVAE policy</figcaption></figure><p>Let’s look at the reconstructed image per the training step to confirm that CVAE learn the representation of an image frame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*huLg60w-zDhpLtHCMeI76w.gif" /><figcaption>Learning screen representations using CVAE</figcaption></figure><p>Even if the learning is not perfect yet, the CVAE can reconstruct the shape of the peddle on both sides of the Pong game more and more over training time.</p><h3>Conclusion</h3><p>In this post, we looked at how to use the CVAE policy in RL when the input is an image frame. As can be seen from the experimental results, it is also important to train the representation of the image itself. In the next post, we will learn to use the method in multi-player Pong games where the movement of agents should be more complex than in single-player games.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9d1ff68394" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementation of the Hide and Seek of the OpenAI — Part 1]]></title>
            <link>https://dohyeongkim.medium.com/implementation-of-the-hide-and-seek-of-the-openai-part-1-3e1b9eeb86cf?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e1b9eeb86cf</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[reinforcement-learning]]></category>
            <category><![CDATA[multi-agent-coordination]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[games]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Sun, 12 Mar 2023 09:48:41 GMT</pubDate>
            <atom:updated>2023-03-12T09:48:41.070Z</atom:updated>
            <content:encoded><![CDATA[<h3>Implementation of the Hide and Seek of the OpenAI — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*-thVG9SJKtp7NrHEF9OUrA.jpeg" /><figcaption>Image from <a href="https://iar.ac.in/research-innovation/consultancy-colloboration/">Iar</a> site</figcaption></figure><p>Collaboration is an essential function of multiplayer game such as a MOBA, and Soccer game. In the case of Reinforcement Learning, the transition probabilities should be stationary in order to be trained well. Due to this point, <a href="https://openai.com/research/learning-to-cooperate-compete-and-communicate">famous early study</a> of the OpenAI tried to apply a additional method to deal with the fluctuating transition probabilities.</p><p>However, <a href="https://www.deepmind.com/blog/emergent-bartering-behaviour-in-multi-agent-reinforcement-learning">recent research of the DeepMind</a> for MARL say that multiple agent game also can be converged to the Nash Equilibrium despite of unstable transition probability.</p><blockquote>In theory such multi-agent systems may continue to explore forever. In<br>practice they often reach an equilibrium point and stop exploring. Efforts to understand how these systems work create tension with the dominant “single-agent paradigm” of artificial intelligence and cognitive science. In short, all the representations that matter are no longer inside the agent’s “head”, but rather are distributed in some fashion between the agent, the population, the environment, and the training protocol itself.</blockquote><h3>Hide and Seek Environment</h3><p>In this series, the <a href="https://github.com/openai/multi-agent-emergence-environments">Multiagent Emergence Environments</a> of OpenAI announced in 2019 will be used. That environment is more difficult because it has a higher state and action dimension than the <a href="https://github.com/openai/multi-agent-emergence-environments">previous MARL environment</a> of same organization.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fu9QB8G97lVY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Du9QB8G97lVY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fu9QB8G97lVY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0691b61f8ee3a0b3e5b22b46e1fc7dc9/href">https://medium.com/media/0691b61f8ee3a0b3e5b22b46e1fc7dc9/href</a></iframe><p>The weight of the trained model is provided by the OpenAI , and as shown in the video above. You can see that agents from home team collaborate to prevent agents from opponent teams from breaking inside.</p><p>If you take a deeper look into the <a href="https://arxiv.org/pdf/1909.07528.pdf">related paper</a>, you can know that agent does not learn such cooperation behavior is not emerged at once, but through several curriculum.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-hJl8H8YQO2C0di6-3bJ0g.png" /><figcaption>Autocurricula of Multi Agent</figcaption></figure><p>The amazing fact about that experiment is that the agents automatically learn the corresponding processes by sharing the reward inside of same tea even if the corresponding processes are not hard-coded at the code.</p><h3>Toy Environment Test</h3><p>Before trying a complex environment, it would be good idea to experiment the the model and training method in a simple environment.</p><p>First, Tensorflow 2 and <a href="https://github.com/openai/multi-agent-emergence-environments">https://github.com/openai/multi-agent-emergence-environments</a> are installed to run the below code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09f9760a62c9cbd1f29f740cad4b2bb6/href">https://medium.com/media/09f9760a62c9cbd1f29f740cad4b2bb6/href</a></iframe><p>The <a href="https://www.tensorflow.org/guide/core/mlp_core">MLP</a>, <a href="https://spinningup.openai.com/en/latest/algorithms/ddpg.html">DDPG</a>, and <a href="https://github.com/openai/multiagent-particle-envs/blob/master/multiagent/scenarios/simple_tag.py">Simple Tag environment</a> are used for that experiment. The agents are trained for a total of 30000 episodes and evaluated for every 10000 episodes. As a result of several test, setting the gradient clipping to 0.5 and changing the discount factor from 0.99 to 0.95, show a faster training speed in a multi agent environment than single agent case.</p><p>After training 10000 episode, the predator agents only have learned to simply chase the prey agent. It can be seen that collaboration behavior has not yet been emerged.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*9jASweQq9qewa_xwWIRC3g.gif" /><figcaption>Evaluation after 10000 episode training</figcaption></figure><p>After training 20000 episodes, one agent starts to move in the different direction from the other two agents. They finally realize that these behavior is better than to obtain more reward.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*i_8RCgeeEmcGqdvthTm-UA.gif" /><figcaption>Evaluation after 20000 episode training</figcaption></figure><p>After training over 30000 episodes, the predator agents begin to drive the prey agent into a corner. In this way, they can obtain the highest reward. As you see from previous step, that kind of collaboration behavior only can be learned from the curriculum learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*x2n59fGgpOBrXUjlwvPeJA.gif" /><figcaption>Evaluation after 30000 episode training</figcaption></figure><p>As a result of the above experiment, it is confirmed that Multi Agent Reinforcement Learning is also converged like a single agent case without additional part to the RL algorithm.</p><h3>Conclusion</h3><p>In the next post, we will take a look at the training result by applying the method used in this post to the Hide and Seek environment. Something needs to be changed or added to the current model and training method in order to train properly because it is a much more complicated environment than this post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e1b9eeb86cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ChatGPT implementation series — 1]]></title>
            <link>https://dohyeongkim.medium.com/chatgpt-implementation-series-1-9caadb51060c?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/9caadb51060c</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[torch]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[chatbots]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Thu, 02 Feb 2023 09:50:02 GMT</pubDate>
            <atom:updated>2023-02-02T09:50:02.153Z</atom:updated>
            <content:encoded><![CDATA[<h3>ChatGPT implementation series — 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*zyb4Jnrd9jFt00Phr3fNmw.jpeg" /><figcaption>Image from <a href="http://the post of the mobilespoon">the post of the mobilespoon</a></figcaption></figure><p>Almost all of the world talks about the exceeding performance of ChatGPT, analysing the dataset, model, and training methods of that would be an interesting task. You can find <a href="https://github.com/openai/summarize-from-feedback">the project</a> that OpenAI used as a major reference to create ChatGPT on GitHub.</p><p>However, the code has many subfiles and was written for a distributed training environment using the <a href="https://mpi4py.readthedocs.io/en/stable/">MPI</a> which makes it impossible to run on a normal desktop like mine.</p><p>Therefore, I removed all subfiles and MPI in such milk and modified the code so that you can try sampling and evaluating the summarization each in one Jupyter Notebook conveniently.</p><p>Simplifying the code in this way would be useful for training the model from the beginning in the process of the corresponding <a href="https://arxiv.org/abs/2009.01325">paper</a>.</p><p>You need to install only the Torch, and Blobfile packages to run the code. It will help you grab the essential part of the ‘<strong>Learning to summarize from human feedback</strong>’ code and paper.</p><p>Please note that it takes about 5 minutes and several gigabytes of storage to download the pre-trained model.</p><h3>Summarize the Reddit post from the pre-trained model</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba4409cac3ae408dff3618c17fd9ecfa/href">https://medium.com/media/ba4409cac3ae408dff3618c17fd9ecfa/href</a></iframe><h3>Calculate the reward of summarization from the pre-trained model</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d79affaa5255f022a266d3e78347483/href">https://medium.com/media/2d79affaa5255f022a266d3e78347483/href</a></iframe><h3>Conclusion and Future Work</h3><p>As you can see from the Notebook results, the summarization results using the provided pre-trained model are quite accurate and almost human-like.</p><p>The score of the reward model is also considered to be quite high, and it seems to evaluate the summarization result properly.</p><p>Now we check everything works well. In the next post, we will learn how to train the model using Supervised Learning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9caadb51060c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easy explanation of the Stabilizing Transformers for Reinforcement Learning with real code]]></title>
            <link>https://dohyeongkim.medium.com/easy-explanation-of-the-stabilizing-transformers-for-reinforcement-learning-with-real-code-12a0bfc5035d?source=rss-ceb0044f7b57------2</link>
            <guid isPermaLink="false">https://medium.com/p/12a0bfc5035d</guid>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[game-ai]]></category>
            <category><![CDATA[reinforcement-learning]]></category>
            <dc:creator><![CDATA[Dohyeong Kim]]></dc:creator>
            <pubDate>Fri, 25 Nov 2022 02:30:19 GMT</pubDate>
            <atom:updated>2022-12-11T18:57:48.457Z</atom:updated>
            <content:encoded><![CDATA[<p>In my <a href="https://medium.com/codex/easy-explanation-of-relational-deep-reinforcement-learning-with-real-code-4e2455217853">previous post</a>, I explained how to use the Transformer Network to find the relationship between objects on one screen frame at once. However, we sometimes need to find the relationship between each screen frame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*awcP-vGTS-XWDtgcJAeCWg.png" /><figcaption>Obstacle Tower Environment</figcaption></figure><p>For example, Agent should pick a key before opening the door for the next mission in the <a href="https://arxiv.org/abs/1902.01378">obstacle tower environment</a>. In such cases, there is a delay between obtaining the key and opening the door. To solve that task, the Neural Network of the agent should keep the information for some time even if there is another reward signal while keeping it. It is easy problem from the perspective of a classic AI algorithm because we just need to keep that information in one variable. However, we need to use the other way if we need to train the network using backpropagation.</p><p>Many papers have been published to tackle that problem. For example, another paper of DeepMind titled <a href="https://arxiv.org/pdf/2102.12425.pdf">Synthetic Returns for Long-Term Credit Assignment</a> uses the Key-to-Door environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HgdnVfdVi75QUcktFS9itg.png" /><figcaption>Key-to-Door environment from <a href="https://arxiv.org/pdf/2102.12425.pdf">paper</a></figcaption></figure><p>As you can see, this environment is a 2D version of Obstacle Tower.</p><p>Among various methods of papers, the method of DeepMind titled <a href="https://arxiv.org/pdf/1910.06764.pdf">the Stabilizing Transformers</a> shows prominently good performance. In this post, we are going to look around that method with Tensorflow code.</p><p>I divide the <a href="https://drive.google.com/file/d/1Nis0fUTxHIHOIZDNB7dVATzl1AoYRE53/view?usp=sharing">full code source</a> of this post into small pieces to explain more easily.</p><h3>Relational Environment</h3><p>Before we use the heavy environment, it would be nice to test the model using a simple environment like the previous post. Therefore, I make the box world environment that is sequentially relational.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/64/1*LHTliFZrrbtl_tM0acJPHw.gif" /><figcaption>The visualization of Box-World environment</figcaption></figure><p>The agent will obtain the 10 rewards if it selects the red box in the first frame and the blue box in the last frame. The first and last box selections should occur together to receive the reward. Otherwise, only 0 rewards are given to the agent. Additionally, there are green boxes that will give a reward of 1 if you choose in the middle. The first and last box position is fixed to make the task easier. A network that has some memory functions is needed to run properly in that environment.</p><p>This environment only requires a basic Python package and is really light. Just copy the below code to the BoxWorldEnvMemory.py file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec2e769c5feb18f4b7cebac0016efa41/href">https://medium.com/media/ec2e769c5feb18f4b7cebac0016efa41/href</a></iframe><p>After putting the environment file in your workspace, you can test the environment in the OpenAI gym format using the below code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdfaf2c29540f520dbceffcc574ab57f/href">https://medium.com/media/bdfaf2c29540f520dbceffcc574ab57f/href</a></iframe><p>The action number and max_episode arguments are the number of places of boxes in one frame and the sequence length of each other. We can change them to test the relation capacity of the Transformer.</p><h3>Transformer-XL</h3><p>The Stabilizing Transformers uses the Transformer-XL that is similar to the classic Transformer but has a memory like an LSTM. I find a really good <a href="https://github.com/davidsvy/transformer-xl">reference code</a> for that from one of the projects to generate the text. As I run that code, it seems like the model generates more long sensible text than the LSTM-based model.</p><p>Let’s compare the Transformer-XL with the original one. We already use that to solve the stacking box environment. That can be drawn like the below image. As you can see, there is no memory transfer between the segment. That is why it can be used to find the relation of objects in one frame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mTEeLXMQ9IqWDbG02z8RVQ.jpeg" /><figcaption>Classic Transformer architecture from <a href="https://arxiv.org/abs/1901.02860">paper</a></figcaption></figure><p>Otherwise, the Transformer-XL has a memory that is transferred between each segment. Even if the LSTM has also memory, it shows poor performance compared to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hCCzO69asOFvc1abI9tq2w.png" /><figcaption>Transformer-XL architecture from <a href="https://arxiv.org/abs/1901.02860">paper</a></figcaption></figure><p>Below is the code for Transformer-XL. You can check some math functions for that in <a href="https://nn.labml.ai/transformers/xl/relative_mha.html">this post</a>. Different from vanilla Transformers, you can the rel_enc_shift function for the shifting.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c379d7ca37b9cb56b242da0bfba5d6e6/href">https://medium.com/media/c379d7ca37b9cb56b242da0bfba5d6e6/href</a></iframe><h3><strong>Gated Transformer</strong></h3><p>The next component that is needed to apply the Transformer to a sequentially relational environment is the gating layer. That can be implemented practically like the below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1f3M4jtz1q71JNRSHK7GwA.png" /><figcaption>Gated Transformer architecture from <a href="https://arxiv.org/pdf/1910.06764.pdf">paper</a></figcaption></figure><p>The paper says that placing the Layer-Norm is related to the Identity Map Reordering. Furthermore, performance is again boosted by using the Gating Layer. It is always awesome they find that kind of breakthrough.</p><p>You can also see how that theory is implemented using Tensorflow like the below code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/955e372265c1323c9befe219f54259d7/href">https://medium.com/media/955e372265c1323c9befe219f54259d7/href</a></iframe><p>Interestingly, the training does not proceed at all when the gating layer is not used.</p><h3>Relative Position Encodings</h3><p>Finally, we need to add the relative position encodings to input data. As you remember, we also use positional encoding to find the relation between the objects of one screen. The difference is that we should add it to each sequence frame.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c601e408e8dd7d815c13ff85d5f3f0eb/href">https://medium.com/media/c601e408e8dd7d815c13ff85d5f3f0eb/href</a></iframe><p>You can see how to add the position encoding to input of model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82a7bec6bf452096249847ecd9c0d218/href">https://medium.com/media/82a7bec6bf452096249847ecd9c0d218/href</a></iframe><h3><strong>Using the Stabilizing Transformers with A2C RL</strong></h3><p>The most tricky part was modifying the Transformer-XL for the NLP task to Reinforcement Learning. The original paper uses the V-MPO which is the most advanced Model-Free RL technique currently. However, we can use the A2C if we use a more simple environment. You can see how to use the Gated Transformer-XL model previously explained with A2C.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93279b01723d772fae2b192b69cb2cd2/href">https://medium.com/media/93279b01723d772fae2b192b69cb2cd2/href</a></iframe><p>It is really similar to <a href="https://drive.google.com/file/d/1VWbnPcOXvSojBXlWBAJV53DyM1M77-XP/view?usp=sharing">DNN</a>, <a href="https://drive.google.com/file/d/1oeUO8LtnBQFqEC-0zrJQoapV22b1cKsb/view?usp=sharing">LSTM</a> based A2C code except for the dimension of logits, and baselines of A2C.</p><h3>Training Result of DNN, LSTM, and Transformer</h3><p>At first evaluation, the action_num is set as 3, and max_episode is set as 5. The Transformer has 32 head sizes and 12 layers. Like an experiment of <a href="https://arxiv.org/pdf/2102.12425.pdf">Synthetic Returns for Long-Term Credit Assignment</a>, I divide the reward into the green box collection and the red/blue box together a collection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cIQ7N0umN5ny_rqO_zl29w.png" /><figcaption>Result of the first environment</figcaption></figure><p>In the first test environment, the DNN and Stabilizing Transformers can collect the green box well but LSTM fails to do that. In the task of delay reward task, only Stabilizing Transformers succeeds. This result is really similar to the result of the <a href="http://Synthetic Returns for Long-Term Credit Assignment">reference paper</a> even if the algorithm is a little different.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b3mfaefxWaIV2N-Ox9sHcA.png" /><figcaption>Result of the <a href="http://Synthetic Returns for Long-Term Credit Assignment">reference paper</a></figcaption></figure><p>We can assume that LSTM and DNN have no ability to deal with delays between actions and rewards are long and when intervening unrelated events contribute variance to long-term return.</p><p>I am still not sure why LSTM shows bad performance at the collecting apple tasks. That works really well in a single-task environment.</p><h3>Training Result of longer environment</h3><p>For now, we know that only the Stabilizing Transformer has a memory ability to remember the long-term reward. There, it is tested against the box environment that has 8 lengths. You can check my testing file <a href="https://drive.google.com/file/d/1uOl1bC7IoUTQc5-sn-NN5GJ6TKtM-Qxz/view?usp=sharing">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*I0ef-1fj3fyS92_6S9OtLA.png" /><figcaption>Result of the second environment</figcaption></figure><p>We can see that agent takes a little longer time to learn that task. We should consider that when we use a heavy environment because training time will be multiplicated to multiple scales.</p><h3>Training Result of the wider environment</h3><p>The one more thing we need to check about the Stabilizing Transformer is the number of action sizes. Does it work well when the action size is increased? As I say in the first part of this post, the number of places where boxes can be placed means action size. Let’s change it from 3 to 4 with 5 environment lengths.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Orhk8sg4Fd6l9hjk4_pJGw.png" /><figcaption>Result of the third environment</figcaption></figure><p>As you can see, the training time is affected by action size. However, the model of the agent manages to learn it.</p><h3>Conclusion</h3><p>In this post, the Stabilizing Transformer of DeepMind is tested with a simple toy environment with various parameters. The ability to integrate information over long time horizons and scale to massive amounts of data is a crucial ability for AI agents. We see that the Transformer also can be used for sequentially relational environments if we add more technique methods such as the relative position encodings, gated transformers, and identity map reordering to Transformer-XL for NLP.</p><p>The future work is applying that to the Obstacle Tower environment and comparing the result with <a href="https://blog.aqnichol.com/2019/07/24/competing-in-the-obstacle-tower-challenge/">the PPO and BC based SOTA algorithm</a>.</p><p>Thank you for reading (:</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12a0bfc5035d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>