创建形式为A.T * diag(b)* A + C的稀疏矩阵的最快方法 [英] Fastest way to create a sparse matrix of the form A.T * diag(b) * A + C?

查看:102
本文介绍了创建形式为A.T * diag(b)* A + C的稀疏矩阵的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试优化一段使用内点方法解决大型稀疏非线性系统的代码.在更新步骤中,这涉及到计算Hessian矩阵H,梯度g,然后求解H * d = -g中的d以获得新的搜索方向.

I'm trying to optimize a piece of code that solves a large sparse nonlinear system using an interior point method. During the update step, this involves computing the Hessian matrix H, the gradient g, then solving for d in H * d = -g to get the new search direction.

Hessian矩阵具有以下形式的对称三对角结构:

The Hessian matrix has a symmetric tridiagonal structure of the form:

A.T * diag(b)* A + C

我已在有问题的特定功能上运行 line_profiler :

I've run line_profiler on the particular function in question:

Line # Hits     Time  Per Hit % Time Line Contents
==================================================
   386                               def _direction(n, res, M, Hsig, scale_var, grad_lnprior, z, fac):
   387                               
   388                                   # gradient
   389   44  1241715  28220.8    3.7     g = 2 * scale_var * res - grad_lnprior + z * np.dot(M.T, 1. / n)
   390                               
   391                                   # hessian
   392   44  3103117  70525.4    9.3     N = sparse.diags(1. / n ** 2, 0, format=FMT, dtype=DTYPE)
   393   44 18814307 427597.9   56.2     H = - Hsig - z * np.dot(M.T, np.dot(N, M))    # slow!
   394                                   
   395                                   # update direction
   396   44 10329556 234762.6   30.8     d, fac = my_solver(H, -g, fac)
   397                                   
   398   44      111      2.5    0.0     return d, fac

从输出中可以明显看出,构造H是迄今为止最昂贵的步骤-与实际解决新方向相比,它花费的时间要长得多.

Looking at the output it's clear that constructing H is by far the most costly step - it takes considerably longer than actually solving for the new direction.

HsigM都是CSC稀疏矩阵,n是密集向量,z是标量.我正在使用的求解器要求H是CSC或CSR稀疏矩阵.

Hsig and M are both CSC sparse matrices, n is a dense vector and z is a scalar. The solver I'm using requires H to be either a CSC or CSR sparse matrix.

这是一个函数,可以生成一些玩具数据,这些数据的格式,尺寸和稀疏度与我的真实矩阵相同:

Here's a function that produces some toy data with the same formats, dimensions and sparseness as my real matrices:

import numpy as np
from scipy import sparse

def make_toy_data(nt=200000, nc=10):

    d0 = np.random.randn(nc * (nt - 1))
    d1 = np.random.randn(nc * (nt - 1))
    M = sparse.diags((d0, d1), (0, nc), shape=(nc * (nt - 1), nc * nt),
                     format='csc', dtype=np.float64)

    d0 = np.random.randn(nc * nt)
    Hsig = sparse.diags(d0, 0, shape=(nc * nt, nc * nt), format='csc',
                        dtype=np.float64)

    n = np.random.randn(nc * (nt - 1))
    z = np.random.randn()

    return Hsig, M, n, z

这是构造H的原始方法:

def original(Hsig, M, n, z):
    N = sparse.diags(1. / n ** 2, 0, format='csc')
    H = - Hsig - z * np.dot(M.T, np.dot(N, M))    # slow!
    return H

时间:

%timeit original(Hsig, M, n, z)
# 1 loops, best of 3: 483 ms per loop

有没有更快的方法来构造这个矩阵?

Is there a faster way to construct this matrix?

推荐答案

在计算三个对角线数组中的乘积M.T * D * M时,我的速度接近4倍.如果d0d1M的主对角线和上对角线,而dD的主对角线,则以下代码将直接创建M.T * D * M:

I get close to a 4x speed-up in computing the product M.T * D * M out of the three diagonal arrays. If d0 and d1 are the main and upper diagonal of M, and d is the main diagonal of D, then the following code creates M.T * D * M directly:

def make_tridi_bis(d0, d1, d, nc=10):
    d00 = d0*d0*d
    d11 = d1*d1*d
    d01 = d0*d1*d
    len_ = d0.size
    data = np.empty((3*len_ + nc,))
    indices = np.empty((3*len_ + nc,), dtype=np.int)
    # Fill main diagonal
    data[:2*nc:2] = d00[:nc]
    indices[:2*nc:2] = np.arange(nc)
    data[2*nc+1:-2*nc:3] = d00[nc:] + d11[:-nc]
    indices[2*nc+1:-2*nc:3] = np.arange(nc, len_)
    data[-2*nc+1::2] = d11[-nc:]
    indices[-2*nc+1::2] = np.arange(len_, len_ + nc)
    # Fill top diagonal
    data[1:2*nc:2] = d01[:nc]
    indices[1:2*nc:2] = np.arange(nc, 2*nc)
    data[2*nc+2:-2*nc:3] = d01[nc:]
    indices[2*nc+2:-2*nc:3] = np.arange(2*nc, len_+nc)
    # Fill bottom diagonal
    data[2*nc:-2*nc:3] = d01[:-nc]
    indices[2*nc:-2*nc:3] = np.arange(len_ - nc)
    data[-2*nc::2] = d01[-nc:]
    indices[-2*nc::2] = np.arange(len_ - nc ,len_)

    indptr = np.empty((len_ + nc + 1,), dtype=np.int)
    indptr[0] = 0
    indptr[1:nc+1] = 2
    indptr[nc+1:len_+1] = 3
    indptr[-nc:] = 2
    np.cumsum(indptr, out=indptr)

    return sparse.csr_matrix((data, indices, indptr), shape=(len_+nc, len_+nc))

如果矩阵M为CSR格式,则可以将d0d1提取为d0 = M.data[::2]d1 = M.data[1::2],我还修改了玩具数据制作例程以返回这些数组,这就是我得到:

If your matrix M were in CSR format, you can extract d0 and d1 as d0 = M.data[::2] and d1 = M.data[1::2], I modified you toy data making routine to return those arrays as well, and here's what I get:

In [90]: np.allclose((M.T * sparse.diags(d, 0) * M).A, make_tridi_bis(d0, d1, d).A)
Out[90]: True

In [92]: %timeit make_tridi_bis(d0, d1, d)
10 loops, best of 3: 124 ms per loop

In [93]: %timeit M.T * sparse.diags(d, 0) * M
1 loops, best of 3: 501 ms per loop


以上代码的全部目的是利用非零条目的结构.如果您绘制要相乘的矩阵图,那么说服自己相信所得三对角矩阵的主要对角线(d_0)和顶部和底部对角线(d_1)相对简单:


The whole purpose of the above code is to take advantage of the structure of the non-zero entries. If you draw a diagram of the matrices you are multiplying together, it is relatively easy to convince yourself that the main (d_0) and top and bottom (d_1) diagonals of the resulting tridiagonal matrix are simply:

d_0 = np.zeros((len_ + nc,))
d_0[:len_] = d00
d_0[-len_:] += d11

d_1 = d01

该函数中的其余代码只是直接构建三对角矩阵,因为使用上述数据调用sparse.diags会慢几倍.

The rest of the code in that function is simply building the tridiagonal matrix directly, as calling sparse.diags with the above data is several times slower.

这篇关于创建形式为A.T * diag(b)* A + C的稀疏矩阵的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆