Python 检查变量中的更改而不复制
我有一个迭代更新算法(想想梯度下降法),当变量Python 检查变量中的更改而不复制,python,arrays,variables,copy,Python,Arrays,Variables,Copy,我有一个迭代更新算法(想想梯度下降法),当变量x的变化小于某个公差tol时,我停止算法 看起来像 def algorithm(x): for t in range(iterations): phi = update_phi(some_variables) temp = copy.deepcopy(x) x = update_x(x, phi) if dist(x,temp)<tol: bre
x
的变化小于某个公差tol
时,我停止算法
看起来像
def algorithm(x):
for t in range(iterations):
phi = update_phi(some_variables)
temp = copy.deepcopy(x)
x = update_x(x, phi)
if dist(x,temp)<tol:
break
其中,phi
是三维阵列(即具有形状(n,m,l)
)。距离dist(x,y)
是向量之间的欧几里德距离
如果我们试图在更新过程中计算变化,它仍然归结为
phi中的变化
,查看更新
代码会有所帮助。它能在运行过程中计算距离并返回该值以及x吗?Samwise补充道,这是一个很好的建议。
def update_x(x, phi):
for i in range(len(x)):
x[i] = const + np.sum(phi[i], axis = 0)
return x