示例#1
0
for λ in torch.range(.1, 2, .3):
    train(float(λ), animator)

for λ in torch.range(2, 20, 5):
    train(float(λ), animator)

plt.show()
"""
# ----------------------------------------------------------------------------
# 1.1.2
    # What do you observe?  Use a validation set to find the optimal value of λ.
    # Is it really the optimal value? Does this matter?
n_train, n_test, num_inputs, batch_size = 20, 100, 200, 5
n_eval = 20
true_w, true_b = torch.ones((num_inputs, 1)) * 0.01, 0.05
train_data = d2l.synthetic_data(true_w, true_b, n_train)
train_iter = d2l.load_array(train_data, batch_size)
test_data = d2l.synthetic_data(true_w, true_b, n_test)
test_iter = d2l.load_array(test_data, batch_size, is_train=False)
eval_data = d2l.synthetic_data(true_w, true_b, n_eval)
eval_iter = d2l.load_array(eval_data, batch_size, is_train=False)
eval_epochs, num_epochs = 5, 10

def init_params():
    w = torch.normal(0, 1, size=(num_inputs, 1), requires_grad=True)
    b = torch.zeros(1, requires_grad=True)
    λ = torch.zeros(1, requires_grad=True)
    return [w, b, λ]

def sgd(params, lr, batch_size):
    with torch.no_grad():
示例#2
0





#### Simple Version

from d2l import torch as d2l
import numpy as np
import torch
from torch.utils import data

true_w = torch.tensor([2, -3.4])
true_b = 4.2
features, labels = d2l.synthetic_data(true_w, true_b, 1000)




def load_array(data_arrays, batch_size, is_train=True):  #@save
    """Construct a PyTorch data iterator."""
    dataset = data.TensorDataset(*data_arrays)
    return data.DataLoader(dataset, batch_size, shuffle=is_train)

batch_size = 10
data_iter = load_array((features, labels), batch_size)



next(iter(data_iter))