1

我正在使用 quadprog 模块来设置用于语音识别的 SVM。我从这里获取了一个 QP 实现:https ://github.com/stephane-caron/qpsolvers/blob/master/qpsolvers/quadprog_.py

这是他们的实现:

def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None,
                  verbose=False):

if initvals is not None:
    print("quadprog: note that warm-start values ignored by wrapper")
qp_G = P
qp_a = -q
if A is not None:
    if G is None:
        qp_C = -A.T
        qp_b = -b
    else:
        qp_C = -vstack([A, G]).T

        qp_b = -np.insert(h, 0, 0, axis=0)


    meq = A.shape[0]
else:  # no equality constraint
    qp_C = -G.T if G is not None else None
    qp_b = -h if h is not None else None
    meq = 0
try:
    
    return solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0]
except ValueError as e:
    if "matrix G is not positive definite" in str(e):
        # quadprog writes G the cost matrix that we write P in this package
        raise ValueError("matrix P is not positive definite")
    raise

形状:

P: (127, 127)
h: (254, 1)
q: (127, 1)
A: (1, 127)
G: (254, 127)

我也有它qp_b最初被分配给hstack一个数组arr = array([0])h但形状为:(1,)防止 numpy 连接数组。我通过插入[0] 来修复此错误。

当我尝试时,quadprog_solve_qp(P, q, G, h, A)我得到:

File "----------------------------.py", line 95, in quadprog_solve_qp
    return solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0]

  File "quadprog/quadprog.pyx", line 12, in quadprog.solve_qp

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

而且我不知道它来自哪里,也不知道我能做什么。如果有人知道 quadprog 模块是如何工作的,或者只是我可能做错了什么,我会很高兴听到。

4

0 回答 0