From 9c6bf2484a84da172bda6c1d04952fdfe15279b4 Mon Sep 17 00:00:00 2001 From: Anirudh Dagar Date: Wed, 23 Mar 2022 01:59:48 +0530 Subject: [PATCH 1/2] [MXNet] Fix explicit NumPy coercing for matplotlib>3.4 --- .../integral-calculus.md | 2 +- .../multivariable-calculus.md | 6 ++++-- chapter_linear-networks/linear-regression.md | 16 +++++++++++++-- chapter_optimization/optimization-intro.md | 20 ++++++++++++++++++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/chapter_appendix-mathematics-for-deep-learning/integral-calculus.md b/chapter_appendix-mathematics-for-deep-learning/integral-calculus.md index 128efe816d..ddef4ef097 100644 --- a/chapter_appendix-mathematics-for-deep-learning/integral-calculus.md +++ b/chapter_appendix-mathematics-for-deep-learning/integral-calculus.md @@ -352,7 +352,7 @@ z = np.exp(- x**2 - y**2) # Plot function ax = d2l.plt.figure().add_subplot(111, projection='3d') -ax.plot_wireframe(x, y, z) +ax.plot_wireframe(x.asnumpy(), y.asnumpy(), z.asnumpy()) d2l.plt.xlabel('x') d2l.plt.ylabel('y') d2l.plt.xticks([-2, -1, 0, 1, 2]) diff --git a/chapter_appendix-mathematics-for-deep-learning/multivariable-calculus.md b/chapter_appendix-mathematics-for-deep-learning/multivariable-calculus.md index 4123d7096b..7c93154bf3 100644 --- a/chapter_appendix-mathematics-for-deep-learning/multivariable-calculus.md +++ b/chapter_appendix-mathematics-for-deep-learning/multivariable-calculus.md @@ -553,8 +553,10 @@ w = np.exp(-1)*(-1 - (x + 1) + (x + 1)**2 + y**2) # Plot function ax = d2l.plt.figure().add_subplot(111, projection='3d') -ax.plot_wireframe(x, y, z, **{'rstride': 10, 'cstride': 10}) -ax.plot_wireframe(x, y, w, **{'rstride': 10, 'cstride': 10}, color='purple') +ax.plot_wireframe(x.asnumpy(), y.asnumpy(), z.asnumpy(), + **{'rstride': 10, 'cstride': 10}) +ax.plot_wireframe(x.asnumpy(), y.asnumpy(), w.asnumpy(), + **{'rstride': 10, 'cstride': 10}, color='purple') d2l.plt.xlabel('x') d2l.plt.ylabel('y') d2l.set_figsize() diff --git a/chapter_linear-networks/linear-regression.md b/chapter_linear-networks/linear-regression.md index e415bb37df..50d9cf717e 100644 --- a/chapter_linear-networks/linear-regression.md +++ b/chapter_linear-networks/linear-regression.md @@ -391,7 +391,7 @@ rather than writing costly for-loops in Python.**) %matplotlib inline from d2l import mxnet as d2l import math -import numpy as np +from mxnet import np import time ``` @@ -507,7 +507,19 @@ def normal(x, mu, sigma): We can now (**visualize the normal distributions**). ```{.python .input n=8} -%%tab all +%%tab mxnet +# Use numpy again for visualization +x = np.arange(-7, 7, 0.01) + +# Mean and standard deviation pairs +params = [(0, 1), (0, 2), (3, 1)] +d2l.plot(x.asnumpy(), [normal(x, mu, sigma).asnumpy() for mu, sigma in params], xlabel='x', + ylabel='p(x)', figsize=(4.5, 2.5), + legend=[f'mean {mu}, std {sigma}' for mu, sigma in params]) +``` + +```{.python .input n=8} +%%tab pytorch, tensorflow # Use numpy again for visualization x = np.arange(-7, 7, 0.01) diff --git a/chapter_optimization/optimization-intro.md b/chapter_optimization/optimization-intro.md index e8df6b59b3..9d5d890d04 100644 --- a/chapter_optimization/optimization-intro.md +++ b/chapter_optimization/optimization-intro.md @@ -150,7 +150,25 @@ annotate('saddle point', (0, -0.2), (-0.52, -5.0)) Saddle points in higher dimensions are even more insidious, as the example below shows. Consider the function $f(x, y) = x^2 - y^2$. It has its saddle point at $(0, 0)$. This is a maximum with respect to $y$ and a minimum with respect to $x$. Moreover, it *looks* like a saddle, which is where this mathematical property got its name. ```{.python .input} -#@tab all +#@tab mxnet +x, y = d2l.meshgrid( + d2l.linspace(-1.0, 1.0, 101), d2l.linspace(-1.0, 1.0, 101)) +z = x**2 - y**2 + +ax = d2l.plt.figure().add_subplot(111, projection='3d') +ax.plot_wireframe(x.asnumpy(), y.asnumpy(), z.asnumpy(), + **{'rstride': 10, 'cstride': 10}) +ax.plot([0], [0], [0], 'rx') +ticks = [-1, 0, 1] +d2l.plt.xticks(ticks) +d2l.plt.yticks(ticks) +ax.set_zticks(ticks) +d2l.plt.xlabel('x') +d2l.plt.ylabel('y'); +``` + +```{.python .input} +#@tab pytorch, tensorflow x, y = d2l.meshgrid( d2l.linspace(-1.0, 1.0, 101), d2l.linspace(-1.0, 1.0, 101)) z = x**2 - y**2 From 487788aef12f2ee383f021dcf57c3caf4e935b46 Mon Sep 17 00:00:00 2001 From: Anirudh Dagar Date: Wed, 23 Mar 2022 02:01:24 +0530 Subject: [PATCH 2/2] Unpin matplotlib==3.4 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index bf088b91da..2e3dec1d41 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ requirements = [ 'jupyter', 'numpy', - 'matplotlib==3.4', + 'matplotlib', 'requests', 'pandas', 'gym'