<?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 Henry Mao on Medium]]></title>
        <description><![CDATA[Stories by Henry Mao on Medium]]></description>
        <link>https://medium.com/@henrymao?source=rss-c6cf0c257ce2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*m39fO6oY3Kqz66yX.</url>
            <title>Stories by Henry Mao on Medium</title>
            <link>https://medium.com/@henrymao?source=rss-c6cf0c257ce2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 14:13:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@henrymao/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[Applying Deep Learning to Puzzle Level Design]]></title>
            <link>https://medium.com/@henrymao/applying-deep-learning-to-puzzle-level-design-15e9372b562?source=rss-c6cf0c257ce2------2</link>
            <guid isPermaLink="false">https://medium.com/p/15e9372b562</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[reinforcement]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Henry Mao]]></dc:creator>
            <pubDate>Sat, 28 Jan 2017 20:11:41 GMT</pubDate>
            <atom:updated>2017-03-06T04:41:58.606Z</atom:updated>
            <content:encoded><![CDATA[<p>Over the winter break, my team at <em>Calclavia</em> started working on a mobile game called Relay. Relay is a puzzle/arcade game where the goal is to <em>relay</em> a circle to a goal block (now available on <a href="https://itunes.apple.com/us/app/relay/id1203089200">App store</a> and <a href="https://play.google.com/store/apps/details?id=com.calclavia.relay">Google play</a>!). As you swipe to change the circle’s direction, the circle moves faster, making it harder to swipe on time. If you hit any walls, you lose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*lOK5WJCQwAappS8iDeJU5w.gif" /><figcaption>A level in Relay</figcaption></figure><p>One of Relay’s key feature is that every level you play is different and tailored to your skill level, giving you a unique play experience every time you come back to the game. Traditionally, game designers would create a set number of levels and release it along with the game. However, our vision for Relay was to create a more personalized and refreshing experience. Our design posed an engineering challenge: how can we automatically design levels in a game that are fun and engaging?</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbtQIzvdUlGY%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbtQIzvdUlGY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbtQIzvdUlGY%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/256c26765800fd3fbe992fbb08b6c0c8/href">https://medium.com/media/256c26765800fd3fbe992fbb08b6c0c8/href</a></iframe><h4>Reinforcement Learning</h4><p>Most games that create “infinite” content use sophisticated algorithms. Minecraft is a game that creates procedural terrains so you have an infinitely large world to explore. Instead of using an algorithm to generate a level in the game, we used a novel approach by training an AI to <em>be </em>the level designer. Unlike an algorithm where the programmer explicitly gives rules to create a level, our AI learns how to design levels through experience. We used deep <a href="https://medium.com/@henrymao/reinforcement-learning-using-asynchronous-advantage-actor-critic-704147f91686#.ozhcr9qne">reinforcement learning</a> as a means to train the AI to design levels that match desired qualities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/0*XEspTWi2QVx4vVJa." /></figure><p>I like to think of reinforcement learning like training a rat to do a task. Let’s say you want to train the rat to press a lever whenever it hears a bell ring. You would give the rat a reward (cheese) whenever it does something right or a punishment (an electric shock) when it does something wrong. (Don’t worry, no rats were hurt to make this game!) After making many mistakes, the rat eventually learns to pull the lever. In psychology this is known as <em>conditioning</em>.</p><p>Similarly, we created a reward/punishment environment to train our AI level designer for Relay. Whenever the AI designs a good level, we reward it. Whenever it designs a poor level, we punish it.</p><p>This raises a tough question: what defines a good level?</p><h4>Level Design</h4><p>A <a href="https://www.youtube.com/watch?v=5LScL4CWe5E">video from Extra Credits</a> states that pacing in a game should resemble pacing in a story — have some kind of exposition, rising action, climax and resolution. The idea is that humans become acclimated to stimuli and the most interesting experiences come from an oscillation in the amount of engagement an entertainment provides.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/0*Zk0B3lrp5DuQZWKC." /><figcaption>Pacing curve for Star Wars</figcaption></figure><p>I took this advice and created a reward system such that whenever the AI follows this pacing curve in its design, we gave it a reward. Pacing in Relay comes in the form of swipes the player has to make in succession to reach a goal. The game becomes more intense as the circle moves faster and the player has to swipe faster. Based on this knowledge, I designed the system so that it rewards levels where the number of swipes needed increases until a certain climax, then decreases so the player has a chance to relax before starting over in the engagement curve.</p><p>Surprisingly, this reward system worked remarkably well. The AI learned a design philosophy and created interesting levels. I took this system a bit further and trained the AI to create different levels based on a desired difficulty, allowing each player to get levels based on their skill in the game.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*HbJWEllS1-OUarDE6Z4_Gg.gif" /><figcaption>Notice how the AI designed the level so that the first two swipes had to be made in rapid succession (exposition). The following two swipes were then designed to be easier and gave the player more time to prepare (rising action). The last several swipes had to be made quickly and were quite intense, serving as a climax for the level.</figcaption></figure><p>In addition to designing an environment, we also had to design a deep learning model architecture that is small enough to run in JavaScript using <a href="https://github.com/transcranial/keras-js">Keras.js</a>. To learn more about the model architecture we used, I suggest checking out our <a href="https://github.com/calclavia/relay-generator">Github repository</a>.</p><h4>Closing Thoughts</h4><p>Relay is definitely not the first game out there to use AI, but I think as machine learning techniques advance, it definitely opens a whole new level of AI that can be applied to games. Just imagine what it will be like if characters in RPG games employ natural language processing to understand what you say to curate a better response! As a developer, I’m definitely going to look more into applying machine learning techniques for my future projects.</p><p>If you want to check out the levels in Relay, you can download Relay from the <a href="https://itunes.apple.com/us/app/relay/id1203089200">App store</a> or on <a href="https://play.google.com/store/apps/details?id=com.calclavia.relay">Google play</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15e9372b562" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reinforcement Learning using Asynchronous Advantage Actor Critic]]></title>
            <link>https://medium.com/@henrymao/reinforcement-learning-using-asynchronous-advantage-actor-critic-704147f91686?source=rss-c6cf0c257ce2------2</link>
            <guid isPermaLink="false">https://medium.com/p/704147f91686</guid>
            <category><![CDATA[a3c]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[reinforcement-learning]]></category>
            <category><![CDATA[deepmind]]></category>
            <dc:creator><![CDATA[Henry Mao]]></dc:creator>
            <pubDate>Tue, 24 Jan 2017 07:15:49 GMT</pubDate>
            <atom:updated>2017-01-24T23:29:19.331Z</atom:updated>
            <content:encoded><![CDATA[<p>In my research, I stumbled upon an effective learning method called Asynchronous Advantage Actor Critic (A3C) published by DeepMind. This algorithm beats the famous DQN by quite a margin and also seems to yield more stable results. I wanted to give a high level explanation in this post of how the algorithm works, hopefully inspiring more people to apply it in their projects. If you’re interested in the code, I implemented <a href="https://github.com/calclavia/rl">the algorithm</a> using Tensorflow and Keras inspired by this Medium <a href="https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part-8-asynchronous-actor-critic-agents-a3c-c88f72a5e9f2#.dgiztjv7l">article</a>. The library is compatible with <a href="http://gym.openai.com/">OpenAI’s Gym API</a>.</p><h3><strong>Actor Critic Models</strong></h3><p>Before we dive into the asynchronous part, I’d like to explain Actor-Critic (AC) learning models. In a reinforcement learning problem, an agent exists in some state <em>s</em> and tries to choose an action <em>a</em> to maximize its discounted future rewards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/0*Rda_s6qNiZhxmQEJ.png" /></figure><p>The AC agent is comprised of an actor and a critic. The actor attempts to learn a policy <strong>π</strong><em>(s) </em>(AKA the rule that the agent follows) by receiving feedback from a critic. The critic learns a value function <em>V(s) </em>(the expected return in rewards), which is used to determine how advantageous it is to be in a particular state. The advantage is defined as A(s) = Q(s, a)-V(s). In practice, we don’t want to compute <em>Q(s, a)</em>. Instead, we formulate an estimate of the advantage function as <em>A(s) = r+γV(s’) - V(s), </em>where<em> r </em>is the current reward and <em>γ </em>is the discount factor. This achieves the same result without needing to learn the Q function. An even more effective method would be to use a <a href="https://arxiv.org/pdf/1506.02438.pdf">generalized advantage function</a>.</p><p><strong>Objective Functions</strong></p><p>Looking at the actor-critic agent from a neural network perspective, we would give the agent two outputs: value and policy. The value output predicts a scalar that learns the value function <em>V(s)</em>. The policy output π(s) (softmax activation) is a vector that represents a probability distribution over the actions. We pick the action non-deterministically by sampling from this probability distribution. We denote π(a | s) as the probability of the sampled action <em>a </em>given state <em>s</em>.</p><p>We arrive at the following loss functions (we want to minimize these). <em>R</em> represents the discounted future reward (R = <em>r+γV(s’))</em>.</p><blockquote><em>Value Loss: L = Σ(R - V(s))² (Sum Squared Error)</em></blockquote><blockquote><em>Policy Loss: L = -log(</em><strong><em>π</em></strong><em>(a | s)) * A(s)</em></blockquote><p>But not so fast! While the loss functions above would work, it is better to introduce the entropy H(π) to the equation.</p><blockquote><em>H(π)=-Σ(P(x) log(P(x))</em></blockquote><p>Entropy is a measure of how spread out the probabilities are. The higher the entropy, the more similar each action’s probability will be, which makes the agent more <em>uncertain</em> about which action to choose. Entropy can be added to the loss function to encourage exploration by preventing the agent from being too decisive and converging at local optima.</p><blockquote>Policy Loss: L = -log(π(a | s)) * A(s) - β*H(π)</blockquote><p>When we combine the two loss functions, we get the loss function for the model overall:</p><blockquote>L = 0.5 * Σ(R — V(s))² - log(π(a | s)) * A(s) - β*H(π)</blockquote><p>Notice that the loss for value is set to 50% to make policy learning faster than value learning. For more information on the derivations of these loss functions, I recommend watching <a href="https://www.youtube.com/watch?v=KHZVXao4qXs">David Silver’s RL lecture </a>videos. With that, we can train our AC agent!</p><h3><strong>Asynchronous</strong></h3><p>The cool part about A3C is the first A — asynchronous. DeepMind’s paper showed that by introducing asynchronous training, we can reduce the correlation between episodes, improving various methods of learning including Q-learning (better data efficiency). It is also a more efficient use of multi-core CPUs, allowing us to train agents to do quite amazing things with just a laptop.</p><p>A3C works by spawning minion AC agents, each performing actions in their own separate environments and updating the master neural network after a certain amount of actions have been taken. The individual agents sync their weights with the master network after every gradient update.</p><figure><img alt="Image result for async actor critic network" src="https://cdn-images-1.medium.com/proxy/1*YtnGhtSAMnnHSL8PvS7t_w.png" /><figcaption>An efficient architecture for A3C, where the policy and value output share the main network.</figcaption></figure><p>That’s it for a high level overview of A3C. If you’re interested in checking out a detailed implementation of the algorithm, be sure to check out my <a href="https://github.com/calclavia/rl">Github repository</a> and <a href="https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part-8-asynchronous-actor-critic-agents-a3c-c88f72a5e9f2#.8dufgx6aw">a great Medium article</a> on its implementation.</p><p>I’ll be following up on this post next week on how I applied A3C for the game <em>Relay </em>for level generation <em>-</em> that’s the cool part! Feel free to leave me suggestions in the comment section!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=704147f91686" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>