.. code:: python
import math
from mxnet import gluon, np, npx
from mxnet.gluon import nn
from d2l import mxnet as d2l
npx.set_np()
.. raw:: html

.. raw:: html
.. code:: python
import math
import numpy as np
import torch
from torch import nn
from d2l import torch as d2l
.. raw:: html

.. raw:: html
.. code:: python
import math
import numpy as np
import tensorflow as tf
from d2l import tensorflow as d2l
.. raw:: html

.. raw:: html
.. code:: python
features[:2], poly_features[:2, :], labels[:2]
.. parsed-literal::
:class: output
(array([[-0.03716067],
[-1.1468065 ]]),
array([[ 1.0000000e+00, -3.7160669e-02, 6.9045764e-04, -8.5526226e-06,
7.9455290e-08, -5.9052235e-10, 3.6573678e-12, -1.9415747e-14,
9.0187767e-17, -3.7238198e-19, 1.3837962e-21, -4.6747992e-24,
1.4476556e-26, -4.1381425e-29, 1.0984010e-31, -2.7211542e-34,
6.3199942e-37, -1.3815009e-39, 2.8516424e-42, -5.6051939e-45],
[ 1.0000000e+00, -1.1468065e+00, 6.5758252e-01, -2.5137332e-01,
7.2069131e-02, -1.6529869e-02, 3.1594271e-03, -5.1760738e-04,
7.4199430e-05, -9.4547095e-06, 1.0842722e-06, -1.1304095e-07,
1.0803007e-08, -9.5299690e-10, 7.8064499e-11, -5.9683248e-12,
4.2778208e-13, -2.8857840e-14, 1.8385756e-15, -1.1097316e-16]]),
array([ 5.1432443 , -0.06415121]))
.. raw:: html

.. raw:: html
.. code:: python
# Convert from NumPy ndarrays to tensors
true_w, features, poly_features, labels = [
torch.tensor(x, dtype=torch.float32)
for x in [true_w, features, poly_features, labels]]
features[:2], poly_features[:2, :], labels[:2]
.. parsed-literal::
:class: output
(tensor([[-1.8495],
[ 0.0641]]),
tensor([[ 1.0000e+00, -1.8495e+00, 1.7103e+00, -1.0544e+00, 4.8750e-01,
-1.8032e-01, 5.5584e-02, -1.4686e-02, 3.3951e-03, -6.9769e-04,
1.2904e-04, -2.1695e-05, 3.3437e-06, -4.7570e-07, 6.2843e-08,
-7.7484e-09, 8.9565e-10, -9.7440e-11, 1.0012e-11, -9.7455e-13],
[ 1.0000e+00, 6.4110e-02, 2.0550e-03, 4.3916e-05, 7.0386e-07,
9.0249e-09, 9.6431e-11, 8.8316e-13, 7.0774e-15, 5.0415e-17,
3.2321e-19, 1.8837e-21, 1.0064e-23, 4.9629e-26, 2.2727e-28,
9.7133e-31, 3.8920e-33, 1.4677e-35, 5.2276e-38, 1.7639e-40]]),
tensor([-9.0335, 5.1258]))
.. raw:: html

.. raw:: html
.. code:: python
# Convert from NumPy ndarrays to tensors
true_w, features, poly_features, labels = [
tf.constant(x, dtype=tf.float32)
for x in [true_w, features, poly_features, labels]]
features[:2], poly_features[:2, :], labels[:2]
.. parsed-literal::
:class: output
(,
,
)
.. raw:: html

.. raw:: html
.. code:: python
def evaluate_loss(net, data_iter, loss): #@save
"""Evaluate the loss of a model on the given dataset."""
metric = d2l.Accumulator(2) # Sum of losses, no. of examples
for X, y in data_iter:
l = loss(net(X), y)
metric.add(l.sum(), l.size)
return metric[0] / metric[1]
.. raw:: html

.. raw:: html
.. code:: python
def evaluate_loss(net, data_iter, loss): #@save
"""Evaluate the loss of a model on the given dataset."""
metric = d2l.Accumulator(2) # Sum of losses, no. of examples
for X, y in data_iter:
out = net(X)
y = y.reshape(out.shape)
l = loss(out, y)
metric.add(l.sum(), l.numel())
return metric[0] / metric[1]
.. raw:: html

.. raw:: html
.. code:: python
def evaluate_loss(net, data_iter, loss): #@save
"""Evaluate the loss of a model on the given dataset."""
metric = d2l.Accumulator(2) # Sum of losses, no. of examples
for X, y in data_iter:
l = loss(net(X), y)
metric.add(tf.reduce_sum(l), tf.size(l).numpy())
return metric[0] / metric[1]
.. raw:: html

.. raw:: html
.. code:: python
def train(train_features, test_features, train_labels, test_labels,
num_epochs=400):
loss = gluon.loss.L2Loss()
net = nn.Sequential()
# Switch off the bias since we already catered for it in the polynomial
# features
net.add(nn.Dense(1, use_bias=False))
net.initialize()
batch_size = min(10, train_labels.shape[0])
train_iter = d2l.load_array((train_features, train_labels), batch_size)
test_iter = d2l.load_array((test_features, test_labels), batch_size,
is_train=False)
trainer = gluon.Trainer(net.collect_params(), 'sgd',
{'learning_rate': 0.01})
animator = d2l.Animator(xlabel='epoch', ylabel='loss', yscale='log',
xlim=[1, num_epochs], ylim=[1e-3, 1e2],
legend=['train', 'test'])
for epoch in range(num_epochs):
d2l.train_epoch_ch3(net, train_iter, loss, trainer)
if epoch == 0 or (epoch + 1) % 20 == 0:
animator.add(epoch + 1, (evaluate_loss(
net, train_iter, loss), evaluate_loss(net, test_iter, loss)))
print('weight:', net[0].weight.data().asnumpy())
.. raw:: html

.. raw:: html
.. code:: python
def train(train_features, test_features, train_labels, test_labels,
num_epochs=400):
loss = nn.MSELoss()
input_shape = train_features.shape[-1]
# Switch off the bias since we already catered for it in the polynomial
# features
net = nn.Sequential(nn.Linear(input_shape, 1, bias=False))
batch_size = min(10, train_labels.shape[0])
train_iter = d2l.load_array((train_features, train_labels.reshape(-1, 1)),
batch_size)
test_iter = d2l.load_array((test_features, test_labels.reshape(-1, 1)),
batch_size, is_train=False)
trainer = torch.optim.SGD(net.parameters(), lr=0.01)
animator = d2l.Animator(xlabel='epoch', ylabel='loss', yscale='log',
xlim=[1, num_epochs], ylim=[1e-3, 1e2],
legend=['train', 'test'])
for epoch in range(num_epochs):
d2l.train_epoch_ch3(net, train_iter, loss, trainer)
if epoch == 0 or (epoch + 1) % 20 == 0:
animator.add(epoch + 1, (evaluate_loss(
net, train_iter, loss), evaluate_loss(net, test_iter, loss)))
print('weight:', net[0].weight.data.numpy())
.. raw:: html

.. raw:: html
.. code:: python
def train(train_features, test_features, train_labels, test_labels,
num_epochs=400):
loss = tf.losses.MeanSquaredError()
input_shape = train_features.shape[-1]
# Switch off the bias since we already catered for it in the polynomial
# features
net = tf.keras.Sequential()
net.add(tf.keras.layers.Dense(1, use_bias=False))
batch_size = min(10, train_labels.shape[0])
train_iter = d2l.load_array((train_features, train_labels), batch_size)
test_iter = d2l.load_array((test_features, test_labels), batch_size,
is_train=False)
trainer = tf.keras.optimizers.SGD(learning_rate=.01)
animator = d2l.Animator(xlabel='epoch', ylabel='loss', yscale='log',
xlim=[1, num_epochs], ylim=[1e-3, 1e2],
legend=['train', 'test'])
for epoch in range(num_epochs):
d2l.train_epoch_ch3(net, train_iter, loss, trainer)
if epoch == 0 or (epoch + 1) % 20 == 0:
animator.add(epoch + 1, (evaluate_loss(
net, train_iter, loss), evaluate_loss(net, test_iter, loss)))
print('weight:', net.get_weights()[0].T)
.. raw:: html

.. raw:: html
.. code:: python
# Pick the first four dimensions, i.e., 1, x, x^2/2!, x^3/3! from the
# polynomial features
train(poly_features[:n_train, :4], poly_features[n_train:, :4],
labels[:n_train], labels[n_train:])
.. parsed-literal::
:class: output
weight: [[ 5.0191536 1.222012 -3.423569 5.5717473]]
.. figure:: output_underfit-overfit_ec26bd_53_1.svg
.. raw:: html

.. raw:: html
.. code:: python
# Pick the first four dimensions, i.e., 1, x, x^2/2!, x^3/3! from the
# polynomial features
train(poly_features[:n_train, :4], poly_features[n_train:, :4],
labels[:n_train], labels[n_train:])
.. parsed-literal::
:class: output
weight: [[ 5.0116954 1.2056783 -3.416093 5.5694838]]
.. figure:: output_underfit-overfit_ec26bd_56_1.svg
.. raw:: html

.. raw:: html
.. code:: python
# Pick the first four dimensions, i.e., 1, x, x^2/2!, x^3/3! from the
# polynomial features
train(poly_features[:n_train, :4], poly_features[n_train:, :4],
labels[:n_train], labels[n_train:])
.. parsed-literal::
:class: output
weight: [[ 4.9935265 1.2147613 -3.4067297 5.575281 ]]
.. figure:: output_underfit-overfit_ec26bd_59_1.svg
.. raw:: html

.. raw:: html
.. code:: python
# Pick the first two dimensions, i.e., 1, x, from the polynomial features
train(poly_features[:n_train, :2], poly_features[n_train:, :2],
labels[:n_train], labels[n_train:])
.. parsed-literal::
:class: output
weight: [[2.7023997 4.230943 ]]
.. figure:: output_underfit-overfit_ec26bd_65_1.svg
.. raw:: html

.. raw:: html
.. code:: python
# Pick the first two dimensions, i.e., 1, x, from the polynomial features
train(poly_features[:n_train, :2], poly_features[n_train:, :2],
labels[:n_train], labels[n_train:])
.. parsed-literal::
:class: output
weight: [[2.447184 5.4534316]]
.. figure:: output_underfit-overfit_ec26bd_68_1.svg
.. raw:: html

.. raw:: html
.. code:: python
# Pick the first two dimensions, i.e., 1, x, from the polynomial features
train(poly_features[:n_train, :2], poly_features[n_train:, :2],
labels[:n_train], labels[n_train:])
.. parsed-literal::
:class: output
weight: [[3.571197 3.5820608]]
.. figure:: output_underfit-overfit_ec26bd_71_1.svg
.. raw:: html

.. raw:: html
.. code:: python
# Pick all the dimensions from the polynomial features
train(poly_features[:n_train, :], poly_features[n_train:, :],
labels[:n_train], labels[n_train:], num_epochs=1500)
.. parsed-literal::
:class: output
weight: [[ 4.99218 1.3058988 -3.353107 5.1165457 -0.11123804 1.303058
0.12690555 0.16650231 0.05132549 -0.02275909 0.00806515 -0.05167904
-0.02426293 -0.01502211 -0.04941352 0.06389863 -0.04761846 -0.04380166
-0.05188227 0.05655775]]
.. figure:: output_underfit-overfit_ec26bd_77_1.svg
.. raw:: html

.. raw:: html
.. code:: python
# Pick all the dimensions from the polynomial features
train(poly_features[:n_train, :], poly_features[n_train:, :],
labels[:n_train], labels[n_train:], num_epochs=1500)
.. parsed-literal::
:class: output
weight: [[ 5.0303855 1.286089 -3.5051425 5.1785264 0.07445128 0.93172127
0.38106453 -0.20703262 0.3148974 -0.16491619 -0.10493812 -0.18088949
0.09067045 -0.0823404 -0.1541275 0.11026925 -0.18319678 -0.15563305
0.17545709 0.10848124]]
.. figure:: output_underfit-overfit_ec26bd_80_1.svg
.. raw:: html

.. raw:: html
.. code:: python
# Pick all the dimensions from the polynomial features
train(poly_features[:n_train, :], poly_features[n_train:, :],
labels[:n_train], labels[n_train:], num_epochs=1500)
.. parsed-literal::
:class: output
weight: [[ 5.0005884 1.3086032 -3.4462366 5.060991 0.13565479 1.198534
0.02681205 0.604673 -0.44458857 0.4510999 -0.15569992 -0.18975744
0.24333505 0.21159105 0.3733602 0.28223094 0.27522564 0.4618569
-0.4253615 0.25948358]]
.. figure:: output_underfit-overfit_ec26bd_83_1.svg
.. raw:: html

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

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

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

.. raw:: html