This commit is contained in:
parent
284552ff0b
commit
6da2a1a9c4
|
@ -0,0 +1,39 @@
|
|||
import torch
|
||||
import torch.optim
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def f(x):
|
||||
return x**2-3
|
||||
|
||||
def df(x):
|
||||
return 2*x
|
||||
def plotf(loss):
|
||||
x = range(len(loss))
|
||||
plt.plot(x,loss)
|
||||
plt.xlabel('Iteration')
|
||||
plt.ylabel('Loss')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
x = torch.tensor([15.],requires_grad=True)
|
||||
optimizer = torch.optim.SGD([x,],lr = 0.1,momentum=0.9)
|
||||
steps = 400
|
||||
loss = []
|
||||
for i in range(steps):
|
||||
optimizer.zero_grad()
|
||||
f(x).backward()
|
||||
optimizer.step()
|
||||
loss.append(f(x))
|
||||
print(loss[i])
|
||||
y = f(x)
|
||||
print("函数最小值是: ",y)
|
||||
plotf(loss)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
import torch
|
||||
from torch.autograd import Variable
|
||||
def f(x):
|
||||
y = x ** 2 -3
|
||||
return y
|
||||
x = Variable(torch.Tensor([15]), requires_grad=True)
|
||||
for i in range(40):
|
||||
grad_x = torch.autograd.grad(f(x), x, create_graph=True)
|
||||
grad_grad_x = torch.autograd.grad(grad_x[0], x)
|
||||
x = Variable(x.data - grad_x[0].data / grad_grad_x[0].data, requires_grad=True)
|
||||
print(x)
|
Loading…
Reference in New Issue