向量範數與矩陣範數

向量範數與矩陣範數

來自專欄 機器學習

在神經網路出現overfit的時候,通常採用regularization的方式來解決過擬合,包括:

  1. L1/L2正則化
  2. Dropout正則化

在L1/L2正則化中分別用到了L1範數和L2範數,本篇我們對常見的向量範數和矩陣範數進行總結。


1. 向量範數

對於向量 x=[x_1,x_2,...,x_m] ,常用的範數包括

1-範數

||x||_1=sum_{i=1}^{m}{|x_i|} ,即向量元素絕對值之和,x 到零點的曼哈頓距離

2-範數

||x||_1=sqrt{sum_{i=1}^{m}{x_i^2}} ,2-範數也稱為Euclid範數(歐幾里得範數,常用計算向量長度),即向量元素絕對值的平方和再開方,表示x到零點的歐式距離

p-範數

||x||_p=(sum_{i=1}^{m}|x_i|^p)^{frac{1}{p}} ,即向量元素絕對值的p次方和的1/p次冪,表示x到零點的p階閔氏距離。

infty -範數

||x||_infty=max_i |x_i| ,當p趨向於正無窮時,即所有向量元素絕對值中的最大值

-infty -範數

||x||_{-infty}=min_i |x_i| ,當p趨向於負無窮時,即所有向量元素絕對值中的最小值

0-範數

零範數即是當p趨於零,可以證明這時候的極限(x_1^p + x_2^p + cdots + x_m^p)^{frac{1}{p}}恰好是向量x=(x_1,x_2,cdots,x_n)^{T}非零元素的個數。


2. 矩陣範數

對於矩陣 A in R^{m	imes n}

1-範數

||A||_1=max_jsum_{i=1}^{m}{|a_{i,j}|} ,列和範數,即所有矩陣列向量絕對值之和的最大值

2-範數

||A||_2=sqrt{lambda_1}lambda_1 表示 A^TA 的最大特徵值,稱為譜範數

infty -範數

||A||_infty=max_jsum_{j=1}^{m}{|a_{i,j}|} ,稱為行和範數,即所有矩陣行向量絕對值之和的最大值

F-範數

||A||_F=(sum_{i=1}^{m}{sum_{j=1}^{n}a_{i,j}^2})^{frac{1}{2}} ,稱為Frobenius範數,即矩陣元素絕對值的平方和再開平方


3. Numpy計算範數

numpy包里的linalg模塊,是專門處理基本線性代數問題的模塊。藉助該模塊中的norm()函數可以輕鬆計算向量與矩陣的範數。

norm函數說明如下所示:

def norm(x, ord=None, axis=None, keepdims=False): Matrix or vector norm. This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ``ord`` parameter. The following norms can be calculated: ===== ============================ ========================== ord norm for matrices norm for vectors ===== ============================ ========================== None Frobenius norm 2-norm fro Frobenius norm -- nuc nuclear norm -- inf max(sum(abs(x), axis=1)) max(abs(x)) -inf min(sum(abs(x), axis=1)) min(abs(x)) 0 -- sum(x != 0) 1 max(sum(abs(x), axis=0)) as below -1 min(sum(abs(x), axis=0)) as below 2 2-norm (largest sing. value) as below -2 smallest singular value as below other -- sum(abs(x)**ord)**(1./ord) ===== ============================ ==========================

運行一些例子如下所示:

import numpy as npimport numpy.linalg as LAdef compute_norm(): mat = np.matrix([[1,2],[3,4]]) inv_mat = np.linalg.inv(mat) print inv_matdef vector_norm(): a = np.arange(9) - 4 print a print LA.norm(a,np.inf) #無窮範數 print LA.norm(a,-np.inf) print LA.norm(a,1) #1範數 print LA.norm(a,2) #2範數def matrix_norm(): a = np.arange(9) - 4 b = a.reshape(3,3) b_t = np.transpose(b) b_new = np.dot(b_t,b) #b_new矩陣為b^t * b x = np.linalg.eigvals(b_new) #求b_new矩陣的特徵值 print x print LA.norm(b,1) #列範數 print LA.norm(b,2) #譜範數,為x里最大值開平方 print LA.norm(b,np.inf) #無窮範數,行範數 print LA.norm(b,"fro") #F範數vector_norm()print matrix_norm()# 向量範數[-4 -3 -2 -1 0 1 2 3 4]4.00.020.07.74596669241#矩陣範數[[-4 -3 -2] [-1 0 1] [ 2 3 4]][ 5.40000000e+01 6.00000000e+00 5.99792822e-16]7.07.348469228359.07.74596669241

推薦閱讀:

吳恩達機器學習第三周課後感
機器學習入門筆記4
知乎問題標籤預測開放數據集上線啦
斯坦福大學機器學習課程(介紹和線性回歸)

TAG:機器學習 | 數學 |