[英]Finding the Hessian matrix of this function
Hi I have the following function:嗨,我有以下 function:
sum from 1 to 5000 -log(1−(xi)^2) -log(1-(a_i)^t*x), where a_i is a random vector and we are trying to minimize this function's value via Netwon's method.从 1 到 5000 的总和 -log(1−(xi)^2) -log(1-(a_i)^t*x),其中 a_i 是一个随机向量,我们试图通过 Netwon 的方法最小化这个函数的值。
I need a way to calculate the Hessian matrix with respect to (x1, x2, x3, ...).我需要一种方法来计算关于 (x1, x2, x3, ...) 的 Hessian 矩阵。 I tried auto-gradient but it took too much time.
我尝试了自动渐变,但花了太多时间。 Here is my current time.
这是我现在的时间。
from autograd import elementwise_grad as egrad
from autograd import jacobian
import autograd.numpy as np
x=np.zeros(5000);
a = np.random.rand(5000,5000)
def f (x):
sum = 0;
for i in range(5000):
sum += -np.log(1 - x[i]*x[i]) - np.log(1-np.dot(x,a[i]))
return sum;
df = egrad(f)
d2f = jacobian(egrad(df));
print(d2f(x));
I have tried looking into sympy but I am confused on how to proceed.我曾尝试调查 sympy,但我对如何继续感到困惑。
You can use the regular NumPy
vectorization array operations which will speed up significantly the execution of the program:您可以使用常规的
NumPy
向量化数组操作,这将显着加快程序的执行速度:
from autograd import elementwise_grad as egrad
from autograd import jacobian
import autograd.numpy as np
from time import time
import datetime
n = 5000
x = np.zeros(n)
a = np.random.rand(n, n)
f = lambda x: -1 * np.sum(np.log(1-x**2) + np.log(1-np.dot(a, x)))
t_start = time()
df = egrad(f)
d2f = jacobian(egrad(df))
t_end = time() - t_start
print('Execution time: ', datetime.datetime.fromtimestamp(t_end).strftime('%H:%M:%S'))
Output Output
Execution time: 02:02:27
In general, every time you deal with numeric data, you should avoid by all means using loops
for calculations, as they usually become the bottleneck of the program due to the their header and the maintenance of the counter variable.一般来说,每次处理数值数据时,都应该避免使用
loops
进行计算,因为它们通常会成为程序的瓶颈,因为它们的 header 和计数器变量的维护。
NumPy
on the other hand, uses a very short header for each array
, and is highly optimized, as you'd expect, for numeric calculations.另一方面, Z3B7F949B2343F9E5390
NumPy
对每个array
使用非常短的 header ,并且正如您所期望的那样,针对数值计算进行了高度优化。
Note the x**2
which squares every item of x
instead of x[i]*x[i]
, and the np.dot(a, x)
which performs the np.dot(x, a[i])
in just one command (where x
and a
switch places to fit the required dimensions).请注意
x**2
将 x 的每个项目平方而不是x
x[i]*x[i]
,以及仅执行np.dot(x, a[i])
的np.dot(a, x)
一个命令(其中x
和a
开关放置以适应所需的尺寸)。
You can refer to this great e-book which will explain this point in a greater detail.您可以参考这本很棒的电子书,它将更详细地解释这一点。
Cheers干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.