<jupyter_start><jupyter_text>Refs:https://github.com/deep-learning-with-pytorch/dlwpt-code<jupyter_code>import torch
torch.set_printoptions(edgeitems=2, linewidth=75)
x = [35.7, 55.9, 58.2, 81.9, 56.3, 48.9, 33.9, 21.8, 48.4, 60.4, 68.4]
y = [0.5,  14.0, 15.0, 28.0, 11.0,  8.0,  3.0, -4.0,  6.0, 13.0, 21.0]

x = torch.tensor(x)
y = torch.tensor(y)
def model(x, w, b):
    return w * x + b
def loss_fn(y_p, y):
    squared_diffs = (y_p - y)**2
    return squared_diffs.mean()
w = torch.ones(())
b = torch.zeros(())

y_p = model(x, w, b)
y_p
loss = loss_fn(y_p, y)
loss
def dloss_fn(y_p, y):
    dsq_diffs = 2*(y_p - y)/y_p.size(0)  # <1>
    return dsq_diffs
def dmodel_dw(x, w, b):
    return x
def dmodel_db(x, w, b):
    return 1.0
def grad_fn(x, y, y_p, w, b):
    dloss_dyp = dloss_fn(y_p, y)
    dloss_dw = dloss_dyp * dmodel_dw(x, w, b)
    dloss_db = dloss_dyp * dmodel_db(x, w, b)
    return torch.stack([dloss_dw.sum(), dloss_db.sum()])  # <1>
def training_loop(n_epochs, learning_rate, params, x, y, print_params=True):
    
    for epoch in range(1, n_epochs + 1):
        w, b = params

        y_p = model(x, w, b)  # <1>
        loss = loss_fn(y_p, y)
        grad = grad_fn(x, y, y_p, w, b)  # <2>

        params = params - learning_rate * grad

        if epoch in {1, 2, 3, 10, 11, 99, 100, 4000, 5000}:  # <3>
            print('Epoch %d, Loss %f' % (epoch, float(loss)))
            if print_params:
                print('    Params:', params)
                print('    Grad:  ', grad)
        if epoch in {4, 12, 101}:
            print('...')

        if not torch.isfinite(loss).all():
            break  # <3>
            
    return params
# normalize
x = 0.1 * x
params = training_loop(
    n_epochs = 5000, 
    learning_rate = 1e-3, 
    params = torch.tensor([1.0, 0.0]), 
    x = x, 
    y = y,
    print_params = False)

params
%matplotlib inline
from matplotlib import pyplot as plt

y_p = model(x, *params)  # <1>

fig = plt.figure(dpi=400)
plt.xlabel("x")
plt.ylabel("y_p")
plt.plot(x.numpy(), y_p.detach().numpy()) # <2>
plt.plot(x.numpy(), y.numpy(), 'o')
#plt.savefig("temp_unknown_plot.png", format="png")  # bookskip<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17587939698492464, 'fraction_numerical': 0.05481955230698949, 'mean_word_length': 2.5609756097560976, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '324336', 'license': 'MIT', 'n_tokens_mistral': 1041, 'n_tokens_neox': 961, 'n_words': 226, 'path': 'Tutorial/.ipynb_checkpoints/c5_gradient_descent-checkpoint.ipynb', 'repo_name': 'danhtaihoang/pytorch-deeplearning', 'size': '2189'}