import copy
import numpy as np
from functools import reduce
DELTA = 0.001
def fun(x):
return x[0]**2+x[1]**2
def partial(f, x, d):
_x = copy.copy(x)
x_ = copy.copy(x)
_x[d] -= DELTA
x_[d] += DELTA
return (f(x_) - f(_x))/(2*DELTA)
y=[]
def down(f,x,lr,decay):
"""
多元函数f在积分区间interval上的定积分
:param f: 函数
:param x: 求梯度的位置,值为向量
:param lr: 学习率
:param decay: 衰减率
:return: 最值的点的位置,值为向量
"""
for i in range(2):
t=1
a=partial(f, x, i)
x[i]-=a*lr/(1+decay*t)
b=partial(f, x, i)
while abs(a-b)>0.00000001:
t=t+1
a=b
x[i]-=a*lr/(1+decay*t)
b=partial(f, x, i)
y.append(x[i])
return y
z=[9,9]
r=down(fun,z,1,0.5)
h=fun(r)
print(r)
print(h)
python实现最速下降算法的时候,同样的for循环两次出来的点竟然不一样,
不知道为什么求助、、