23 lines
336 B
Python
23 lines
336 B
Python
|
#!/usr/bin/python3
|
||
|
|
||
|
import torch
|
||
|
|
||
|
|
||
|
def fun(x):
|
||
|
return 2*x**4 - x**3 + 3.5*x + 10
|
||
|
|
||
|
|
||
|
x = torch.tensor(5., requires_grad=True)
|
||
|
|
||
|
learning_rate = torch.tensor(0.01)
|
||
|
|
||
|
for _ in range(100):
|
||
|
y = fun(x)
|
||
|
print(x, " => ", y)
|
||
|
y.backward()
|
||
|
|
||
|
with torch.no_grad():
|
||
|
x = x - learning_rate * x.grad
|
||
|
|
||
|
x.requires_grad_(True)
|