给定
H
∈
R
d
×
n
H\in\R^{d\times n}
H∈Rd×n,要解:
min
?
Z
∥
H
?
H
Z
∥
F
2
+
α
∥
Z
∥
1
s
.
t
.
?diag
(
Z
)
=
0
\min_Z\parallel H-HZ\parallel_F^2+\alpha\parallel Z\parallel_1 \\ s.t.\ \text{diag}(Z)=0
Zmin?∥H?HZ∥F2?+α∥Z∥1?s.t.?diag(Z)=0 拆开每一列单独求解,变成:
min
?
z
i
∥
h
i
?
H
^
z
i
∥
2
2
+
α
∥
z
i
∥
1
\min_{z_i}\parallel h_i-\hat Hz_i\parallel_2^2+\alpha\parallel z_i\parallel_1
zi?min?∥hi??H^zi?∥22?+α∥zi?∥1? 其中
H
^
\hat H
H^ 是把 H 第 i 列
h
i
h_i
hi? 换成全 0。即带 L1 范数正则的最小二乘(L1-norm regularized least-squares)[1,2],可以用 cvxopt 包配合 [3] 实现好的 l1regls 求。
Preliminary
安装见 [4],conda、pip 都行。
mutual conversion
numpy.ndarray 和 cvxopt.matrix 之间的相互转换,参考 [5,6,7]。
import cvxopt
import numpy as np
H_np = np.arange(12).reshape(3, 4)
print(H_np)
H_cvx = cvxopt.matrix(H_np)
print(H_cvx)
print(np.array(H_cvx))
Code
- 注意 H 要排成列向量。
- float 型 array 的
dtype 要转成 np.double 再转 cvxopt.matrix ,见 [7]。 - [3] 要单独下载,且在开头加上
solvers.options['show_progress'] = False 让 solver 求解时不要 bb[9]。
import struct
import cvxopt
import numpy as np
import l1regls
"""L1 regularized least square
min || H - HZ ||_2 + || Z ||_1
"""
H = np.random.permutation(12).reshape(2, 6).astype(np.double)
d, n = H.shape
print("H:\n", H)
Z = np.zeros([n, n], dtype=np.double)
for i in range(n):
h = H[:, i]
_mask = np.ones_like(H[0:1])
_mask[0][i] = 0
H_masked = H * _mask
H_masked = cvxopt.matrix(H_masked)
h = cvxopt.matrix(h)
z = l1regls.l1regls(H_masked, h)
z = np.array(z)
Z[:, i] = z.flatten()
print("Z:\n", Z)
print("recon H:\n", H.dot(Z))
H:
[[ 6. 11. 4. 7. 8. 1.]
[10. 0. 9. 3. 2. 5.]]
Z:
[[ 0.00000000e+00 -6.98943781e-06 8.92271576e-01 2.97727171e-01
1.97727272e-01 1.59974460e-06]
[ 1.38700201e-01 0.00000000e+00 -1.18924004e-01 4.69834622e-01
6.15289256e-01 -1.03918158e-01]
[ 1.10717194e+00 -3.29324447e-01 0.00000000e+00 6.28937736e-08
4.00154515e-10 5.47136286e-01]
[ 1.66159302e-05 1.62159367e-05 -4.83780134e-07 0.00000000e+00
1.43251121e-09 -1.72704896e-08]
[ 4.66304188e-06 1.52891479e+00 -1.34402595e-06 1.63529549e-07
0.00000000e+00 -4.41351100e-08]
[ 7.52874506e-06 -2.84235943e-05 3.14914688e-06 2.54879563e-08
3.46453896e-10 0.00000000e+00]]
recon H:
[[ 5.95455111 10.91406371 4.04545442 6.95454546 7.95454545 1.04545454]
[ 9.96464426 0.0937462 8.92272736 2.97727274 1.97727273 4.92424243]]
References
- L1-norm regularized least-squares
- L1-norm regularized least-squares on Python
- l1regls.py
- Installation instructions
- Numpy and CVXOPT
- python3 conversion between cvxopt.matrix and numpy.array
- converting numpy vector to cvxopt
- Fast l1-minimization algorithms and an application in robust face recognition: A review
- How to silent cvxopt solver [Python]?
- torch.lstsq
|