.. code:: python
%matplotlib inline
import math
import time
from mxnet import np
from d2l import mxnet as d2l
.. raw:: html

.. raw:: html
.. code:: python
%matplotlib inline
import math
import time
import numpy as np
import torch
from d2l import torch as d2l
.. raw:: html

.. raw:: html
.. code:: python
%matplotlib inline
import math
import time
import numpy as np
import tensorflow as tf
from d2l import tensorflow as d2l
.. raw:: html

.. raw:: html
.. code:: python
n = 10000
a = np.ones(n)
b = np.ones(n)
.. raw:: html

.. raw:: html
.. code:: python
n = 10000
a = torch.ones(n)
b = torch.ones(n)
.. raw:: html

.. raw:: html
.. code:: python
n = 10000
a = tf.ones(n)
b = tf.ones(n)
.. raw:: html

.. raw:: html
.. code:: python
c = np.zeros(n)
timer = Timer()
for i in range(n):
c[i] = a[i] + b[i]
f'{timer.stop():.5f} sec'
.. parsed-literal::
:class: output
'4.45868 sec'
.. raw:: html

.. raw:: html
.. code:: python
c = torch.zeros(n)
timer = Timer()
for i in range(n):
c[i] = a[i] + b[i]
f'{timer.stop():.5f} sec'
.. parsed-literal::
:class: output
'0.09248 sec'
.. raw:: html

.. raw:: html
.. code:: python
c = tf.Variable(tf.zeros(n))
timer = Timer()
for i in range(n):
c[i].assign(a[i] + b[i])
f'{timer.stop():.5f} sec'
.. parsed-literal::
:class: output
'4.70278 sec'
.. raw:: html

.. raw:: html
.. code:: python
timer.start()
d = a + b
f'{timer.stop():.5f} sec'
.. parsed-literal::
:class: output
'0.00037 sec'
.. raw:: html

.. raw:: html
.. code:: python
timer.start()
d = a + b
f'{timer.stop():.5f} sec'
.. parsed-literal::
:class: output
'0.00028 sec'
.. raw:: html

.. raw:: html
.. code:: python
timer.start()
d = a + b
f'{timer.stop():.5f} sec'
.. parsed-literal::
:class: output
'0.00030 sec'
.. raw:: html

.. raw:: html
.. code:: python
# 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, [normal(x, mu, sigma) 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])
.. figure:: output_linear-regression_216540_55_0.svg
.. raw:: html

.. raw:: html
.. code:: python
# 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, [normal(x, mu, sigma) 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])
.. figure:: output_linear-regression_216540_58_0.svg
.. raw:: html

.. raw:: html
.. code:: python
# 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, [normal(x, mu, sigma) 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])
.. figure:: output_linear-regression_216540_61_0.svg
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html