.. 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([[-0.0251],
[ 0.5741]]),
tensor([[ 1.0000e+00, -2.5148e-02, 3.1620e-04, -2.6506e-06, 1.6664e-08,
-8.3813e-11, 3.5129e-13, -1.2620e-15, 3.9671e-18, -1.1085e-20,
2.7876e-23, -6.3728e-26, 1.3355e-28, -2.5835e-31, 4.6406e-34,
-7.7801e-37, 1.2228e-39, -1.8091e-42, 2.8026e-45, -0.0000e+00],
[ 1.0000e+00, 5.7413e-01, 1.6481e-01, 3.1541e-02, 4.5272e-03,
5.1984e-04, 4.9742e-05, 4.0798e-06, 2.9279e-07, 1.8678e-08,
1.0723e-09, 5.5969e-11, 2.6778e-12, 1.1826e-13, 4.8498e-15,
1.8563e-16, 6.6609e-18, 2.2495e-19, 7.1751e-21, 2.1681e-22]]),
tensor([5.1817, 5.3689]))
.. 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.0191045 1.2220526 -3.4236722 5.5719056]]
.. 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: [[ 4.973806 1.2689306 -3.3781712 5.499426 ]]
.. 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.9894967 1.1841421 -3.3892994 5.606123 ]]
.. 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.7026136 4.226884 ]]
.. 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: [[3.7069585 3.095555 ]]
.. 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.0591443 4.3200784]]
.. 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.992259 1.3061745 -3.353206 5.11689 -0.1113766 1.3029786
0.12686245 0.16647518 0.05131551 -0.02276107 0.00806388 -0.05167878
-0.02426295 -0.0150221 -0.04941357 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: [[ 4.97878 1.2591952 -3.3576016 5.3477707 -0.25378734 1.0585515
0.00885677 0.2865524 -0.00880263 -0.02626877 0.04555864 -0.00776612
-0.13054115 0.0106086 0.05013648 -0.16826996 -0.07351876 0.12042709
-0.13315001 0.00572934]]
.. 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: [[ 4.989105 1.229152 -3.3774624 5.3754754 -0.1008428 0.6542578
0.63709617 0.3373214 0.55007446 0.24387589 0.11496052 0.45547286
-0.3716535 0.31222677 0.3570863 -0.23200653 0.51337767 0.14197758
-0.0198204 0.23797804]]
.. 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