1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
import torch import math
x = torch.linspace(-math.pi, math.pi, 2000) y = torch.sin(x)
p = torch.tensor([1, 2, 3]) xx = x.unsqueeze(-1).pow(p)
model = torch.nn.Sequential( torch.nn.Linear(3, 1), torch.nn.Flatten(0, 1) ) loss_fn = torch.nn.MSELoss(reduction='sum')
learning_rate = 1e-3 optimizer = torch.optim.RMSprop(model.parameters(), lr=learning_rate) for t in range(2000): y_pred = model(xx)
loss = loss_fn(y_pred, y) if t % 100 == 99: print(t, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
linear_layer = model[0] print(f'Result: y = {linear_layer.bias.item()} + {linear_layer.weight[:, 0].item()} x + {linear_layer.weight[:, 1].item()} x^2 + {linear_layer.weight[:, 2].item()} x^3')
|