<?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 Amber on Medium]]></title>
        <description><![CDATA[Stories by Amber on Medium]]></description>
        <link>https://medium.com/@qempsil0914?source=rss-4808b36a0933------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5ArZzrSUEiyFO8GsDR6dyA.jpeg</url>
            <title>Stories by Amber on Medium</title>
            <link>https://medium.com/@qempsil0914?source=rss-4808b36a0933------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 04:37:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@qempsil0914/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 Q-Learning, Part2: Double Deep Q Network, (Double DQN)]]></title>
            <link>https://medium.com/@qempsil0914/deep-q-learning-part2-double-deep-q-network-double-dqn-b8fc9212bbb2?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8fc9212bbb2</guid>
            <category><![CDATA[double-q-learning]]></category>
            <category><![CDATA[double-deep-q-learning]]></category>
            <category><![CDATA[deep-q-learning]]></category>
            <category><![CDATA[q-learning]]></category>
            <category><![CDATA[reinforcement-learning]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Fri, 26 Apr 2019 19:17:02 GMT</pubDate>
            <atom:updated>2019-04-26T19:17:02.451Z</atom:updated>
            <content:encoded><![CDATA[<h4>An introduction and implementation tutorial with python3 and Tensorflow</h4><p><a href="https://medium.com/@qempsil0914/zero-to-one-deep-q-learning-part1-basic-introduction-and-implementation-bb7602b55a2c">Last article</a>, we have discussed the basic (Deep) Q-Learning and implemented it in a quick way. However, there are some disadvantages in the basic version and various Q-Learning algorithms are developed to overcome such drawbacks.</p><p>Today, we focus on one problem of overestimations of action value, Q-value, in basic (Deep) Q-Learning and introduce one popular solution, called Double Deep Q Network (Double DQN), a Double Q-Learning with Neural Network architecture. For the sake of practicality, we will only discuss the core concept of Double Q-Learning. The other details in paper are left for the readers who are really interested in. Finally, we implement Double DQN with Python3 and Tensorflow. Here are the sections we are going to discuss.</p><ul><li>What is Double Q-Learning</li><li>Double Q-Learning Algorithm</li><li>Double Deep Q Network (Double DQN)</li><li>Implement Double Deep Q Network (Double DQN)</li></ul><h3>What is Double Q-Learning</h3><p>Double Q-Learning ([1] <a href="https://papers.nips.cc/paper/3964-double-q-learning">H. van Hasselt 2010</a>) was proposed for solving the problem of large overestimations of action value (Q-value) in basic Q-Learning.</p><blockquote>Briefly, the problem of overestimations is that the agent always choose the non-optimal action in any given state only because it has the maximum Q-value.</blockquote><p>In <a href="https://medium.com/@qempsil0914/zero-to-one-deep-q-learning-part1-basic-introduction-and-implementation-bb7602b55a2c">basic Q-learning</a>, the optimal policy of the Agent is always to choose the best action in any given state. The assumption behind the idea is that the best action has the maximum expected/estimated Q-value. <strong>However, the Agent knows nothing about the environment in the beginning, it needs to estimate <em>Q(s, a)</em> at first and update them at each iteration. Such Q-values have lots of noises and we are never sure whether the action with maximum expected/estimated Q-value is really the best one.</strong></p><p>Unfortunately, the best action often has smaller Q-values against the non-optimal one’s in most cases. According to the optimal policy in basic Q-Learning, the Agent tends to take the non-optimal action in any given state only because it has the maximum Q-value. Such problem is called the overestimations of action value (Q-value).</p><p>When such problem occurs, <strong>the noises from estimated Q-value will cause large positive biases in updating procedure</strong>. As a consequence, the learning process will be very complicated and messy.</p><h4>Understanding why do positive biases happen</h4><blockquote>Recall: <a href="https://medium.com/@qempsil0914/zero-to-one-deep-q-learning-part1-basic-introduction-and-implementation-bb7602b55a2c">Loss Function of basic Q-Learning</a></blockquote><p>Since the best Q(s, a) is estimated by using current Q(s, a), which is very noisy. The difference between the best and current Q(s, a) in Loss Function is also messy and contains positive biases which caused by different noises. These positive biases impact tremendously on update procedure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LsKaemvo3IGpvi2Aufy0EQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qOGvRAi7sDh4bdXB_W9L6w.png" /></figure><p>By the way, if the noises of all Q-values have <a href="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>, more specifically, Q-values are equally overestimated, then overestimations are not the problem since these noises don’t impact on the difference between the Q(s’, a) and Q(s, a). More details are in [1] <a href="https://papers.nips.cc/paper/3964-double-q-learning">H. van Hasselt 2010</a>, Section 2.</p><h3>Double Q-Learning Algorithm</h3><p>Double Q-Learning uses two different action-value functions, Q and Q’, as estimators. Even if Q and Q’ are noisy, these noises can be viewed as <a href="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>. That is, this algorithm solves the problem of overestimations of action value. Such proof is in [1] <a href="https://papers.nips.cc/paper/3964-double-q-learning">H. van Hasselt 2010</a>, Section 3.</p><p>The update procedure is slightly different from the basic version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nm0lt3oobxdBHTMACUZ-cg.png" /></figure><ul><li>Q function is for <strong>selecting the best action <em>a </em>with maximum Q-value of next state.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZtjomnlG11J73VYLj337CA.png" /></figure><ul><li>Q’ function is for <strong>calculating expected Q-value by using the action <em>a </em>selected above.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jgqy6YXK3YrEYBFbNXBpDg.png" /></figure><ul><li>Update Q function by using the expected Q-value of Q’ function.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b1WOxAlD8vaCt6iPGxwlVg.png" /></figure><h4>Completed Pseudocode</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mPzntWJWazLmk8W6z8jOfg.png" /><figcaption>[1]<a href="https://papers.nips.cc/paper/3964-double-q-learning"> H. van Hasselt. <em>Double Q-learning. </em>NIPS, 2010</a>.</figcaption></figure><p>However, this proposed method is a tabular/matrix way, we have discussed such drawbacks <a href="https://medium.com/@qempsil0914/zero-to-one-deep-q-learning-part1-basic-introduction-and-implementation-bb7602b55a2c">in part 1</a>. Based on the same reasons, we need to go deep now.</p><h3>Double Deep Q Network (Double DQN)</h3><blockquote>Double Q-Learning implementation with Deep Neural Network is called Double Deep Q Network (Double DQN).</blockquote><p>Double DQN is proposed in <a href="https://www.aaai.org/ocs/index.php/AAAI/AAAI16/paper/download/12389/11847">[2] H. van Hasselt, 2016</a>. Inspired by Double Q-Learning, Double DQN uses two different Deep Neural Networks, Deep Q Network (DQN) and Target Network.</p><p>Note that there is no learning rate<em> α</em> when updating the Q-values since it will be used in the optimization stage of updating the parameters of Deep Q Network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_6qcY5Fp_kjGj1_wCukGEg.png" /></figure><ul><li>Deep Q Network —<strong> selecting the best action <em>a </em>with maximum Q-value of next state.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ryUbqRCoqh9cUXYqLkg6ZA.png" /></figure><ul><li>Target Network —<strong>calculating the estimated Q-value with action <em>a </em>selected above.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HPeG10Z145N4pe85C5Tnbw.png" /></figure><ul><li>Update the Q-value of Deep Q Network based on the estimated Q-value from Target Network</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Eb7fDqPjNrsN0mA-OQLQ-Q.png" /></figure><ul><li>Update the parameters of Target Network based on the parameters of Deep Q Network per several iterations.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b2f1N-og34XgJUMLOE7O6A.png" /></figure><ul><li>Update the parameters of Deep Q Network based on <a href="https://www.tensorflow.org/api_docs/python/tf/train/AdamOptimizer">Adam optimizer</a>.</li></ul><h3>Implement Double Deep Q Network (Double DQN)</h3><blockquote>A step-by-step tutorial for Double DQN implementation.</blockquote><h4>Problem Definition</h4><p>We use <a href="https://gym.openai.com/docs/">Gym library</a> to derive the environment and solve the <a href="https://gym.openai.com/envs/CartPole-v1/"><em>CartPole-v1</em></a> problem.</p><h4>Step 1 — Experience Replay Construction</h4><blockquote>Experience Replay is a technique in Reinforcement Learning for Agent to make Agent learn from a batch of experiences/memories.</blockquote><p>When the Agent takes an action in given state, it gets rewards and some information. These information can be stored in Agent as its experience. Each experience can be represented to the following list.</p><pre>[state, action, reward, next_state, done]</pre><p>With <em>Experience Replay </em>technique, the Agent can store a certain amount of experiences due to the memory limit. When the Agent accumulates enough experiences, it starts to train itself based on batches of experiences. This technique is used in lots of studies, such as [3] <a href="http://web.stanford.edu/class/psych209/Readings/MnihEtAlHassibis15NatureControlDeepRL.pdf">V. Mnih et al. 2015</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3efae7ff16add755426f314d1d3576b6/href">https://medium.com/media/3efae7ff16add755426f314d1d3576b6/href</a></iframe><h4>Step 2— Target Network Construction</h4><blockquote>The target network is for calculating estimated Q-value with action which was selected from Deep Q network (DQN).</blockquote><p>For <a href="https://gym.openai.com/envs/CartPole-v1/"><em>CartPole-v1</em></a> problem, the network architecture is described below.</p><ul><li><strong>Input Layer</strong> — 4 units since there are 4 features in state space of environment.</li><li><strong>Output Layer</strong> — 2 units since there are 2 actions that Agent can take, right or left.</li><li><strong>Hidden Layer 1</strong>— 250 units for demonstration.</li><li><strong>Hidden Layer 2</strong>— 250 units for demonstration.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/325525eaa7e6707b8a395deac8b2d96f/href">https://medium.com/media/325525eaa7e6707b8a395deac8b2d96f/href</a></iframe><h4>Step 3— Deep Q Network (DQN) Construction</h4><blockquote>DQN is for selecting the best action with maximum Q-value in given state.</blockquote><p>The architecture of Q network(QNET) is the same as Target Network(TNET)</p><ul><li><strong>Input Layer </strong>— 4 units since there are 4 features in state space of environment.</li><li><strong>Output Layer</strong> — 2 units since there are 2 actions that Agent can take, right or left.</li><li><strong>Hidden Layer 1</strong>–250 units for demonstration.</li><li><strong>Hidden Layer 2</strong>–250 units for demonstration.</li></ul><p><em>In Line 4,</em> we create a TNET in QNET for calculating estimated Q-value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F8frXNh79-fnn7nkk8Hj1Q.png" /></figure><p><em>In Line 28</em>, we need to update Target Network based on the parameters of Deep Q Network per several iterations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b2f1N-og34XgJUMLOE7O6A.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e30d2955e69ed680dff6e5ccb36678d/href">https://medium.com/media/4e30d2955e69ed680dff6e5ccb36678d/href</a></iframe><p>According to step 1, the learning is based on batches of experiences. Therefore, we create specific parameters and optimization algorithm for batch learning, see the method<em> _batch_learning_model(self).</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c844cc4e1352f026d5b84c4f67b01f8f/href">https://medium.com/media/c844cc4e1352f026d5b84c4f67b01f8f/href</a></iframe><h4>Step 3-1 —Double DQN Algorithm for batch learning</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2FMtnGPMQq9laxvxuHZgjQ.png" /></figure><p><em>In line 25–28, </em>executing optimization and the parameters of QNET will update automatically.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac3bf620856454e71e421e91207a511d/href">https://medium.com/media/ac3bf620856454e71e421e91207a511d/href</a></iframe><h4>Step 3–2 — Other methods</h4><p>These methods are for the training stage of Agent. The Agent controls when to update target network or whether to explore the environment in accordance with the hyper parameter — <em>exploration rate.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45c4ce4421c6496593536c8e28fe40f2/href">https://medium.com/media/45c4ce4421c6496593536c8e28fe40f2/href</a></iframe><h4>Step 4— Agent Construction</h4><p>The Agent will train itself by using Double Q Network.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae2851f365dec89d990413b5ddff61c0/href">https://medium.com/media/ae2851f365dec89d990413b5ddff61c0/href</a></iframe><h4>Step 5— Start Training</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81ddd53bbc203117c3ccd22fd73ef2d9/href">https://medium.com/media/81ddd53bbc203117c3ccd22fd73ef2d9/href</a></iframe><pre># See the information of training process<br>episodes: 0 to 100, average_reward: 31.782, exploration: 0.598<br>episodes: 100 to 200, average_reward: 136.340, exploration: 0.132<br>episodes: 200 to 300, average_reward: 271.540, exploration: 0.012<br>episodes: 300 to 400, average_reward: 289.860, exploration: 0.001<br>episodes: 400 to 500, average_reward: 345.220, exploration: 0.001<br>episodes: 500 to 600, average_reward: 332.240, exploration: 0.001<br>...<br>...</pre><p>The result shows that Deep Q-Learning works well in <a href="https://gym.openai.com/envs/CartPole-v1/"><em>CartPole-v1</em></a><em> </em>problem. Now, it’s your turn.</p><h3>Summary</h3><p>In this article, we discuss about the most important part of Double Deep Q-Learning. Next part, we may discuss more about it or introduce another method of Deep Q-Learning.</p><p>Any feedbacks, thoughts, comments, suggestions, or questions are welcomed! Thanks.</p><h3>Reference</h3><p>[1]<a href="https://papers.nips.cc/paper/3964-double-q-learning"> H. van Hasselt. <em>Double Q-learning. </em>NIPS. 2010</a>.</p><p>[2] <a href="https://www.aaai.org/ocs/index.php/AAAI/AAAI16/paper/download/12389/11847">H. van Hasselt. , A. Guez, D. Silver. <em>Deep Reinforcement Learning with Double Q-Learning</em>, AAAI. 2016.</a></p><p>[3] <a href="http://web.stanford.edu/class/psych209/Readings/MnihEtAlHassibis15NatureControlDeepRL.pdf">V. Mnih, K. Kavukcuoglu, D. Silver, A. A. Rusu, J. Veness, M. G. Bellemare, A. Graves, M. A. Riedmiller, A.s Fidjeland, G. Ostrovski, S. Petersen, C. Beattie, A. Sadik, I. Antonoglou, H. King, D. Kumaran, D. Wierstra, S. Legg, D. Hassabis. <em>Human-level control through deep reinforcement learning. </em>Nature 518(7540): 529–533. 2015.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8fc9212bbb2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zero to One: (Deep) Q-learning, Part1, basic introduction and implementation]]></title>
            <link>https://medium.com/@qempsil0914/zero-to-one-deep-q-learning-part1-basic-introduction-and-implementation-bb7602b55a2c?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb7602b55a2c</guid>
            <category><![CDATA[reinforcement-learning]]></category>
            <category><![CDATA[q-learning]]></category>
            <category><![CDATA[tabular-q-learning]]></category>
            <category><![CDATA[deep-q-learning]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Tue, 09 Apr 2019 16:35:49 GMT</pubDate>
            <atom:updated>2019-04-13T10:18:10.621Z</atom:updated>
            <content:encoded><![CDATA[<h3>(Deep) Q-learning, Part1: basic introduction and implementation</h3><h4>This article is a beginning guide for novice.</h4><p><a href="https://github.com/Amber0914/Reinforcement_Learning_Algorithms"><em>The source code used here is available in my github.</em></a></p><p>In this article, we are going to discuss about the basic concept of <em>Q-Learning</em> and its implementation. Therefore, we will give readers some insights instead of digging into mathematical details such as <em>TD-learning</em> and <em>Bellman equation</em>. In order to demonstrate, we implement basic <em>Q-Learning</em> in a simple and intuitive way.</p><h4>There are several sections we are going to discuss about.</h4><ul><li><strong>What is Reinforcement Learning (RL)</strong></li><li><strong>What is Q-Learning algorithm (QL)</strong></li><li><strong>Understanding RL with Q-Learning</strong> — give readers insights by visualizing the procedures of <em>Q-learning</em> algorithm in <em>Reinforcement Learning Problem</em>.</li><li><a href="https://github.com/Amber0914/Reinforcement_Learning_Algorithms/blob/master/Q-Learning/frozenlake_unslippery.py"><strong>Simple Q-Learning Implementation with Python3</strong></a> — Implement QL with tabular method, a step-by-step tutorial.</li><li><a href="https://github.com/Amber0914/Reinforcement_Learning_Algorithms/blob/master/Q-Learning/deep_frozenlake_unslippery.py"><strong>Deep Q-Learning Implementation with Python3</strong></a><strong> — </strong>Implement QL with Deep Neural Network (Deep Q Network, DQN)<strong>, </strong>a step-by-step tutorial.</li></ul><h3>What is Reinforcement Learning (RL)</h3><p><strong><em>Reinforcement Learning (RL)</em>, a branch of Machine Learning, is applied to solve such problems that we know WHAT we want, but not really sure HOW to get there. </strong>For example, how do we train an Agent which can recognize whether there exists a tree in the image? Not even humans really know how to identify a tree, we just know when we see it, right? Hence, as to machine, this task is complex and impractical by using traditional method.</p><p>Let’s pause, think how to teach a puppy to sit. When giving SIT instruction to a puppy, it doesn’t understand at first and takes a random action. Fortunately, it may accidentally sit in some time points. In these moments, we give it rewards.<em> </em>As time goes by, the puppy sits down correctly for expected rewards when it receives SIT instruction. In other words — it has learned when/how to sit.</p><blockquote><em>This is where the magic happens in Reinforcement Learning, </em><strong><em>Rewards.</em></strong></blockquote><p>Inspired by the rewarding process when training a puppy, we reward the Agent with desired actions such as successful recognization and/or punish undesired ones. Finally, the Agent tends to take specific action to get more expected reward. In general, Reinforcement Learning (RL) is nothing but a rewarding process. Our goal is to train a rational Agent which can learn by itself with rewards and behave properly in the environment.</p><p>By the way, Agent is the term in AI which means an entity that can perceive and act in the environment.</p><h4>Reinforcement Learning Procedure</h4><ol><li>An Agent perceives from the environment and gets <em>current-state</em>.</li><li>According to <em>current state</em>, the Agent takes an <em>action </em>with <em>strategy/policy</em> in the environment.</li><li>The Agent gets the <em>reward</em> from the environment and update its <em>strategy/policy.</em></li><li>After taking the <em>action</em>, the environment updates and reaches to <em>next-state.</em></li><li>Repete 1–4.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o9AqpWda1MglklAq3QR1QA.png" /></figure><p>Overall, we need to design/define two parts first when solving RL problem.</p><ul><li><strong>Environment </strong>accepts the <em>action</em>, returns <em>rewards</em> and updates to <em>next state</em> based on its internal rules.</li><li><strong>Agent</strong> perceives <em>current state</em>, takes the <em>action</em> based on its <em>policy </em>and receives<em> rewards</em> for updating(learning) the <em>policy.</em></li></ul><h3>What is Q-Learning algorithm</h3><blockquote>Q-Learning is an algorithm in RL for the purpose of policy learning.</blockquote><p><strong>The <em>strategy/policy</em> is the core of the Agent. It controls how does the Agent interact with the environment</strong>. If an Agent learns the <em>policy</em> perfectly, then it is able to determine the most proper <em>action</em> in any given <em>state</em>. For instance, the Agent can identify whether there’s a tree in an image correctly if it has learned <em>the best policy.</em></p><p><strong>Mathematically, the <em>policy</em> in <em>Q-Learning </em>is modeled as <em>an action-value function Q(s, a), </em></strong>which represents how much is the Agent willing to take an action<em> a </em>in given<em> </em>state<em> s.</em> In addition, the value of <em>Q(s, a)</em> returned is called <em>Q-value. </em>The larger <em>Q-value,</em> the larger possibility of the action<em> a</em> will be taken since the Agent expects to get more rewards if it has large <em>Q-value</em>.</p><h4>Analogy to <a href="https://medium.com/@qempsil0914/machine-learning-notes-week1-b63d520b3afd">Supervised Learning</a></h4><p>In Supervised Learning, each data has the <em>true label (i.e. right answer)</em>. Our goal is to find the<em> best hypothesis function h(x) </em>to predict the <em>label. </em>The closer value between<em> </em>the<em> predicted label </em>and <em>true label</em>, the better.</p><p>In Reinforcement Learning, the <em>hypothesis function h(x) </em>we modeled by using<em> Q-learning</em> algorithm is called <strong><em>action-value function Q(s, a). </em></strong>We use <strong><em>Q(s, a)</em></strong> as the policy of the Agent. The closer value between<em> learned policy </em>and the<em> best policy</em>, the better. If the<em> best Q(s, a)</em> is found, then the Agent has learned the<em> policy </em>perfectly. Therefore, our goal here is to find the <strong><em>best Q(s, a), </em></strong><em>or called </em><strong><em>TD-target </em></strong>in term<strong>.</strong></p><p>The <a href="https://medium.com/@qempsil0914/machine-learning-notes-week2-multivariate-linear-regression-mse-gradient-descent-normal-e15785f771bd">Cost Function</a> (or <a href="https://en.wikipedia.org/wiki/Loss_function">Loss Function</a>) is used for measuring the quality of <em>h(x) </em>in Supervised Learning and it can be calculated precisely since the <em>label (i.e. right answer)</em> is known.</p><p><strong>There’s also Loss Function, a.k.a TD-error, in Reinforcement Learning. Unfortunately, the <em>TD-target </em>is always unknown. Then, how do we estimate the quality of the policy, i.e. <em>Q(s, a),</em> that the Agent learned?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ioDmnSKIPdiK8PEx63M-bA.png" /></figure><p>The way we apply to estimate the <em>best Q(s, a) </em>in <em>Q-learning</em> algorithm is <a href="https://en.wikipedia.org/wiki/Bellman_equation">Bellman Equation</a>. The procedure is described as following.</p><p>Step 1 — In time t, the Agent takes an<em> action a_t </em>in given current state<em> s_t. </em>Then, the Agent gets a <em>reward, </em>denoted<em> R_t+1, </em>when it arrives to next state <em>s_t+1.</em></p><p>Step 2 — In according to <em>Q(s, a)</em>, we are able to pre-calculate the maximum <em>Q-value</em> in given state<em> s_t+1</em> by considering all possible actions it can take. The <em>discount factor γ </em>is for weighting the importance of maximum next-state<em> Q-value.</em></p><p>Step 3 — Combine step 1 and step 2, the estimated <em>best Q(s, a), </em>or <em>TD-target, </em>is completed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HzK2X2s9SOsvsF1rvBwiDA.png" /></figure><h4><strong>Minimize Loss Function (<em>TD-error</em>)</strong></h4><p>There are many ways that can be applied to minimize Loss Function such as<em> Gradient Descent </em>which we used in Deep Q-learning implementation. In general, the core procedure of updating <em>Q(s, a) </em>is based on the <em>current rewards </em>and the <em>maximum future rewards (expected rewards).</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vT8KOhsx1yeKzeAANQ8O9Q.png" /></figure><ul><li><strong>Learning Rate </strong>— a hyper-parameter for controlling the convergent speed of updating procedure.</li><li><strong>Discount Factor </strong>— a hyper-parameter for weighting the importance of estimated future reward. The value of <em>discount factor </em>is between 0~1<em>. </em>The more closer to 1, the more important the <em>future reward</em> is.</li></ul><p>To understand the learning procedure, let’s take an example and see what happens.</p><h3>Understanding RL with Q-Learning</h3><h4>Problem Definition</h4><p>Taking the problem <a href="https://gym.openai.com/envs/FrozenLake-v0/"><em>FrozenLake-v0</em></a> as example. Our environment can be described as a 4x4 map with 16 <em>states</em>. Each <em>state</em> contains one situation — <em>S, F, H</em> or<em> G</em>. In any given <em>state</em>, the Agent has four <em>actions</em> (<em>left, down, right, up)</em> to arrive <em>next state. </em>The Agent starts at <em>S,</em> aims to arrive <em>G.</em></p><p>Note that we assume there is no slippery in <em>F</em> here which is different from the original problem<em>. </em>Thus, the movement in the environment is deterministic.</p><pre># Environment<em><br>SFFF       (S: starting point, safe)<br>FHFH       (F: frozen surface, safe)<br>FFFH       (H: hole, fall to your doom)<br>HFFG       (G: goal, where the frisbee is located)</em></pre><pre># Action<br>0:left, 1:down, 2:right, 3:up</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D97xrSXJxNQu6gOuRlrBwQ.png" /></figure><h4>Training Procedure</h4><p>At the beginning the Agent has an empty action-value function <em>Q(s, a) </em>since<em> </em>it doesn’t know about the environment.</p><p>Simply, we can view <em>Q(s, a) </em>as a matrix with <em>states</em> as rows and <em>actions</em> as columns, called <em>Q-matrix, </em>as the following right figure shows. Each element in <em>Q-matrix </em>is the <em>Q-value</em> of <em>Q(s, a)</em> in given <em>state s </em>and <em>action a.</em></p><p>Now, the Agent needs to explore the environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WHoB4TxZajkvpg_oHO3Hbg.png" /></figure><p>Suppose the Agent takes a random<em> action — right.</em></p><ul><li>The Agent arrives <em>state 1</em> and gets<em> current rewards.</em></li><li>The environment returns the maximum<em> expected reward.</em></li><li>The Agent updates <em>Q(s, a)</em> based on <em>current </em>and <em>expected rewards.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bray-zDj6H-Yn72xixGa2A.png" /></figure><p>It keeps going, takes <em>down</em> and arrives <em>state 5. </em>The Agent also gets <em>rewards </em>and<em> </em>updates <em>Q(s, a). </em>However, this training episode is terminated as the Agent falls into the hole. Therefore, we reset the environment for next training episode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pvpMC-aeYWu8DSIzTyrG5Q.png" /></figure><p>After resetting, the Agent starts at state 0 again and according to pass experience, it has learned the policy <em>Q(s, a).</em> The Agent keeps learning in this training episode and takes action by considering <em>current Q(s, a).</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lkTK04_1mCYObleTXRL6Pw.png" /></figure><p>At first, suppose the Agent takes <em>right</em> again. Next, it takes<em> right</em> instead of<em> down </em>after considering<em> current Q(s, a). </em>At each step, it also gets<em> rewards </em>and<em> updates</em> <em>Q(s, a)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qoSgL6u--FhGzRZGT0ygNQ.png" /></figure><p>As time goes by, the Agent has learned <em>Q(s, a) </em>well. Now we reset the environment again for testing. Surprising! The Agent can reach the goal easily with a series of proper actions which means it has learned the<em> best policy.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ehY9xCSsnWowQXWwFzj5uw.png" /></figure><h3>Simple Q-Learning Implementation with Python3</h3><h4>Step 0 — Overview</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/116c65aeeeeb7f9f1c106e72084382f4/href">https://medium.com/media/116c65aeeeeb7f9f1c106e72084382f4/href</a></iframe><h4>Step 1 — Environment Construction</h4><p>We use <a href="https://gym.openai.com/docs/">Gym library</a> to derive the environment and solve the <a href="https://gym.openai.com/envs/FrozenLake-v0/">FrozenLake</a> problem. However, instead of using the original FrozenLake environment, we make a new one with no slippery to make sure the movement in the environment is deterministic. Here, we named this environment as <em>FrozenLakeNoSlippery-v0.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5635bd064725e22166323ce8cba4146e/href">https://medium.com/media/5635bd064725e22166323ce8cba4146e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v5slop1RwuNF7LUEm2llxQ.png" /></figure><h4>Step 2 — hyper-parameters and Q-table initialization</h4><p>In <em>line 7,</em> the <em>discount factor</em> is used to measure the importance of<em> future reward. </em>Its value is 0~1. The more closer to 1, the more important the <em>future reward</em> is.</p><p>In <em>line 8–9</em>, the parameters <em>exploration_rate</em> and <em>exploration_decay </em>is used for training state and affects which kind of action, <em>random action </em>or<em> optimal action,</em> would the Agent take.</p><p>In <em>line15–17</em>, we create the simplest form of<strong><em> Q(s, a)</em></strong><em> — </em>a matrix with <em>states</em> as rows and <em>actions</em> as columns, called <em>Q-table/Q-matrix</em>. Since the Agent knows nothing at the beginning, all <em>Q-values</em> are initialized to zeros.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ed454354273b6b1add4c5315461325e/href">https://medium.com/media/7ed454354273b6b1add4c5315461325e/href</a></iframe><h4>Step 3— Setting the Policy that the Agent follows.</h4><p>In training stage, the Agent can take random or optimal action. The former is for <em>Exploration </em>in order to get better future reward and the later is for <em>Exploitation</em> and always choose the best known <em>action</em> in a given <em>state.</em></p><p>In testing stage, the Agent always takes the optimal action.</p><ul><li>Optimal Policy (Exploitation) — choose the most valuable <em>action</em> in any <em>state</em>.</li><li>Random Policy (Exploration) — take a random <em>action</em> in given <em>state</em>.</li><li>Control the Exploration Rate — Start with 100% exploration, move slowly towards roughly 0% since we want less and less exploration and focus on the optimal policy.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/551b20b052bec661358e1d0a74cb95a9/href">https://medium.com/media/551b20b052bec661358e1d0a74cb95a9/href</a></iframe><p><strong>Why Exploration and Exploitation</strong></p><p>Without <em>Exploration</em>, the Agent only cared about past performance and guaranteed results. However, the past performance is no guarantee or indication of future results. With the <em>Exploration</em>, the agent is more eager to explore riskier options to get larger rewards in the future.</p><p>However, we don’t want the Agent always takes a random move without considering the optimal policy. Therefore, we gradually decay the <em>exploration rate</em> in order to make the Agent focus on the <em>best policy</em> to reach the goal, a.k.a <em>Exploitation.</em></p><h4>Step 4 — Training with Q-learning Algorithm</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a17a8f48aa44b0e0ae7e8f1bb39675fa/href">https://medium.com/media/a17a8f48aa44b0e0ae7e8f1bb39675fa/href</a></iframe><h4>Step 5 — Testing</h4><p>In the testing procedure, the Agent always takes the <em>Optimal policy</em> in any given <em>state,</em> see<em> line8.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eeeeb73f7978aa921fb6f5f5c30ad98f/href">https://medium.com/media/eeeeb73f7978aa921fb6f5f5c30ad98f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j5mwFfrgugYe8s7_MBYIzw.png" /></figure><h4>Step 6 — How to use the code</h4><blockquote>The completed code above is available <a href="https://github.com/Amber0914/Reinforcement_Learning_Algorithms/blob/master/Q-Learning/frozenlake_unslippery.py">here</a>, if you are interested in, then you can run and expand it by yourself.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81a8eadae322179b03f5204e8200703d/href">https://medium.com/media/81a8eadae322179b03f5204e8200703d/href</a></iframe><h3><strong>Deep Q-Learning Implementation with Python3</strong></h3><h4>Why going deep</h4><p>With the matrix method, the space we need is<em> n</em> states by <em>m</em> actions. When these numbers are large such as 1000 states and 1000 actions per state, we would need a matrix of 1 million elements. Thus, the matrix method doesn’t work in most interesting problems such as chess, which needs 10¹²⁰ states space. Instead of recording <em>Q-value</em> precisely with matrix, the approximately way is more feasible such as Neural Network (NN) framework.</p><p>There is <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week4-non-linear-hypothesis-and-artificial-neural-network-a3c4bbdecb41">reference 1</a> and <a href="https://medium.com/@qempsil0914/course">2</a> for reviewing NN.</p><blockquote>Briefly, Deep Q-Learning is the method for approximating <em>Q(s, a)</em> with Deep Neural Networks, called Deep Q Network (DQN).</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qOVe1nW4ZeSJwR0oLV9VDg.png" /></figure><h4>Step 0 — Overview</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/983e45bf24f799517ccf6bd45dd60d79/href">https://medium.com/media/983e45bf24f799517ccf6bd45dd60d79/href</a></iframe><h4>Step 1 — Environment Construction</h4><p>We use the same <em>FrozenLakeNoSlippery-v0 </em>environment, a version without slippery against the original <a href="https://gym.openai.com/envs/FrozenLake-v0/"><em>version</em></a> in <a href="https://gym.openai.com/docs/"><em>Gym library</em></a><em>.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5635bd064725e22166323ce8cba4146e/href">https://medium.com/media/5635bd064725e22166323ce8cba4146e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v5slop1RwuNF7LUEm2llxQ.png" /><figcaption>Environment</figcaption></figure><h4>Step 2 — Neural Network Model Definition (NN <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week4-non-linear-hypothesis-and-artificial-neural-network-a3c4bbdecb41">reference 1</a> and <a href="https://medium.com/@qempsil0914/course">2</a>)</h4><p>We build the NN model by using<em> </em><a href="https://www.tensorflow.org/install"><em>Tensorflow</em></a>, a machine learning framework. Since the problem is relatively simple, our NN model is also simple and only contains one hidden layer.</p><ul><li><strong>Input Layer</strong> — 16 units since there are 16 states in the environment</li><li><strong>Output Layer</strong> — 4 units since there are 4 actions that Agent can take.</li><li><strong>Hidden Layer</strong> — 10 units for demonstration.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ovda1dJgbRHGi8Tj52-IFw.png" /><figcaption>line 3–15</figcaption></figure><ul><li><strong>Loss Function</strong> — Mean Square Error</li><li><strong>Optimal Method</strong> for minimizing the loss function — Gradient Descent</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2cd95ae02c6c1444706b199d7750488/href">https://medium.com/media/a2cd95ae02c6c1444706b199d7750488/href</a></iframe><h4>Step 3 — Initialize hyper-parameters and Neural Network Model</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5b00d7ba0ab249803c54ad5ecddac77/href">https://medium.com/media/f5b00d7ba0ab249803c54ad5ecddac77/href</a></iframe><h4>Step 4 —Training</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e62dd31071b3fd5bcbf47489f19f99c5/href">https://medium.com/media/e62dd31071b3fd5bcbf47489f19f99c5/href</a></iframe><h4>Step 5 — Testing</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b06786f2ea9027acef403e2c21ad9f4/href">https://medium.com/media/1b06786f2ea9027acef403e2c21ad9f4/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cDJ5Z24s4bspSlKAT-ukUg.png" /></figure><h4>Step6 — How to use</h4><blockquote>The completed code above is available <a href="https://github.com/Amber0914/Reinforcement_Learning_Algorithms/blob/master/Q-Learning/deep_frozenlake_unslippery.py">here</a>, if you are interested in, then you can run and expand it by yourself.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9b46d116785dd95ddabbc4b38a29a87/href">https://medium.com/media/a9b46d116785dd95ddabbc4b38a29a87/href</a></iframe><h3>Summary</h3><p>In this article, we show the overview of <em>Reinforcement Learning</em> and one popular algorithm,<em> Q-learning</em> algorithm. There are lots of details in <em>Q-learning </em>such as <em>TD-learning, </em>if you are really interested in these details, then you can check <a href="http://incompleteideas.net/book/the-book-2nd.html">[2], Chapter6.Temporal-Difference Learning</a>.</p><p>Next, we are going to discuss about one of the Deep Q-Learning method, Double Deep Q-Learning, or called Double Deep Q Network (Double DQN).</p><h3>Reference</h3><ul><li>[1] <a href="http://www.cs.rhul.ac.uk/~chrisw/thesis.html">C.J.C.H. Watkins. <em>Learning from Delayed Rewards</em>. PhD thesis, Cambridge University, Cambridge, England, 1989.</a></li><li>[2] <a href="http://incompleteideas.net/book/the-book-2nd.html">Richard S. Sutton, Andrew G. Barto. <em>Reinforcement Learning: An Introduction. </em>MIT Press, Cambridge, MA, 2018.</a></li></ul><p>If you like it, please give me a 👏. Any feedbacks, thoughts, comments, suggestions, or questions are welcomed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb7602b55a2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implement Neural Network without using deep learning libraries, step by step tutorial, python3]]></title>
            <link>https://medium.com/@qempsil0914/implement-neural-network-without-using-deep-learning-libraries-step-by-step-tutorial-python3-e2aa4e5766d1?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2aa4e5766d1</guid>
            <category><![CDATA[forward-propagation]]></category>
            <category><![CDATA[backward-propagation]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Sat, 19 Jan 2019 16:56:36 GMT</pubDate>
            <atom:updated>2019-01-19T16:56:36.853Z</atom:updated>
            <content:encoded><![CDATA[<p>Nowadays, Neural Network (NN) is a very powerful model and can be applied to various applications. This article provides a step-by-step tutorial for implementing NN, Forward Propagation and Backward propagation without any library such as tensorflow or keras.</p><ul><li>We have discussed the concept of NN in <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week4-non-linear-hypothesis-and-artificial-neural-network-a3c4bbdecb41">week4</a> and <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week5-neural-network-lost-function-forward-and-backward-8b293401e4dc">week 5</a>.</li><li>The completed source code used in this article is available here.</li></ul><h3>First Step: Preparing Dataset</h3><p>We use <a href="https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html">the Iris dataset</a> of scikit-learn for demonstration and visualize it as table.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb650cff7b045ec1f2c1171288b9d052/href">https://medium.com/media/bb650cff7b045ec1f2c1171288b9d052/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TwOZonbHUV0R7KM2xK265w.png" /></figure><h4><strong>Relabel Dataset</strong></h4><p>For convenience’s sake, we convert this problem into Binary Classification Problem by relabeling iris dataset — 1: Setosa, 0: not Setosa.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e09c895c9f01f1d6eee080ca8e38f9d/href">https://medium.com/media/0e09c895c9f01f1d6eee080ca8e38f9d/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2y2jiJLoI-aORushv1gZmw.png" /></figure><h4><a href="https://en.wikipedia.org/wiki/Scatter_plot"><strong>Plot Dataset</strong></a></h4><p>Here, we show the result of relabeling by plotting two pictures. One is with two sepal features (width and length), and the other is with two petal features (width and length).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30f21f7b144e7f62c165ea92f88110be/href">https://medium.com/media/30f21f7b144e7f62c165ea92f88110be/href</a></iframe><p>The color of points represents the data’s label.</p><ul><li>Original Label — Red: Setosa, Blue: Versicolor, Yellow: Virginica.</li><li>Relabel — Red: Setosa, Blue: Not Setosa.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*9LI2vhp2A_o4X4cYiLd7ow.png" /><figcaption>sepal features</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*p20KUAF0xa3wRtSudOcxAg.png" /><figcaption>petal features</figcaption></figure><p>Surely we can use a <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">simple logistic regression</a> to solve this classification problem, but that’s not the point of this tutorial, our goal is to demonstrate how to design your Neural Network for solving classification problems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KMkty6TE1GK0Sn_8BwdaBQ.png" /></figure><h3>Second Step — Building NN model</h3><p>Here is the NN model we want to build. Input layer has 4 units as the features of Iris dataset are 4 — ‘sepal length (cm)’, ‘sepal width (cm)’, ‘petal length (cm)’, ‘petal width (cm)’. Output layer has only one unit since it is the binary classification problem— Setosa and Not-Setosa. For convenience’s sake, we use only one hidden layer with six units. Let’s start!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bleJg4Y2aL61aCAb8xg3nQ.png" /></figure><h4>Step2.1 — Initialization of an NN model</h4><p>Firstly, initialize the model and set the weighting matrix <strong>W¹, W²</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aab07344196bcb07314d34fd28c7cdf6/href">https://medium.com/media/aab07344196bcb07314d34fd28c7cdf6/href</a></iframe><blockquote>Recall — A neuron (node/unit) is actually a logistic unit with Sigmoid (logistic) Activation Function(i.e simple logistic regression).</blockquote><p>After Forward Propagation procedure, we can get the final value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e90ef534f92b6ed6b6f699db3ed4ad1/href">https://medium.com/media/1e90ef534f92b6ed6b6f699db3ed4ad1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E26EItKKpbnoMpqjzKL8ow.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gfI7QKoWaQY7VX7gnUCK7Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4NzR8IkN6xcGnQweDifqXw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iMZZ9LrGARH1PAz1gQMQhg.png" /></figure><h4>Step2.3 — Implement Loss Function (i.e. Cost Function)</h4><p>In this problem, the loss function <strong><em>J(θ)</em></strong> we used is only for binary classification problem (e.g Setosa or Not-Setosa). The general form of <strong><em>J(θ)</em></strong> is discussed in <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week5-neural-network-lost-function-forward-and-backward-8b293401e4dc">week5.</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-DmdoIhJykb0mGl2TYiLCQ.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42bbbd3b519e8318cf04f4ede040ea4c/href">https://medium.com/media/42bbbd3b519e8318cf04f4ede040ea4c/href</a></iframe><h4>Step2.4 — Implement Backward Propagation</h4><blockquote>We discussed the details of Backward Propagation in <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week5-neural-network-lost-function-forward-and-backward-8b293401e4dc">week5</a>.</blockquote><p>First, compute the Partial Derivative of <strong>J(Θ).</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1f73ee1f7fa97dfc8feace541282e72/href">https://medium.com/media/d1f73ee1f7fa97dfc8feace541282e72/href</a></iframe><p>Second, update the weighting matrix <strong>W¹ and W² </strong>with the result of the Partial Derivative of <strong>J(Θ).</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fA3kihQc3Yb9qmLxRwdmEw.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67f0251a6e91c01a34b34b55475cf023/href">https://medium.com/media/67f0251a6e91c01a34b34b55475cf023/href</a></iframe><h4>Step2.5 — Implement Training and Testing Stage</h4><p>With the training weighting matrix <strong>W</strong>, we can execute Forward Propagation and get the final value, then we can use the value for prediction.</p><ul><li>If the final value ≥0.5, predict the class is Setosa.</li><li>If the final value &lt;0.5, the class is Not Setosa.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50d2dc900632566b3eaedf31c1a4550b/href">https://medium.com/media/50d2dc900632566b3eaedf31c1a4550b/href</a></iframe><h3>Third Step — Testing model</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce0de0de4a9ea0aa6601e76cc4e64eb6/href">https://medium.com/media/ce0de0de4a9ea0aa6601e76cc4e64eb6/href</a></iframe><h3>Summarize</h3><p>The source code here is more flexible that you can add any hidden units and input units according to your dataset, but it’s only for binary classification problem. You can try it or extend it by yourselves. We hope this could give you more concrete concepts of NN. If you like it, please give me a 👏. Any feedbacks, thoughts, comments, suggestions, or questions are welcomed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2aa4e5766d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coursera’s Machine Learning Notes — Week5, Neural Network — Lost Function, Forward-and-Backward…]]></title>
            <link>https://medium.com/@qempsil0914/courseras-machine-learning-notes-week5-neural-network-lost-function-forward-and-backward-8b293401e4dc?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b293401e4dc</guid>
            <category><![CDATA[loss-function]]></category>
            <category><![CDATA[backward-propagation]]></category>
            <category><![CDATA[coursera]]></category>
            <category><![CDATA[forward-propagation]]></category>
            <category><![CDATA[neural-networks]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Sat, 19 Jan 2019 16:54:20 GMT</pubDate>
            <atom:updated>2019-01-19T16:59:23.762Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coursera’s Machine Learning Notes — Week5, Neural Network — Lost Function, Forward-and-Backward Propagation</h3><blockquote>Notes on Coursera’s <a href="https://www.coursera.org/learn/machine-learning">Machine Learnin</a>g course, instructed by Andrew Ng, Adjunct Professor at Stanford University.</blockquote><p>Let’s continue with what’s left over from <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week4-non-linear-hypothesis-and-artificial-neural-network-a3c4bbdecb41">last week</a>.</p><h3>Forward Propagation</h3><blockquote>This procedure is for prediction.</blockquote><p>The steps are described as below:</p><ul><li>Firstly, we need a weighting matrix Θ<strong> </strong>(or denoted W) for each hidden layer. We can derive them randomly or per prior-knowledge.</li><li>Start from Input Layer X (or denoted a¹).</li><li>Do Forward Propagation throughout the Output Layer and get the final value.</li><li>Use the final value for prediction.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FczAiD6e8zWjWupOQkP_-Q.png" /></figure><h4>More Details</h4><p>Let’s take binary classification problem as example and visualize the detail of Forward Propagation. First of all, we need a completed NN architecture, that is, all weighting matrices <strong>W </strong>are known already.</p><ul><li>Input Layer: 4 units, Output Layer: 1 units, Hidden Layer : 6 units.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V_HL-RUfVfO82hIVshDkjQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ar4vNTqGAV6RPCp7lsTJqg.png" /></figure><p><strong>Forward Propagation (1)— From Input Layer to Output Layer</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*evggCLgdqmJnSZhJJWXKzg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HqNJ7P5tQntr9eSrGmyAKQ.png" /></figure><p><strong>Forward Propagation(2) — From Hidden Layer to Output Layer</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qwq2cu1J4Zyc3RRZ_UwiMQ.png" /></figure><p>In the end, we can get the final value from Output Layer, and use it for prediction.</p><ul><li>If final value ≥ 0.5, we predict the label y=1.</li><li>If not, then we predict the label y=0.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rYBiJ3cDLDaxP1KFEfz6qw.png" /></figure><h4><strong>Multi-Classification Problem</strong></h4><p>In multi-classification problem, there are more than one unit in the Output Layer. Each unit represents <strong>how certain the data belongs to a category. </strong>In this situation, we can apply the <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">one-vs-all</a> strategy to decide the prediction result. Note that the Forward Propagation is same as binary classification problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5wOrBMUQYM4SDOZhi4wqdA.png" /></figure><h3>Loss Function</h3><blockquote>Loss Function is also called Cost Function, but this name is used more commonly in NN.</blockquote><p>The goal of Loss Function is to measure the error that the model made. Here, we give a generated formula about loss function. Note that a node of NN is a logistic unit with Sigmoid (logistic) Activation Function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fdNu7Lwd8EjYlkMZicMtBQ.png" /></figure><h3>Backward Propagation</h3><blockquote>This procedure is for <strong>minimizing Loss Function</strong>, just like the technique — gradient descent we used in previous note.</blockquote><p>The way we used here is similar to Gradient Descent and there are two steps:</p><ol><li>Compute the partial derivative of <strong>J(Θ).</strong></li><li>Update each element of weighting matrix <strong>Θ</strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fA3kihQc3Yb9qmLxRwdmEw.png" /></figure><h4>Understanding with an Intuitive Way</h4><p>In the course, professor Ng gives a intuitive way to think of Backward Propagation. Let’s visualize the procedure by using the result of partial derivative of <strong>J(Θ).</strong></p><p>Let’s start from another example, assuming weighting matrices <strong>Θ</strong> (or denoted <strong>W</strong>) has been initialized. Our goal is to minimize the <strong>J(Θ)</strong> and then update the weighting matrices <strong>Θ</strong>.</p><blockquote>Note — We can use random method for initializing the weighting matrices Θ if we have no prior knowledge of the problem.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jOSudTUv7DV1TCf1aCaYKw.png" /></figure><p>After Calculus calculation, we can get the result of partial derivative of <strong>J(Θ).</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BFOT6jQmHgFjsP71po-hTg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ky7xiAYuVaqwsHTLBHA7Sg.png" /></figure><p>Let’s dig into the meaning of<strong> δ.</strong></p><ul><li><strong>δ³</strong> is the error of layer3, the Output Layer in this example.</li><li><strong>δ² </strong>is the error of layer2, the Hidden Layer in this example.</li><li><strong>δ¹ </strong>is not exist since layer1 is the Input Layer.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nYhZktt9lSikPXTXYcT6_w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9oZRMWU9SC382K_2Hd3SwA.png" /></figure><p>With the result of partial derivative of <strong>J(Θ), </strong>now you can update the weighting matrix Θ.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fA3kihQc3Yb9qmLxRwdmEw.png" /></figure><h4>More Details about Partial Derivative of <strong>J(Θ)</strong></h4><p>In this section, we want to give some hints about the derivations of the formulation above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uCMoqEyfCbiJm4cdjOouFA.png" /></figure><p>For convenience’s sake, we use the same example above and suppose there is only one data. Hence, the loss function <strong>J(Θ)<em> </em></strong>is simpler than the general form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Am5a8--rOd6EELVv6U4-DA.png" /></figure><p>In order to do the backward propagation, we need to do the forward propagation first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RqggjtwnE8T4uIZlicnHgA.png" /></figure><p>Then we can do Partial Derivative of J(Θ). Here, we show the Partial Derivative of two elements of W¹ and W² respectively. If you are interested, you can solve it with hints.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LebxbSwrXnaKC8rjbeArAg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ciZb9V9qVM8Zg0Axizh4w.png" /></figure><p>Finally, you can prove the formula of partial derivative of J(Θ) if you follow the procedure above. Don’t worry about it, if you are not familiar with Calculus, we have given you enough concepts in the section of <strong><em>Understanding with an Intuitive Way.</em></strong></p><h3>How to implement NN in practice?</h3><p><a href="https://medium.com/@qempsil0914/implement-neural-network-without-using-deep-learning-libraries-step-by-step-tutorial-python3-e2aa4e5766d1">Here is the reference for exercise</a>, we implement NN without using deep learning library. We hope the tutorial can help you understand the architecture of NN and the Forward and Backward Propagation procedure more clearly.</p><p>So far, we have discussed how to build a model, including using linear regression, logistic regression and NN. So, what is the next step when we get a training model? Could we make it better? These questions will be discussed next week — How to evaluate the model.</p><p>I hope this article is helpful/useful to you, and if you like it, please give me a 👏. Any feedbacks, thoughts, comments, suggestions, or questions are welcomed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b293401e4dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coursera’s Machine Learning Notes – Week4, Non-Linear Hypothesis and Artificial Neural Network…]]></title>
            <link>https://medium.com/@qempsil0914/courseras-machine-learning-notes-week4-non-linear-hypothesis-and-artificial-neural-network-a3c4bbdecb41?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3c4bbdecb41</guid>
            <category><![CDATA[non-linear-classification]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[coursera]]></category>
            <category><![CDATA[andrew-ng]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Mon, 14 Jan 2019 14:54:52 GMT</pubDate>
            <atom:updated>2019-01-16T09:11:08.881Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coursera’s Machine Learning Notes – Week4, Non-Linear Hypothesis and Neural Network (NN)</h3><blockquote><em>Notes on Coursera’s </em><a href="https://www.coursera.org/learn/machine-learning"><em>Machine Learnin</em></a><em>g course, instructed by Andrew Ng, Adjunct Professor at Stanford University.</em></blockquote><p>In previous notes, we introduced linear hypotheses such as <a href="https://medium.com/@qempsil0914/machine-learning-notes-week1-b63d520b3afd">linear regression</a>, <a href="https://medium.com/@qempsil0914/machine-learning-notes-week2-multivariate-linear-regression-mse-gradient-descent-normal-e15785f771bd">multivariate linear regression</a> and<a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6"> simple logistic regression</a>. They are useful for many problems, but still not enough to cover all problems in reality. The non-linear classification problems is one of those that can not be solved by just using linear method. So, let’s start!</p><h3>Non-Linear Classification Problem</h3><p>When we want to use machine learning to build a car image classifier, we need a training dataset with true labels, a car or not a car. After learning process, we get a good classifier. When we test it with a new image, the classifier will answer whether this new image is a car or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p42NmTAkp1UGif-wRHpuRw.png" /><figcaption>Source: week4 lecture slides, by Andrew Ng</figcaption></figure><p>Before moving on, we need to know how a computer ‘sees’ a picture. Like the picture in the right, a computer always ‘sees’ an image as a bunch of pixels with intensity values. For example, the picture shows the position of a pixel (red point) and its intensity value is 69.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QYWAI_Eqz4oXoR5QPz-4vg.png" /><figcaption>Source: week4 lecture slides, by Andrew Ng</figcaption></figure><p>Now, let’s see the learning process. First, take two pixels as features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_rfXORD1hgYGOAIL5cgWtA.png" /><figcaption>Source: week4 lecture slides, by Andrew Ng</figcaption></figure><p>Second, define a<strong> non-linear logistic regression</strong> as a hypothesis <strong><em>H</em></strong>. Our goal is to find a good <strong><em>H</em></strong> which can distinguish positive data and negative data well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GGcdeCMWXgkpvPU_gsvrgQ.png" /></figure><p>Last, learn all parameters of <strong><em>H</em></strong>. Compare this with <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">linear logistic regression introduced in week3</a>, the non-linear form is more complex since it is with lots of polynomial terms.</p><p>However, when a number of feature is large, the above solution is not a good choice to learn complex non-linear hypothesis. Suppose we have a 50x50 pixels image and all pixels are features, hence, a non-linear hypothesis must have more than 2500 features since <strong><em>H</em></strong> has extra quadratic or the cubic features. The computation cost would be very expensive in order to find all parameters <strong><em>θ </em></strong>of these features per the training data.</p><p>Therefore, we need a better way — Neural Network, which is a very powerful and widely used model to learn a complex non-linear hypothesis for many applications.</p><h3>Neural Network (NN)</h3><p>In this section, we are going to talking about how to represent hypothesis when using neural networks.</p><p>Originally, Neural Network is an algorithm inspired by human brain that tries to mimic a human brain. Like human brain’s neurons, NN has a lots of interconnected nodes (a.k.a neurons) which are organized in layers.</p><h4>Simplest Neural Network</h4><p>This simplest NN model only contains a neuron. <strong>We can treat a neuron (node) as a logistic unit with <em>Sigmoid (logistic) Activation Function</em></strong><em>,</em> which can output a computation value based on sigmoid activation function.</p><ul><li>The terminology of parameters <em>θ </em>in<em> </em>NN<em> </em>is called <strong><em>‘</em></strong><em>weights</em><strong><em>’.</em></strong></li><li>Depending on the problems, you can decide whether to use the bias units or not.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7_ak1ZETlIs66XaxEs8mjw.png" /></figure><h4>Neural Network (NN)</h4><blockquote>Layer 1 is called <strong>Input Layer</strong> that inputs features.</blockquote><blockquote>Last Layer is called <strong>Output Layer</strong> that outputs the final value computed by hypothesis <strong><em>H</em></strong>.</blockquote><blockquote>The layer between Input Layer and Output Layer is called <strong>Hidden Layer</strong>, which is a block we group neurons together.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jXl3GVQuGK1zgJLgUWWOBw.png" /></figure><h4>Note</h4><blockquote>A neuron (node) is actually a logistic unit with Sigmoid (logistic) Activation Function(i.e simple logistic regression). If you are familiar with Linear Algebra, you can think of a hidden layer as a linear combination of previous layer’s nodes. <strong>Therefore, the core idea of NN is to solve complex non-linear classification problem by using many sequences of simple logistic regression.</strong></blockquote><p>In order to get a clear picture about what this neural network is doing, let’s go through the computational steps and visualize them.</p><p><strong>First</strong>, we visualize the transition process of matrix <strong>Θ</strong>, which is a controlling function mapping from layer <em>j</em> to <em>j+1.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f_IrzMJFmALDkkjoyHyOjQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hjr2pPbdjXmS5B1OsZszHA.png" /></figure><p><strong>Second</strong>, we visualize each computation process of neurons. Note that the output value of each neurons is calculated by its sigmoid activation function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*88Z4ekTCEEYfw-s2619iVQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*viic3kbGuFNP3T7mHb81Og.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IlJ6P4Qo2SR4PVNsCAwBfg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HFd9l6pohPjA804Gd79PKQ.png" /></figure><h4><strong>Other Neural Network Architectures</strong></h4><blockquote>Other Neural Network Architectures can be designed by extending hidden layers. The number of neurons per layer will be based on the problems.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LLSRWnaJq-oLrc0seF6oiw.png" /></figure><h3>Applications and Examples</h3><p>Based on above concept, we are going to design some NN to show that how an NN model can be applied to non-linear classification problems.</p><h4>Examples1 — AND</h4><p>We can design a simple NN with single neuron for solving AND problem. <br>Given -30, 20 and 20 as weights, the <em>Sigmoid Activation Function </em><strong><em>H</em></strong> of this neuron (node) can be specified. When we predict data using this <strong><em>H, </em></strong>we can get a perfect result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*culdsZM4tS_hHG0pb5jrcw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eTN5V5UFEnlUuq2ojiojdg.png" /></figure><h4>Examples2 — OR</h4><p>The concept of OR operation is similar to AND, but we change the weight of the bias unit as -10.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4J1y88929WuHtd-exfzrOg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rjNw7qAY-E8CTuK-5DzYTA.png" /></figure><h4>Examples3 — Negation</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZM1mWEg5GRRVkivPJKIUEw.png" /></figure><h4>Examples4 — NAND</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ILlUBguHBFvte6vrcqSXXQ.png" /></figure><h4>Examples5 — XOR</h4><p>Clearly, this is a non-linear classification problem, we can solve it by using a non-linear logistic regression<strong> <em>H</em> </strong>that we discussed in the beginning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B-wJAzck3u_vT_yjcAKoqg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Q8mCHO3GtO4YFDQL73_7A.png" /></figure><p>However, when the number of features and data is large, The <strong><em>H </em></strong>will be too complex to understand and the computation cost is expensive. <strong>Instead, we use NN structure to make model <em>H </em>more clear and simple.</strong> Here, we<strong><em> </em></strong>apply NN to XOR Problem based on AND, NAND and OR.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jGG8jfAsyzkhZKfm89qQ4g.png" /></figure><h3>Multi-Class Classification Problem</h3><p>In this case, we can add nodes in the Output Layer, each node can predict one class, the concept of this is similar to <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">one-vs-all</a> mechanism we discussed in week3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4K-tlgMIm7QzDcDsH3a53w.png" /><figcaption>Source: week4 lecture slides, by Andrew Ng</figcaption></figure><p>So far, we have built the basic concept of NN. Next week, we will dig into the mathematic of NN, including how to find the weighting Θ, how to define the cost function, how to train a better NN, etc.</p><p>I hope this article is helpful/useful to you, and if you like it, please give me a 👏. Any feedbacks, thoughts, comments, suggestions, or questions are welcomed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3c4bbdecb41" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coursera’s Machine Learning Notes — Week3, Overfitting and Regularization. PartII]]></title>
            <link>https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-overfitting-and-regularization-partii-3e3f3f36a287?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e3f3f36a287</guid>
            <category><![CDATA[overfitting]]></category>
            <category><![CDATA[andrew-ng]]></category>
            <category><![CDATA[regularization]]></category>
            <category><![CDATA[coursera]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Sat, 12 Jan 2019 18:03:03 GMT</pubDate>
            <atom:updated>2019-01-12T18:03:03.623Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coursera’s Machine Learning Notes — Week3, Overfitting and Regularization. PartII</h3><blockquote>Notes on Coursera’s <a href="https://www.coursera.org/learn/machine-learning">Machine Learnin</a>g course, instructed by Andrew Ng, Adjunct Professor at Stanford University.</blockquote><p>After learning process, we get a good model. If we apply it to certain problems, we may or may not get good performance. That is the issue we are going to discuss today — How come the performance of a good model is terrible?</p><p>Let’s think it first— what is a good model?</p><p>In the previous note, the definition of a ‘good’ model is the one with least training error. First, define a hypothesis function <strong><em>H</em> </strong>(i.e. model). Second, define a Cost Function <strong><em>J(θ)</em></strong> for error measuring. Third, learn parameters of<em> </em><strong><em>H</em></strong><em> </em>by minimizing <strong><em>J(θ)</em></strong>. In the end, we can get a learned model with least training error. In the extreme case, this ‘good’ model has zero training error and fits training data perfectly, that is, the predict label of all training data is equal to their corresponding truth label.</p><p>Taking linear regression as example, the model fits all training data (red points) perfectly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O14grdvnFXqfSZBN_Rmbpg.png" /></figure><p>However, when new data (green point) comes in, this model has terrible performance with huge prediction error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tvgT7ZLwA_ue7InMwPdrgg.png" /></figure><p>The problem here is called Overfitting. A truly good model must have both little training error and little prediction error.</p><h4>Overfitting</h4><blockquote>The learned model works well for training data but terrible for testing data (unknown data). In other words, the model has little training error but has huge perdition error.</blockquote><p>If we have a whole dataset that captured all possibilities of the problem, we don’t need to worry about overfitting. When new data comes in, it shall fall into one possibility, hence, the model can predict it perfectly.</p><p>For example, suppose we have a training dataset that captures all points on the line and also get a perfect model h(x) with zero training error. Whenever new data comes in, it shall be one of the points, hence, the prediction error must be zero. In this special case, overfitting is not a problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JJWk0amiPyUXvQUDhXJ3VQ.png" /></figure><p>Unfortunately, the training data we get in reality is normally a small part of the whole dataset. Therefore, even if the model fits these training data perfectly, this is still not a good model and overfitting must occur.</p><p>When overfitting occurs, we get an over complex model with too many features. One way to avoid it is to apply Regularization and then we can get a better model with proper features.</p><h4>Regularization</h4><blockquote>It’s a technique applied to Cost Function <strong>J(θ)</strong> in order to avoid Overfitting.</blockquote><p>The core idea in Regularization is to keep more important features and ignore unimportant ones. The importance of feature is measured by the value of its parameter <strong><em>θj.</em></strong></p><p>In linear regression, we modify its cost function by adding regularization term. The value of <strong><em>θj</em></strong> is controlled by<strong> </strong>regularization parameter <strong>λ</strong>.<strong> </strong>Note that <strong><em>m</em></strong> is the number of data and <strong><em>n</em></strong> is the number of features(parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iq0A6CnGl7grw559bY3pnA.png" /></figure><p>For instance, if we want to get a better model instead of the overfitting one. Obviously, we don’t need features X³ and X⁴ since they are unimportant. The procedure describes below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Iybu0fFqQKAZHP6wK58_Ew.png" /></figure><p>First, we modify the Cost Function <strong><em>J(θ) </em></strong>by adding regularization. Second, apply gradient descent in order to minimize<em> </em><strong><em>J(θ) </em></strong>and get the values of <strong><em>θ3</em></strong> and <strong><em>θ4. </em></strong>After the minimize procedure, the values of<strong><em> θ3</em></strong><em> </em>and <strong><em>θ4</em></strong> must be near to zero if <strong>λ=1000.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G8_EkeuubAQtPUI0_0s5SQ.png" /></figure><p>Remember, the value of <strong><em>J(θ)</em></strong> represents training error and this value must be positive (≥0). The parameter <strong>λ=1000 </strong>has significant effect on <strong><em>J(θ), </em></strong>therefore, <strong><em>θ3 </em></strong>and<strong><em> θ4 </em></strong>must be near to zero (e.g 0.000001) so as to eliminate error value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mcFTFWBU_0SEqcRF-bh4EQ.png" /></figure><h4>Regularization Parameter λ</h4><ul><li>If <strong>λ</strong> is too large, then all the values of <strong><em>θ</em></strong> may be near to zero and this may cause Underfitting. In other words, this model has both large training error and large prediction error. (Note that the regularization term starts from <strong><em>θ1</em></strong>)</li><li>If <strong>λ </strong>is zero or too small, its effect on parameters <strong><em>θ </em></strong>is little. This may cause Overfitting.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rDsHGyI-AQ9s2wzSVdE4qA.png" /></figure><p>To sum up, there are two advantages of using regularization.</p><ul><li>The prediction error of the regularized model is lesser, that is, it works well in testing data (green points).</li><li>The regularization model is simpler since it has less features (parameters).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-d9StdNJbLscvzX4t1yZfw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LbXSPlwqQ4ywSGrAGYNKqw.png" /></figure><p>So far, we have discussed the concept of regularization. Next, we will show how to minimize regularized cost function by using gradient descent.</p><h4>Recall: Gradient Descent</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BPuIeM5ClWxOqulC0dT2Pw.png" /></figure><h4>Regularized linear regression</h4><blockquote>If you are not familiar with linear regression, please see <a href="https://medium.com/@qempsil0914/machine-learning-notes-week1-b63d520b3afd">here</a>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZNMRfFYK5dbmag9LovIcxg.png" /></figure><h4>Regularized logistic regression</h4><blockquote>If you are not familiar with logistic regression, please see <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">here</a>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5AZ-XuzOPZK18XeA0IXE2Q.png" /></figure><p>Next week, we are going to introduce a popular topic — <strong>Neural Network</strong>, numerous Neural Network Architectures have been developed and applied to applications widely.</p><p>Thanks for reading, and if you like it, please give me a 👏. Any feedbacks, thoughts, comments, suggestions, or questions are welcomed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e3f3f36a287" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coursera’s Machine Learning Notes — Week3, Classification Problem, Logistic Regression and…]]></title>
            <link>https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5792753f8d6</guid>
            <category><![CDATA[classification]]></category>
            <category><![CDATA[andrew-ng]]></category>
            <category><![CDATA[logistic-regression]]></category>
            <category><![CDATA[coursera]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Sun, 06 Jan 2019 20:18:19 GMT</pubDate>
            <atom:updated>2019-01-12T18:06:58.512Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coursera’s Machine Learning Notes — Week3, Classification Problem, Logistic Regression and Gradient Descent. PartI</h3><blockquote>Notes on Coursera’s <a href="https://www.coursera.org/learn/machine-learning">Machine Learnin</a>g course, instructed by Andrew Ng, Adjunct Professor at Stanford University.</blockquote><p>In <a href="https://medium.com/@qempsil0914/machine-learning-notes-week1-b63d520b3afd">week1</a> and <a href="https://medium.com/@qempsil0914/machine-learning-notes-week2-multivariate-linear-regression-mse-gradient-descent-normal-e15785f771bd">week2</a>, we introduced the Supervised Learning and Regression Problem. Today, we are going to discuss another problem in Supervised Learning — the Classification Problem.</p><p>Before going into details, we should think about - what’s the difference between Regression Problem and Classification Problem? The answer is label type, the former is continuous number while the later is discrete number.</p><p>In Regression Problem, a label is a continuous number (or real number). <a href="https://medium.com/@qempsil0914/machine-learning-notes-week2-multivariate-linear-regression-mse-gradient-descent-normal-e15785f771bd">Taking Housing Price as example</a>, according to the different combination of features, we can get the label (i.e. price) such as $460,000, $232,000, $315,000, etc. All of them are the actual value of the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ly54PqEsHuVvOVZf8WfeDQ.png" /></figure><p><strong>Whereas the label in Classification Problem represents ‘category’.</strong> Here we take breast cancer detection as an example. In breast cancer detection, what we concern most is the tumor type, that is, malignant or benign. For convenience’s sake, we simply labeled malignant and benign as 0 and 1 respectively. Note that the discrete number (0 and 1) means the label (tumor type) of the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jGISz6YismDIwpU26GvqRw.png" /></figure><p>Now we have the basic ideas of two problems. Let’s go into the details of Classification Problem!</p><h3>Classification Problem</h3><h4>Hypothesis function</h4><p>Since the label type is different from Regression Problem, we should use another hypothesis for solving Classification Problem. Here, we are going to introduce a popular one — Logistic Regression.</p><p><strong>Logistic Regression</strong> is also called a sigmoid function, which maps real numbers into probabilities, range in [0, 1]. Hence, the value of sigmoid function means <strong>how certain the data belongs to a category. </strong>The formula is defined as the following picture.</p><ul><li>Note that <em>y</em> represents the label, <em>y=1</em> is the goal label and <em>y=0</em> is the other label, and in sigmoid function, we always concern about the goal label.</li><li>In most cases, we take 0.5 as the threshold of probability. If <em>h(x)≥0.5,</em> we predict the data belongs to label 1, while <em>h(x)&lt;0.5</em>, we predict the data belongs to label 0.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_DonXY5v-xVHGdh3l-TJPA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oI9Y6BvKmAzM5mrT9YzVsQ.png" /></figure><p>In order to understand this concept more easily and clearly, let’s visualize the example and explain it step by step.</p><ol><li><strong>Preparing Dataset - </strong>suppose we have a breast detection dataset, each one has two features and one label.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*krqghLXLHbHmdiWr1eW6fA.png" /></figure><p>2. <strong>Getting Parameters -</strong> after learning process, we get a nice model, that is, we get the parameters of logistic regression. Here, we assume they are -3, 1 and 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QiKM1lCxMxmwdXOVDOmO9A.png" /></figure><p>3. <strong>Predicting Labels — according to the sigmoid function with learned parameters.</strong></p><ul><li>If the <em>-3+x1+x2≥0</em>, it means <em>h(x)≥0.5</em>, then we predict this tumor is benign (label 1).</li><li>If not, then we predict this tumor is malignant (label 0)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DGg-4vBh7YGCregrHzBXaw.png" /></figure><h4><strong>Understanding the concept of probability in logistic regression</strong></h4><p>In the following picture, although the label of point A and point B are both 1, their probabilities are different, and <em>P(B∈1)&gt;P(A∈1)</em>. When a point is farther away from the redline, this label is more determinable. Whereas when a point is right on the redline, the predictor can’t surely determine which label the point shall belong to since the probabilities are the same, <em>P(a∈label0)=P(a∈label1)=0.5. </em>That’s quite intuition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NdpfnEJzaWJq6SxxRVndCQ.png" /></figure><h4>Cost Function</h4><p>After learning process, we get the predictor - the logistic regression with learned parameters. To decide and improve the quality of the predictor, we need to define the Cost Function which measures the error that the hypothesis made and to minimize it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fD-H2Xr4ScsupKs6HGs4nw.png" /></figure><h4>Gradient Descend</h4><p>We can minimize the cost function by using gradient descent, the detail we discussed in previous note, please check <a href="https://medium.com/@qempsil0914/machine-learning-notes-week2-multivariate-linear-regression-mse-gradient-descent-normal-e15785f771bd">here</a>. In logistic regression, the gradient descent looks identical to linear regression.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BPuIeM5ClWxOqulC0dT2Pw.png" /></figure><h3>Multi-class classification</h3><p>So far, we have only discussed the binary classification problem but we often meet the multi-class classification problem in reality, i.e. more than two labels.</p><p>Here, we introduce a common method based on logistic regression, called one-vs-all or one-vs-rest.</p><p>The core idea of one-vs-all method is to classify the dataset into positive class and negative class (i.e. goal class and the other classes) at each time, and then to get the probability of being the positive class. After calculating the probabilities of being all positive classes, we choose the maximum one as the predictor’s result.</p><p>For example, suppose we have a dataset with three classes.</p><ul><li>Class1 (△)</li><li>Class2 (◯)</li><li>Class3 (☆)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ydfaNIcJJ7e3I_DDe1o8Q.png" /></figure><ul><li><strong>Step1 </strong>- Choosing the class1 as positive class, and the other classes are negative class.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0wfML28E8YBHENe_CFSRww.png" /></figure><ul><li><strong>Step2 </strong>- Suppose we get a predictor g1(z1) after learning process, we use this predictor to estimate the probability of being class1 (△)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YlGDKy_IUozDiup7dfEEug.png" /></figure><ul><li><strong>Step3</strong> - Repeat step1 and step2, but set a different positive class. Finally, we can get g2(z2) and g3(z3), as shown in the following picture. We use predictor g2 and g3 to estimate the probability of being class2 (◯) and class3 (☆) respectively.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sln9Vqxmm1r7ZtzMxo5xJg.png" /></figure><ul><li><strong>Step4</strong> - Predicting the class of new data by using three predictors, g1, g2 and g3.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aQ3NmgWlTv1XFcajMK-JCA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a4qHRqWWYIYCeID8GkSXpA.png" /></figure><p>As you can see, the probabilities we get for the new data are <strong><em>g1&lt;0.5</em></strong>, <strong><em>g2&gt;0.5</em></strong> and <strong><em>g3&lt;0.5.</em></strong> The maxima is<strong><em> g2&gt;0.5</em></strong>, Hence, we can conclude the predict label of this new data is class2 (◯).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6UebtMhCUQfaTDAuTW56-Q.png" /></figure><p>If you want to do a project relate to classification problem, here is a <a href="https://medium.com/@qempsil0914/machine-learning-nlp-text-classification-with-amazon-review-data-using-python3-step-by-step-3fb0cc0cecc1">reference</a> for novices.</p><p><a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-overfitting-and-regularization-partii-3e3f3f36a287">Next part, we will be talking about two important concepts - Overfitting and Regularization.</a></p><p>I hope this article is helpful/useful to you, and if you like it, please give me a 👏. Any feedbacks, thoughts, comments, suggestions, or questions are welcomed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5792753f8d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coursera’s Machine Learning Notes — Week2, Multivariate Linear Regression, MSE, Gradient Descent…]]></title>
            <link>https://medium.com/@qempsil0914/machine-learning-notes-week2-multivariate-linear-regression-mse-gradient-descent-normal-e15785f771bd?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/e15785f771bd</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[andrew-ng]]></category>
            <category><![CDATA[normal-equation]]></category>
            <category><![CDATA[gradient-descent]]></category>
            <category><![CDATA[linear-regression]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Sun, 30 Sep 2018 16:27:59 GMT</pubDate>
            <atom:updated>2019-01-11T14:39:09.270Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coursera’s Machine Learning Notes — Week2, Multivariate Linear Regression, MSE, Gradient Descent and Normal Equation.</h3><blockquote>Notes on Coursera’s <a href="https://www.coursera.org/learn/machine-learning">Machine Learnin</a>g course, instructed by Andrew Ng, Adjunct Professor at Stanford University.</blockquote><p>In <a href="https://medium.com/@qempsil0914/machine-learning-notes-week1-b63d520b3afd"><strong>Week1</strong></a>, we introduced the single variable linear regression. In this note, we will provide a concept of<strong> multivariate linear regression</strong> (i.e. multiple variables linear regression).</p><p>There are three parts in this note,</p><ul><li>Part I - We will talk about <strong>multivariate linear regression </strong>and its cost function, Mean Square Error.</li><li>Part II - How to minimize the cost function by using the Gradient Descent Algorithm, and provide a few skills to improve its performance.</li><li>Part III - Introducing the Normal Equation Method, another way to solve multivariate linear regression.</li></ul><h3>PART I - Multivariate Linear Regression</h3><blockquote>Linear Regression with multiple features</blockquote><p>If we have a <strong>continuous labeled data with more than one feature</strong>, we can use the multivariate linear regression to make a machine learning model.</p><p>The label is the answer to a data and the value is continuous (i.e. real numbers). With below housing price example, the label is price and the features are size, number of bedrooms, number of floors and age.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*50U3zOUFSEn0qejuaNYRYw.png" /></figure><p>Note that there is only one true model for the data. Unfortunately, we don’t know what the true model is. And in order to find the true one, we make a hypothesis, tuning it until we can check if it is close enough to the true model or it is a wrong one (according to the criterion such as accuracy, precision, F1 score etc.). If the hypothesis is wrong, then make another one and tune it.</p><p>When choosing a model, be utmost care about making the hypothesis.</p><p>Per above housing price example, suppose the true model for the data is linear, then we can try to use linear regression and its hypothesis function.</p><h4>Hypothesis function</h4><p>In Multivariate Linear Regression, the hypothesis function with multiple variables x and parameters θ is denoted below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4RTaC3lmtnYSlO6d1G1fow.png" /></figure><p>The following example shows that how to use the hypothesis function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BWdSOsH-J86PlKiinZB2hQ.png" /></figure><p>When tuning the hypothesis, our model <strong>learns parameters θ</strong> which makes hypothesis function <strong><em>h(x)</em></strong> a ‘good’ predictor. A Good predictor means the hypothesis is closed enough to the true model. The closer, the better.</p><p>But how do we know the hypothesis <strong><em>h(x) </em></strong>is good enough or not? The answer is using a cost function, which we defined to measure the error that the hypothesis made.</p><h4>Cost Function</h4><blockquote><em>The accuracy of Hypothesis Function </em><strong><em>h(x)</em></strong><em> can be measured by using Cost Function.</em></blockquote><p>In Regression Problem, the most popular cost function is <a href="https://en.wikipedia.org/wiki/Mean_squared_error"><strong>Mean Error Square(MSE)</strong></a>. The formulation is defined as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hM7K8YoL_orE8_aapT-GcQ.png" /></figure><p>Note that the square (see above pink rectangle) is necessary since an error value is always positive. Moreover, the square error function is differentiable, thus we can apply the Gradient Descent Algorithm to it.</p><p>Essentially, the cost function <strong><em>J(θ)</em></strong> is the sum of the square error of each data. The larger the error, the worser the performance of the hypothesis. Therefore, we want to minimize the error, that is, minimize the <strong><em>J(θ)</em></strong><em>.</em></p><h3>PART II - Gradient Descent Algorithm</h3><p>Gradient Descent Algorithm is a common method for minimizing cost function. Once the minimum error is found, the model learns the best parameters <em>θ</em> in the meanwhile. Thus, we probably find the good predictor, a hypothesis function with the best parameters<em> θ </em>that makes the minimum error.</p><p><em>Note that we use the word ‘probably’ here, since the hypothesis with the best parameters θ may suffer from the problem, the so called ‘overfitting’. We will discuss it in other section.</em></p><h4>Pseudocode</h4><blockquote>At each iteration, the <em>parameters θ </em>need to be updated <strong><em>simultaneously.</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BPuIeM5ClWxOqulC0dT2Pw.png" /></figure><p>In order to get a better understanding of the Gradient Descent Algorithm easily, we visualized the steps in following figure.</p><p>In figure (a), the starting point is at orange (<em>θj, J(θj)). </em>Calculate its partial differentiation, then multiplied it by a learning rate α and the updated result is at green (<em>θj, J(θj)),</em> as figure (b) shows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jxbSOnhAF2huiwSKXTAuLw.png" /></figure><h4>Learning rate α</h4><p>We use learning rate α to control how much we update at one iteration. If α is too small, it makes the gradient descent update too slow, whereas the update may overshoot the minimum and won’t converge.</p><p>Note that we set a fixed learning rate α in the beginning since the gradient descent will update slowly and automatically until it reaches the minimum. Hence, there is no need to change the learning rate α at each iteration by ourselves.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C3-HupBn5BP9U3Z_6ELfhg.png" /></figure><h4>Make Gradient Descent Well</h4><p>In this segment, we provide two skills, Feature Scaling and Learning Rate, to ensure the gradient descent will work well.</p><p><strong>Feature Scaling, </strong>also called normalized features. With different range of each feature, the contours may be extremely skinny, which will make Gradient Descent suffer from extremely slow, see Figure(a). After feature scaling, the outcome shows in Figure(b).</p><p>It’s worth making sure that the features are on a similar scale and roughly within the range [-1, 1].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ImvekfhM6sXo2IyAdslKLg.png" /></figure><p>One way to scale features is<a href="https://en.wikipedia.org/wiki/Feature_scaling"> Mean Normalization</a>, the formula is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G_AyuAqAcu4ezV2hJCmt7w.png" /></figure><p><strong>Learning Rate α -</strong> How to choose the value of α? According to the suggestion from Adjunct Professor Ng, is to try …, 0.001, 0.003, …, 0.01, 0.03, …, 0.1, 0.3, …, 1, …</p><h3>PART III - Normal Equation Method</h3><p>Normal Equation Method is very useful when solving the Linear Regression Problem.</p><p>Take a simple example - Linear regression with single variable. In the following figure, there are three linear equations as we have three data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h355AuSCxZKbyZ0rrqOI_g.png" /></figure><p>In linear Algebra, linear systems can be represented as the matrix equations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vwI6ZFYL8o88SCdi-zEl1Q.png" /></figure><p>If you are familiar with the concept of <strong>Pseudo Inverse </strong>in Linear Algebra, the parameters <em>θ</em> can be obtained by this formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BIWCTn40qA8Qkgm7yEtgKA.png" /></figure><p>In Multivariate Linear Regression, the formula is the same as above. But, what if the Normal Equation is non-invertible? Then consider deleting redundant features or using the regularization.</p><p>To sum up, the advantages in using Normal Equation are</p><ul><li>No need to choose learning rate α</li><li>No need to iterate</li><li>Feature scaling is not necessary</li></ul><p>Note that if the number of features is very large, the Normal Equation works extremely slow, while the Gradient Descent Algorithm still works well.</p><p>Next week, we will discuss Classification Problem and one of the popular hypothesis - Logistic Regression. If you are interested, see <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">here</a>.</p><p>Thanks for reading, and if you like it, please give me a 👏. Any feedbacks, thoughts, comments, suggestions, or questions are welcomed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e15785f771bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning, NLP: Text-Classification with Amazon review data using Python3, step-by-step…]]></title>
            <link>https://medium.com/@qempsil0914/machine-learning-nlp-text-classification-with-amazon-review-data-using-python3-step-by-step-3fb0cc0cecc1?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fb0cc0cecc1</guid>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[bag-of-words]]></category>
            <category><![CDATA[classification]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Sat, 22 Sep 2018 10:27:56 GMT</pubDate>
            <atom:updated>2019-01-06T20:26:38.489Z</atom:updated>
            <content:encoded><![CDATA[<h3>Machine Learning, NLP: Text-Classification with Amazon review data using Python3, step-by-step tutorial.</h3><p>Text-Classification is one of the active topics of research called Natural Language Processing (NLP). This article provides a supervised way to solve this problem, that is, the model learns from <em>labeled</em> data.</p><p>The complete source code used in this article is available <a href="https://github.com/Amber0914/NLP-Text_Classification"><strong>here</strong></a>.</p><h3>First Step: Gathering Dataset</h3><p><a href="http://jmcauley.ucsd.edu/data/amazon/">Amazon Review DataSet</a> is a useful resource for you to practice. Here, we choose a smaller dataset — <a href="http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/reviews_Clothing_Shoes_and_Jewelry_5.json.gz">Clothing, Shoes and Jewelry</a> for demonstration.</p><p>Format is one-review-per-line in json.</p><pre>{   <br>   &quot;reviewerID&quot;: &quot;A2SUAM1J3GNN3B&quot;,   <br>   &quot;asin&quot;: &quot;0000013714&quot;,<br>   &quot;reviewerName&quot;: &quot;J. McDonald&quot;,<br>   &quot;helpful&quot;: [2, 3],<br>   &quot;reviewText&quot;: &quot;I bought this for my husband who plays the piano.  He is having a wonderful time playing these old hymns.  The music  is at times hard to read because we think the book was published for singing from more than playing from.  Great purchase though!&quot;,<br>   &quot;overall&quot;: 5.0,<br>   &quot;summary&quot;: &quot;Heavenly Highway Hymns&quot;,<br>   &quot;unixReviewTime&quot;: 1252800000,<br>   &quot;reviewTime&quot;: &quot;09 13, 2009&quot; <br>}</pre><p>where</p><ul><li>reviewText — text of the review</li><li>overall — rating of the product</li></ul><p>If you are not familiar with json file, you can convert it to csv, just following the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c15acf5e2f542d8a03eda74701fb7162/href">https://medium.com/media/c15acf5e2f542d8a03eda74701fb7162/href</a></iframe><p>Since the value of <strong>overall</strong> is from 1 to 5, for convenience’ sake, we treat reviewText as two classification — negative and positive, <strong><em>labeled</em></strong><em> -1</em> and <em>+1 </em>respectively.</p><p>Here are the rules:<br>1. If the rating is 1 or 2, then the reviewText is treated as the <strong>negative</strong>, <strong><em>labeled</em></strong><em> </em><strong><em>-1.</em><br>2. </strong>If the rating is 4 or 5, then the reviewText is treated as the <strong>positive, <em>labeled+1.</em><br>3. Ignore</strong> all reviewsText with the rating 3. Since they belong to a neutral sentiment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39c2ac71830128825cc7fe0779700c37/href">https://medium.com/media/39c2ac71830128825cc7fe0779700c37/href</a></iframe><h4>Splitting Dataset into Training and Testing sets</h4><p>Training set — a subset to train the model.<br>Testing set — a subset to test the trained model.</p><p>Using the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html">package</a> of <a href="http://scikit-learn.org/">scikit-learn</a>, we can split data easily.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e5a99c1c742c79a4aecc2ccf7bdda6a/href">https://medium.com/media/5e5a99c1c742c79a4aecc2ccf7bdda6a/href</a></iframe><h3>Second Step: Text Data Processing</h3><p>The second and the most important step — clean dataset. A good model is not only dependent on the algorithm, but on a clean dataset mostly. There are many tactics in Text Data Processing, such as</p><ul><li>Remove non-alphanumeric characters, except for white space.</li><li>Convert all characters to lowercase, in order to treat the words such as “Hello”, “hello”, “HELLO”, “HEllO” all the same.</li><li>Consider <a href="https://nlp.stanford.edu/IR-book/html/htmledition/tokenization-1.html">Tokenization</a>, <a href="https://nlp.stanford.edu/IR-book/html/htmledition/stemming-and-lemmatization-1.html">Stemming, and lemmatization</a>.</li></ul><p>and so on.</p><p>In addition to clean dataset, we must represent text as a vector of “number” in order to satisfy the requirement since machine learning models always take numeric values as input.</p><h4><a href="https://en.wikipedia.org/wiki/Bag-of-words_model">Bag-of-words model</a></h4><p>Here, we use the most common encoding method, <a href="https://en.wikipedia.org/wiki/Bag-of-words_model">Bag-of-words model</a>, to encode the text as the bag of its words.</p><p>There are two stages — <a href="https://nlp.stanford.edu/IR-book/html/htmledition/tokenization-1.html"><strong><em>Tokenizing</em></strong></a><strong> </strong>and<strong><em> Vectorizing.</em></strong></p><p><strong><em>Tokenization</em></strong> is the task of chopping a sentence up into words, called tokens.</p><p>For example, if we tokenize three sentence <em>S1, S2 </em>and <em>S3</em>, what we will get is a <em>token_set</em>, each element in it is called a token.</p><pre>S1 = &quot;John likes to watch movies&quot;<br>S2 = &quot;Mary likes movies too&quot;<br>S3 = &quot;Joe likes horror movies and action movies&quot;</pre><pre>token_set = {&quot;John&quot;, &quot;Mary&quot;, &quot;Joe&quot;, &quot;likes&quot;, &quot;to&quot;, &quot;watch&quot;, &quot;movies&quot;, &quot;too&quot;, &quot;horror&quot;, &quot;and&quot;, &quot;action&quot;}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ddBXtWLd8AL1e37x61VTWg.png" /></figure><p>Next stage is <strong><em>Vectorization, </em></strong>which creates a vector to represent a text.<br>For example, take the <em>S3: “Joe likes horror movies and action movies” </em>and check the frequency of words from the<em> tokens_set.</em></p><p>&quot;John&quot;: 0<br>&quot;Mary”: 0<br>&quot;Joe&quot;: 1<br>&quot;likes&quot;: 1<br>&quot;to&quot;: 0<br>&quot;watch&quot;: 0<br>&quot;movies&quot;: 2<br>&quot;too&quot;: 0<br>&quot;horror&quot;: 1<br>&quot;and&quot;: 1<br>&quot;action&quot;: 1</p><p>Thus, the vector of <em>S3 </em>can be represented as:</p><pre>vector_S3 = [0, 0, 1, 1, 0, 0, 2, 0, 1, 1, 1]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VSqLHf1iZX2D7ZnlHkAcJg.png" /></figure><h4><a href="http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html">CountVectorizer</a></h4><p>In scikit-learn, <a href="http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html">CountVectorizer</a> is a good tool to help us construct the <a href="https://en.wikipedia.org/wiki/Bag-of-words_model">Bag-of-words model</a> that encoding data into the vector form.</p><p>Note that the parameter <em>token_pattern </em>is a regular expression denoting what constitutes a “token”, setting <em>token_pattern=r’\b\w+\b’ </em>means we only take a word as a token.</p><p>Applying <strong>fit_transform</strong> method to <strong>training set </strong>(i.e. train_X), we will get the vocabulary dictionary (i.e. <em>token_set</em>) and the vector of all <em>reviewText </em>(i.e. <em>train_vector</em>)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/327745cd3d31596470d41bd75f089238/href">https://medium.com/media/327745cd3d31596470d41bd75f089238/href</a></iframe><p>To help understand the result easily, we visualize the form of <em>train_vector </em>and<em> mark the index, </em>as shown in the following table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w_Yms0Cv_ZsV9T4WsK1DFg.png" /></figure><p>Applying <strong>transform</strong> method to<strong> test set </strong>(i.e. test_X), we will get the vector of all <em>reviewText</em> with <strong>the same</strong> <em>token_set</em> (i.e. <em>test_vector)</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18df9018706f0c7054f3c4bc3b21daa1/href">https://medium.com/media/18df9018706f0c7054f3c4bc3b21daa1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f-NdlTy7ZY9OJhdvqhRIhw.png" /></figure><p>Back to amazon dataset, ConuntVectorizer will do the pre-processing on text data before creating the vector, which we mentioned at the beginning of this section. Thus, we don’t need to clean data by ourselves.</p><p>The code is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f7b773712f84fb430e05600fa038212/href">https://medium.com/media/7f7b773712f84fb430e05600fa038212/href</a></iframe><h3>Final Step — Model Constructing</h3><p>For the classification problem, we use the popular model — <a href="http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html">Logistic Regression</a> for demonstration. Below shows how we utilize the model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a95eb593641a8213f1a976a2148e71a2/href">https://medium.com/media/a95eb593641a8213f1a976a2148e71a2/href</a></iframe><p>If you want to know more about logistic regression, please check <a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">here</a>.</p><p>I hope this article is helpful/useful to you, and if you like it, please give me a 👏. Any feedbacks, thoughts, comments, suggestions, or questions are welcomed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fb0cc0cecc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coursera’s Machine Learning Notes — Week1, Introduction]]></title>
            <link>https://medium.com/@qempsil0914/machine-learning-notes-week1-b63d520b3afd?source=rss-4808b36a0933------2</link>
            <guid isPermaLink="false">https://medium.com/p/b63d520b3afd</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[andrew-ng]]></category>
            <category><![CDATA[gradient-descent]]></category>
            <category><![CDATA[cost-function]]></category>
            <category><![CDATA[linear-regression]]></category>
            <dc:creator><![CDATA[Amber]]></dc:creator>
            <pubDate>Fri, 03 Aug 2018 19:10:50 GMT</pubDate>
            <atom:updated>2019-05-01T10:50:49.644Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Notes from Coursera’s <a href="https://www.coursera.org/learn/machine-learning">Machine Learnin</a>g course, instructed by Andrew Ng, Adjunct Professor at Stanford University.</blockquote><ul><li><a href="https://medium.com/@qempsil0914/machine-learning-notes-week2-multivariate-linear-regression-mse-gradient-descent-normal-e15785f771bd">Week2 — Multivariate Linear Regression, MSE, Gradient Descent and Normal Equation.</a></li><li><a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">Week3 — Classification Problem, Logistic Regression and Gradient Descent. PartI</a></li><li><a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-overfitting-and-regularization-partii-3e3f3f36a287">Week3 — Overfitting and Regularization. PartII</a></li><li><a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week4-non-linear-hypothesis-and-artificial-neural-network-a3c4bbdecb41">Week4 — Non-Linear Hypothesis and Neural Network (NN)</a></li><li><a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week5-neural-network-lost-function-forward-and-backward-8b293401e4dc">Week5 — Neural Network — Lost Function, Forward-and-Backward Propagation</a></li></ul><h3>Definition of Machine Learning</h3><blockquote>Machine learning is a science that gives computers the ability to learn without explicitly programmed.</blockquote><h3>Supervised Learning</h3><blockquote>given data with <strong>label (i.e. right answer)</strong></blockquote><h4>(1) Regression Problem: predict continuous valued label</h4><p>For example, given training data with housing size and price, which represents feature and label respectively. <br>When new data comes in, our goal is to predict its label, that is, the corresponding value of housing price, in the range between [0, 400].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ezvMPSoAZNomPOKAAdragg.png" /></figure><h4>(2) Classification problem: predict discrete valued label</h4><p>For example, given training data with tumor size and its category, which represents feature and label respectively. <br>In this case, we labeled 0 as Benign tumor and labeled 1 as Malignant tumor and make model with supervised learning. When new data comes in, our training model predicts its label, that is, label 1 (Malignant) or label 0 (Benign).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*khHP9Jx006XddRRsPyMfmg.png" /></figure><blockquote>Note:<br>It’s called Binary or Binomial Classification if we group data in two kinds of label.<br>It’s called Multi-class or Multinomial Classification if we group data in more than two kinds of label.</blockquote><h3>Unsupervised Learning</h3><blockquote>given data<strong> without any label(i.e. without any right answer)</strong></blockquote><p>Suppose each data has two features, denoted by x1 and x2.</p><p>The biggest difference between supervised and unsupervised learning is that each data has a label in the case of supervised learning, whereas there is<strong> NO label for each data in the case of unsupervised learning, which means, they have not been classified.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ubyn0Ar1LrrC4YdxLELMag.png" /></figure><h3>When we meet problems, should we apply Supervise Learning or Unsupervised Learning ?</h3><blockquote>Hint:<br>For supervised learning, there will ALWAYS be an input-output pair.<br>For unsupervised learning, there is just data with no label nor meaning and we try to make some sense out of it.</blockquote><h3>Making Model with Supervised Learning</h3><blockquote>Given a training dataset, making a model with supervised learning and fitting it by Learning Algorithm. In other words, learning a hypothesis function<strong> h: X → Y</strong> so that <strong>h(x)</strong> is a ‘good’ predictor for the corresponding value of <strong>y</strong>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vcbK4uaqcNY-GLbQ2pmX1A.png" /></figure><h3>Linear Regression for Regression Problem</h3><blockquote>Linear Regression is a kind of hypothesis function</blockquote><h4><strong>Linear Regression with one variable x and two parameters θ</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1tYoJysJpVbjKUM7ix46QQ.png" /></figure><h4>Regression Problem: predict continuous valued label</h4><p>Suppose we have a training dataset and each data is represented as <em>(x, y).<br></em>In Regression Problem, <em>x</em> ∈X is the feature and <em>y</em>∈Y is the continuous valued label.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p58Ibpx1SwVJqwRmS8tXFQ.png" /></figure><p>After applying Learning Algorithm, our model will learn a hypothesis function <strong><em>h</em>: X → Y</strong> so that <strong><em>h(x)</em></strong> is a ‘good’ predictor for the corresponding value of <em>y</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q85fWfewI8b5Cf3i5WSfQg.png" /></figure><p>When new data appears <em>(blue point)</em>, we can predict its label <em>(black mark) </em>by its feature<em>(green mark).</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AsuOeehSwW2OuCc5OEoc1A.jpeg" /></figure><h3>Cost Function</h3><blockquote>The accuracy of Hypothesis Function can be measured by using Cost Function.</blockquote><h4>Here, we use Square Error Function, one of the Cost Function, to measure the accuracy of Hypothesis Function.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mYZOUDyaqSAKWst-2lEdbQ.png" /></figure><blockquote>J(θ): cost function<br>θ: parameters of the hypothesis function h(x)<br>h(x): predicted label of data i<br>y: actual label of data i<br>m: amount of data</blockquote><p>For example, suppose there is a Linear Regression with one variable and two parameters θ, the square error function will be as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0JVWM37k7_lOAxy34UrSvQ.png" /></figure><p>In the following figures, we can see what does Hypothesis Function and Cost Function look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vx4bZC0gRHeBMJzG7inNZA.png" /></figure><h4><strong>Our goal is to minimize the cost function!!</strong></h4><h3><strong>Gradient Descent Algorithm</strong></h3><blockquote>Using Gradient Descent Algorithm to minimize the cost function.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*awzxo6eLlsmqn3sKpwJXOw.png" /></figure><blockquote>Note:<br><strong>Parameters <em>θ</em> should be simultaneously update at each iteration.</strong></blockquote><p>For example, applying Gradient Descent Algorithm to square error function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tK9vW0Msv-mp1nmJLoLw3w.png" /></figure><blockquote>Note.<br>Gradient Descent is called ‘<strong>batch’ </strong>gradient descent if it needs to look at every example in the entire training set at each step.</blockquote><h4>Learning Rate α</h4><p>If<strong> </strong>α is too small, Gradient Descent is slow.<br>If α is too large, Gradient Descent may overshoot minimum and not converge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y4MS1VknE8ZVj46eR9pkwQ.png" /></figure><blockquote>Note:<br>Choosing a proper learning rate α in the beginning and stick to it at each iteration since Gradient Descent will automatically take smaller steps when approaching local minimum.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WCwO12UTgQutw5uQFcbzSA.png" /></figure><h3>Gradient Descent for Linear Regression — at <br>Intuitive view</h3><p>Applying Gradient Descent to minimize cost function and we can see what happens at each iteration, step1 to step 5. <br>Ultimately, we will get a good hypothesis function with parameters θ.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PcWtfCA4Uz0hddbnhF3qsg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5s0sC-NESD9DZjQ4MNbaSQ.png" /></figure><p>In this note, we mainly focused on Linear Regression for Regression Problem and will discuss Logistic Regression, another kind of hypothesis function for Classification Problem, in ‘<a href="https://medium.com/@qempsil0914/courseras-machine-learning-notes-week3-classification-problem-logistic-regression-and-d5792753f8d6">Machine Learning Notes — Week3</a>’.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b63d520b3afd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>