根据亚当的伪代码:
我写了一些代码:
from matplotlib import pyplot as plt
import numpy as np
# np.random.seed(42)
num = 100
x = np.arange(num).tolist()
# The following 3 sets of g_list stand for 3 types of gradient changes:
# g_list = np.random.normal(0,1,num) # gradient direction changes frequently in positive and negtive
# g_list = x # gradient direction always positive and gradient value becomes larger gradually
g_list = [10 for _ in range(num)] # gradient direction always positive and gradient value always the same
m = 0
v = 0
beta_m = 0.9
beta_v = 0.999
m_list = []
v_list = []
for i in range(1,num+1):
g = g_list[i-1]
m = beta_m*m + (1 - beta_m)*g
m = m/(1-beta_m**i)
v = beta_v*v + (1 - beta_v)*(g**2)
v = v/(1-beta_v**i)
m_list.append(m)
v_list.append(np.sqrt(v))
mv = np.array(m_list)/(np.array(v_list) +0.001)
print("==>> mv: ", mv)
plt.plot(x, g_list, x, mv)
运行代码,我得到以下情节:
对我来说,我认为是反直觉的,因为我认为当梯度方向始终为正,梯度值恒定时,学习率的系数(即 mv)应该接近 1,但mv
我得到的第 100是 3.40488818e-70,几乎接近于零。
如果我更改一些代码:
# m = m/(1-beta_m**i)
if i == 1:
m = m/(1-beta_m**i)
# v = v/(1-beta_v**i)
if i == 1:
v = v/(1-beta_v**i)
我得到的结果是这样的:
这更符合我的直觉。
有人可以告诉我上面的代码是否正确,如果正确,是否符合您的直觉来获得类似上面的内容?