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')
'''
99 4164.08349609375
199 2228.770263671875
299 1261.802978515625
399 790.7125244140625
499 530.3314819335938
599 348.21429443359375
699 217.8380584716797
799 127.3868408203125
899 68.13304138183594
999 33.380455017089844
1099 16.761796951293945
1199 10.3683500289917
1299 9.076005935668945
1399 8.968759536743164
1499 8.895014762878418
1599 8.914703369140625
1699 8.91008186340332
1799 8.922473907470703
1899 8.916147232055664
1999 8.931474685668945
Result: y = 0.0006827712059020996 + 0.8572337031364441 x + 0.000728971092030406 x^2 + -0.09283709526062012 x^3
'''
|