Articles on sangyx's Bloghttps://blogs.python-gsoc.orgUpdates on different articles published on sangyx's BlogenFri, 28 Aug 2020 16:43:21 +0000udiff: Project Summaryhttps://blogs.python-gsoc.org/en/sangyxs-blog/udiff-project-summary/<h2>0. Github</h2> <p><a href="https://github.com/Quansight-Labs/udiff">https://github.com/Quansight-Labs/udiff</a></p> <h2>1. Describe my work briefly</h2> <p>Complete and package udiff.</p> <h2>2. What is done (commits)</h2> <ul> <li>Implement VJP: <a href="https://github.com/Quansight-Labs/udiff/commit/e23163c813e94bd8c19ac47c7529e8b8f3e14742">e23163c</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/4038f617c200634aab8f52e1114cd0f3b69cb9b8">4038f61</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/43147e05de2ca07ef2abd77c5d31bc1cf515d12f">43147e0</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/b6c4550820573b4897ad8332a9735ed13cae7588">b6c4550</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/3c3360e23c1dc2d2df5de09425002f48eee5113c">3c3360e</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/f6f04076abf4a4008ba20ad01e50812bbfb7fbd1">f6f0407</a></li> <li>Implement jacobian: <a href="https://github.com/Quansight-Labs/udiff/commit/fc7bc9aff1d48c9987e3dc4c9a9c83819347039e">fc7bc9a</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/9cd95d952db1a28bb50d7f9e48ea8d2456148b15">9cd95d9</a></li> <li>Add JVP: <a href="https://github.com/Quansight-Labs/udiff/commit/5b4157e94667b5f312f31cf5245cea830858333e">5b4157e</a></li> <li>Add documents: <a href="https://github.com/Quansight-Labs/udiff/commit/75f2bcf78a8e85da58a16714560c3b4885e721d7">75f2bcf</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/aed45d2b8b879477fe09ffbdffeb418d26da690b">aed45d2</a>, <a href="https://github.com/Quansight-Labs/udiff/pull/35/commits/1726b0ddfc69f2562fe0bd7a15b916560e6023e7">1726b0d</a></li> <li>Setup CI: <a href="https://github.com/Quansight-Labs/udiff/commit/15db53b18d8f0f5f6496af1c217085850a16f059">15db53b</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/842d73f988352067db0e0ac962e0a4d61587eb86">842d73f</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/bfb81510cf86dcad9fed4aa819d8423ebb9e2b2f">bfb8151</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/d7058a4235f0c5df85dd0cbe6738459d0c2ba5e8">d7058a4</a></li> <li>Add test cases: <a href="https://github.com/Quansight-Labs/udiff/commit/8ca34a0c20ab8b4a5d0761d78fc738f5a716d415">8ca34a0</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/304768983aa3ca28e583728c76d2b0a56adf5f86">3047689</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/5719c03e06e3c5bab4b91078105ce9e2e8cd2e3a">5719c03</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/015b4deaee6c57cad4a93adad6fd74d071711e7a">015b4de</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/a348c469bb22b2c5f8b97812d68bfa528e2c903e">a348c46</a>, <a href="https://github.com/Quansight-Labs/udiff/commit/9f7e3b5276a1c0d84030c6db16a76599c5fa33b5">9f7e3b5</a></li> </ul> <h2>3. TODO</h2> <ul> <li>Merge the code in branch <a href="https://github.com/sangyx/udiff/tree/dev">dev</a> to master if the <a href="https://github.com/Quansight-Labs/unumpy/issues/63">functions</a> implemented in unumpy.</li> <li>Implement JVP.</li> <li>Add more test cases to improve test coverage to over 80%(70% now).</li> <li>Publish udiff on pypi <a href="https://github.com/Quansight-Labs/udiff/issues/25">#25</a>.</li> <li>Fix bugs and issues in udiff.</li> </ul>sangyunxin@gmail.com (sangyx)Fri, 28 Aug 2020 16:43:21 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/udiff-project-summary/[Week 12] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-12-check-in/<h2>1. What did you do this week?</h2> <ul> <li> <p>Fix repeat_to_match_shape &amp; grad_broadcast_to.</p> </li> <li> <p>Clean code &amp; move unimplement code to dev.</p> </li> </ul> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <ul> <li>Publish udiff on pypi.</li> <li>Complete jvp.</li> <li>Add linalg.</li> <li>Add more document.</li> </ul>sangyunxin@gmail.com (sangyx)Sat, 22 Aug 2020 08:00:55 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-12-check-in/[Week 11] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-11-check-in-1/<h2>1. What did you do this week?</h2> <ul> <li> <p>Add more diff funtions and test cases.</p> </li> </ul> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <ul> <li>Publish a simple version of udiff on pypi.</li> <li>Add jvp.</li> </ul>sangyunxin@gmail.com (sangyx)Sun, 16 Aug 2020 13:55:47 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-11-check-in-1/[Week 10] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-10-check-in/<h2>1. What did you do this week?</h2> <ul> <li> <p>Complete np.reshape and np.stack.</p> </li> <li> <p>Split raw_functions &amp; nograd_functions</p> </li> <li> <p>Fix higher-order derivatives.</p> </li> </ul> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <ul> <li>Publish a simple version of udiff on pypi.</li> <li>Add more testcases.</li> </ul>sangyunxin@gmail.com (sangyx)Sun, 09 Aug 2020 13:18:25 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-10-check-in/[Week 9] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-9-check-in-1/<h1> </h1> <h2>1. What did you do this week?</h2> <ul> <li> <p>Control the base of the differentiation.</p> </li> <li> <p>High-order derivatives support.</p> </li> <li> <p>Cache diff in dict.</p> </li> </ul> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <ul> <li>Publish a simple version of udiff on pypi.</li> <li>Complete np.reshape and np.stack.</li> </ul>sangyunxin@gmail.com (sangyx)Sun, 02 Aug 2020 13:31:20 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-9-check-in-1/[Week 8] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-8-check-in-1/<h2>1. What did you do this week?</h2> <ul> <li> <p>Control the base of the differentiation.</p> </li> </ul> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <ul> <li>Publish a simple version of udiff on pypi.</li> <li>Complete more functions and testcases.</li> </ul>sangyunxin@gmail.com (sangyx)Sun, 26 Jul 2020 12:19:21 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-8-check-in-1/[Week 7] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-7-check-in-2/<h1> </h1> <h2>1. What did you do this week?</h2> <ul> <li>Fix np.sum.</li> <li>Merge pipelines into master.</li> </ul> <h2>2. Difficulty</h2> <p>Compatibility issue between uarray and unumpy.</p> <h2>3. What is coming up next?</h2> <ul> <li>Publish a simple version of udiff on pypi.</li> </ul>sangyunxin@gmail.com (sangyx)Sun, 19 Jul 2020 04:03:45 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-7-check-in-2/[Week 6] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-6-check-in-1/<h1> </h1> <h2>1. What did you do this week?</h2> <ul> <li>Use unumpy multimethods.</li> <li>Improve documentation.</li> </ul> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <ul> <li>Publish a simple version of udiff on pypi.</li> <li>Fix np.sum.</li> </ul>sangyunxin@gmail.com (sangyx)Sun, 12 Jul 2020 01:32:03 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-6-check-in-1/[Week 5] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-5-check-in-1/<h1> </h1> <p>1. What did you do this week?</p> <ul> <li>Add more test cases to cover more functions.</li> </ul> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <ul> <li>Use unumpy multimethods.</li> <li>Improve documentation.</li> <li>Publish a simple version of udiff on pypi.</li> </ul>sangyunxin@gmail.com (sangyx)Sun, 05 Jul 2020 14:21:24 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-5-check-in-1/[Week 4] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-4-check-in/<h2>1. What did you do this week?</h2> <p>This week's job is to implement jacobian and add test cases. </p> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <ul> <li>Add more test cases to cover more functions.</li> <li>Improve documentation.</li> <li>Publish a simple version of udiff on pypi.</li> </ul>sangyunxin@gmail.com (sangyx)Sun, 28 Jun 2020 10:13:50 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-4-check-in/[Week 3] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-3-check-in-1/<h2>1. What did you do this week?</h2> <p>This week's job is to add test cases and change the code as mentor's request. </p> <h2>2. Difficulty</h2> <p>No difficulties this week.</p> <h2>3. What is coming up next?</h2> <p>The work for the next week is to merge vjp and start to work on jacobian.</p>sangyunxin@gmail.com (sangyx)Sun, 21 Jun 2020 08:58:29 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-3-check-in-1/[Week 2] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-2-check-in-4/<h2>1. What did you do this week?</h2> <p>This week's main job is to implement the backward propagation. After that, I complete the whole vjp. </p> <p>Because of the bad support for figure and code, more detail see: <a href="https://sangyx.com/1790">https://sangyx.com/1790</a></p> <h2>2. Difficulty</h2> <p>I complicated this problem at the beginning. Later I found that imitating pytorch's API to implement is more intuitive and simple. </p> <p>In addition, because I am not very familiar with uarray, some implementations are not elegent.</p> <h2>3. What is coming up next?</h2> <p>The work for the next week is to test vjp and work with my mentor to rewrite the code more elegent.</p>sangyunxin@gmail.com (sangyx)Sun, 14 Jun 2020 08:45:04 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-2-check-in-4/[Week 1] Check-inhttps://blogs.python-gsoc.org/en/sangyxs-blog/week-1-check-in-6/<h2>1. What did you do this week?</h2> <p>This week's main job is to build a calculation graph. The core of the automatic differential system is vjp, which is composed of calculation graph construction and gradient calculation.</p> <h2>2. Difficulty</h2> <p>The computational graph construction is not as simple as I thought. The main problem encountered in the process is that some basic differential operators not only need to pass in the tensors, but also need to pass in some parameters of the function, such as np.sum. This requires that some parameters required by the differential operator be passed in advance when constructing the calculation graph. In addition, according to the different calculation paths, the parents of each node in the calculation graph should be marked appropriately, and then they can be calculated along the correct path during back propagation.</p> <h2>3. What is coming up next?</h2> <p>The work for the next week is to implement simple back propagation and complete a complete differential operation.</p>sangyunxin@gmail.com (sangyx)Thu, 11 Jun 2020 07:12:10 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/week-1-check-in-6/[Community Bonding Period] What is Automatic Differentiation?https://blogs.python-gsoc.org/en/sangyxs-blog/community-bonding-period-what-is-automatic-differentiation/<p>The optimization process of deep learning models is based on the gradient descent method. Deep learning frameworks such as PyTorch and Tensorflow can be divided into three parts: model api, gradient calculation and gpu acceleration. Gradient calculation plays an important role, and the core technology of this part is automatic differentiation.</p> <h2><b>1 Differential method</b></h2> <p>There are four differential methods:</p> <ul> <li> <p>Manual differentiation</p> </li> <li> <p>Numerical differentiation</p> </li> <li> <p>Sign differential</p> </li> <li> <p>Automatic differentiation</p> </li> </ul> <p>Manual differentiation is to use the derivation formula to manually write the derivation formula. This method is accurate and effective, and the only disadvantage is that it takes effort.</p> <p>Numerical differentiation uses the definition of derivative.This method is simple to implement, but there are two serious problems: truncation error and roundoff error. But this method can be a good way to check whether the gradient is accurate.</p> <p>Another method is symbolic differentiation, which transfers the work we did in manual differentiation to the computer. The problem with this method is that the expression must be closed-form, that is, there cannot be loops and conditional expressions. So that the entire problem can be converted into a pure mathematical symbol problem can be solved using some algebraic software. However, when expressions are complex, the problem of "expression swell" is prone to occur.</p> <p>The last is our protagonist: automatic differentiation. It is also the most widely used derivation method in programe.</p> <h2><b>2 Automatic differentiation</b></h2> <p>The automatic differentiation discovers the essence of differential calculation: Differential calculation is a combination of a limited series of differentiable operators.</p> <p>We can regarded the formula: f(x1, x2)=ln(x1)+x1x2-sin(x2) as a calculation graph (What’s more, it can be regarded as a tree structure, too). In the process of forward calculation, we can obtain the value of each node.</p> <p>Then we can express the derivation process of  df/dx1<b> </b>as a series of differential operator combinations. The calculation can be divided into two types: calculating the formula from forward to backward is called Forward Mode, and calculating the formula from backward to forward is called Reverse Mode. </p> <p>The gradient values ​​calculated by the two modes are the same, but for the calculation order is different, the calculation speed is different. Generally, if the Jacobian matrix is relatively high, then the forward mode is more efficient; if the Jacobian matrix is ​​wider, then the reverse mode is more efficient.</p> <h2><b>3 JVP, VJP and vmap</b></h2> <p>If you have used pytorch, you will find that if y is a tensor instead of a scalar, you will be asked to pass a  grad_variables in y.backward(). And the derivative result x.grad has the same shape as x. Where is the Jacobian matrix?</p> <p>The reason is that deep learning frameworks such as Tensorflow and PyTorch prohibit the derivatives with tensor by tensor, but only retain scalar by tensor. When we call y.backward () and enter a grad_variables v. In fact, it actually converts y into a weighted sum l = torch.sum(y * v) , where l is a scalar, and then the gradient of x.grad is naturally of the same shape as x. The reason for this is that the loss of deep learning is definitely a scalar, and gradient descent requires that the gradient must be of the same type as x.</p> <p>But what if we want to obtain the Jacobian matrix?</p> <p>The answer is to derive x for each value of y.In addition, Google's new deep learning framework JAX uses a more advanced method, the vectorization operation vmap to speed up the calculation. </p> <h2><b>4 Reference</b></h2> <ul> <li> <p><a href="https://jax.readthedocs.io/en/latest/notebooks/autodiff_cookbook.html">The Autodiff Cookbook</a></p> </li> <li> <p><a href="https://arxiv.org/pdf/1502.05767.pdf">Automatic Differentiation in Machine Learning: a Survey</a></p> </li> <li> <p><a href="https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html">AUTOGRAD: AUTOMATIC DIFFERENTIATION</a></p> </li> </ul> <p> </p>sangyunxin@gmail.com (sangyx)Mon, 25 May 2020 11:18:53 +0000https://blogs.python-gsoc.org/en/sangyxs-blog/community-bonding-period-what-is-automatic-differentiation/