[論文分享] DHP: Differentiable Meta Pruning via HyperNetworks

xiaqiu發表於2020-12-28

authors: Yawei Li1, Shuhang Gu, etc.

comments: ECCV2020

cite: [2003.13683] DHP: Differentiable Meta Pruning via HyperNetworks (arxiv.org)

code: ofsoundof/dhp: This is the official implementation of "DHP: Differentiable Meta Pruning via HyperNetworks". (github.com) (official)



0、速覽

研究現狀和存在問題

目前 AutoML 和 neural architecture search(NAS) 在剪枝領域的應用異常火爆,但是作者指出目前的自動剪枝要麼依賴於強化學習(reinforcement learning)要麼依賴於進化演算法(evolutionary algorithm)。這些演算法不具有可微性,所以需要較長的搜尋階段才能收斂

本文為了解決這個問題,提出了新的基於超網路的可微剪枝方法,用於自動進行網路剪枝。

具體做法

設計一個額外的超網路(hypernetwork),該超網路在初始化之後,訓練後的輸出作為骨幹網(backbone network)的權值引數

而骨幹網路的輸出(文中稱為潛在向量,latent vector)在 一範數正則化近端梯度求解後,得到稀疏潛在向量超網路將前後兩層 \(z^{l-1}、z^{l}\) 的稀疏潛在向量作為超網路的輸入,訓練之後的輸出作為後一層 \(z^{l}\) 新的權值引數。相較於上次權值引數,相應的切片被去除,達到了網路剪枝的效果。

可微性來自為剪枝定製的超網路和近端梯度



1、介紹

With the advent of AutoML and neural architecture search (NAS) [63,8], a new trend of network pruning emerges, i.e. pruning with automatic algorithms and targeting distinguishing sub-architectures. Among them, reinforcement learning and evolutionary algorithm become the natural choice [19,40]. The core idea is to search a certain fine-grained layer-wise distinguishing configuration among the all of the possible choices (population in the terminology of evolutionary algorithm).After the searching stage, the candidate that optimizes the network prediction accuracy under constrained budgets is chosen.

隨著AutoML和neural architecture search (NAS)的出現[63,8],出現了一個新的網路剪枝趨勢,即使用自動演算法並針對不同的子架構進行剪枝。其中,強化學習和進化演算法成為常用的選擇[19,40]。其核心思想是在所有可能的選擇(進化演算法術語中的種群)中搜尋特定的細粒度分層區別配置。搜尋階段結束後,在預算受限的情況下,選擇最優網路預測精度的候選演算法。

However, the main concern of these algorithms is the convergence property.

這些演算法關注點在於收斂性。強化學習存在著收斂困難的問題;進化演算法無法訓練整個種群直至收斂。

A promising solution to this problem is endowing the searching mechanism with differentiability or resorting to an approximately differentiable algorithm. This is due to the fact that differentiability has the potential to make the searching stage efficient。

一個有希望的解決方案就是賦予搜尋機制可微性或近似可微,這會使得搜尋階段變得高效。

Actually, differentiability has facilitated a couple of machine learning approaches and the typical one among them is NAS. ... Differentiable architecture search (DARTS) reduces the insatiable consumption to tens of GPU hours ...

NAS 就是具有可微性的一種方案,但是早期過於耗時。後來的可微結構搜尋(Differentiable architecture search)大大減小了GPU消耗時間。

Another noteworthy direction for automatic pruning is brought by MetaPruning [40] which introduces hypernetworks [13] into network compression .... ...

另一種解決思路是 MetaPruning,它將超網路引入到網路壓縮中。超網路的輸出被作為骨幹網路的的引數;在訓練過程中,梯度會被反向傳播到超網路。這種方法屬於meta learning 因為超網路中的引數是骨幹網路引數的後設資料(meta-data)。本文的方法屬於MetaPruning。



... ...Each layer is endowed with a latent vector that controls the output channels of this layer. Since the layers in the network are connected, the latent vector also controls the input channel of the next layer. The hypernetwork takes as input the latent vectors of the current layer and previous layer that controls the output and input channels of the current layer respectively.

本文方法:骨幹網路每一層被賦予一個潛在向量來控制這一層的輸出通道。由於骨幹網路中的每層是相互連結的,潛在向量也控制著下一層的輸入通道。超網路以當前層和上一層的潛在向量作為輸入,分別控制著當前層的輸出通道和輸入通道,輸出結果被作為骨幹網路當前層的新引數。

To achieve the effect of automatic pruning, ‘1sparsity regularizer is applied to the latent vectors. A pruned model is discovered by updating the latent vectors with proximal gradient. The searching stage stops when the compression ratio drops to the target level.

為了達到自動修剪的效果,對潛在向量應用了 \(l_1\) 稀疏正則化;利用近端梯度更新潛在向量,得到一個剪枝模型。當壓縮比下降到目標水平後,搜尋階段停止,此時潛在向量已經被稀疏化了。



2、Methodology


2.1 超網路設計 Hypernetwork design

符號:標準(x)-> 標量;小寫(z)-> 向量;大寫(Z)-> 矩陣/高緯張量

超網路設計如上

The hypernetwork consists of three layers. The latent layer takes as input the latent vectors and computes a latent matrix from them. The embedding layer projects the elements of the latent matrix to an embedding space. The last explicit layer converts the embedded vectors to the final output.

超網路一共有三層。第一層是潛在層(latent layers),負責將輸入的潛在向量計算成潛在矩陣;第二層是嵌入層(embedding layer),負責將潛在矩陣的元素投射到嵌入空間(embedding space);第三層是顯示層,負責將嵌入向量轉化成最終輸出

對於一個cnn,假設第 \(l\) 層的權值引數的維度為 \(n * c * w * h\),這裡的 \(n, c, w*h\) 分別表示輸出通道,輸入通道,卷積核尺寸。現在該層被賦予一個潛在向量 \(z^{l}\),潛在向量的大小和該層的輸出通道相同,同理上層 \(z^{l-1}\)。超引數網路以 \(z^l, z^{l-1}\) 為輸入。第一層潛在向量的計算如下:

\[\mathbf { Z } ^ { l } = \mathbf { z } ^ { l } \cdot \mathbf { z } ^ { l - 1 ^ { T } } + \mathbf { B } _ { 0 } ^ { l } \]


這裡,\(\mathbf { Z } ^ { l } , \mathbf { B } _ { 0 } \in \mathbb { R } ^ { n \times c }\)。第二層公式:

\[\mathbf { E } _ { i , j } ^ { l } = \mathbf { Z } _ { i , j } ^ { l } \mathbf { w } _ { 1 } ^ { l } + \mathbf { b } _ { 1 } ^ { l } , i = 1 , \cdots , n , j = 1 , \cdots , c \]


這裡,\(\mathbf { E } _ { i , j } ^ { l } , \mathbf { w } _ { 1 } ^ { l } , \mathbf { b } _ { 1 } ^ { l } \in \mathbb { R } ^ { m }\)\(\mathbf { E } _ { i , j } ^ { l } , \mathbf { w } _ { 1 } ^ { l } , \mathbf { b } _ { 1 } ^ { l }\) 聚合成3D張量,命名為 \(\mathbf { W } _ { 1 } ^ { l } , \mathbf { B } _ { 1 } ^ { l } , \mathbf { E } ^ { l } \in \mathbb { R } ^ { n \times c \times m }\)。第三次公式:

\[\mathbf { O } _ { i , j } ^ { l } = \mathbf { w } _ { 2 } ^ { l } \cdot \mathbf { E } _ { i , j } ^ { l } + \mathbf { b } _ { 2 } ^ { l } , i = 1 , \cdots , n , j = 1 , \cdots , c \]


這裡,\(\mathbf { O } _ { i , j } ^ { l } , \mathbf { b } _ { 2 } ^ { l } \in \mathbb { R } ^ { w h } , \mathbf { w } _ { 2 } ^ { l } \in \mathbb { R } ^ { w h \times m }\)\(\mathbf { w } _ { 2 } ^ { l } , \mathbf { b } _ { 2 } ^ { l } , \text { and } \mathbf { O } _ { i , j } ^ { l }\)聚合成高維張量,\(\mathbf { W } _ { 2 } ^ { l } \in \mathbb { R } ^ { n \times c \times w h \times m }\)\(\mathbf { B } _ { 2 } ^ { l } , \mathbf { O } ^ { l } \in\mathbb { R } ^ { n \times c \times w h }\)。抽象整合後:

\[\mathbf { O } ^ { l } = h \left( \mathbf { z } ^ { l } , \mathbf { z } ^ { l - 1 } ; \mathbf { W } ^ { l } , \mathbf { B } ^ { l } \right) \]


此時,超網路的輸出 \(O^{l}\) 被用作骨幹網路第 \(l\) 層的權值引數。

2.2 稀疏正則化和近端梯度 Sparsity regularization and proximal gradient

The core of differentiability comes with not only the specifically designed hypernetwork but also the mechanism used to search the the potential candidate. To achieve that, we enforce sparsity constraints to the latent vectors.

可微性的核心不僅存在於特別設計的超網路,而且在於用於搜尋潛在候選人的機制。為了實現這一點,我們對潛在向量實施稀疏性約束

假設上文提到的CNN的損失函式如下:

\[\min _ { \mathbf { W } , \mathbf { B } , \mathbf { z } } \mathcal { L } ( \mathbf { y } , f ( \mathbf { x } ; h ( \mathbf { z } ; \mathbf { W } , \mathbf { B } ) ) ) + \gamma \mathcal { D } ( \mathbf { W } ) + \gamma \mathcal { D } ( \mathbf { B } ) + \lambda \mathcal { R } ( \mathbf { z } ) \]


這裡 \(\mathcal { L } ( \cdot , \cdot ) , \mathcal { D } ( \cdot ) , \text { and } \mathcal { R } ( \cdot )\) 分別是視覺任務的損失函式,權值衰減項損失函式,和稀疏正則化損失函式。\(\gamma, \lambda\) 是正則化因子。假設稀疏正則化採用的 \(l_1\) 正則化:\(\mathcal { R } ( \mathbf { z } ) = \sum _ { l = 1 } ^ { L } \left\| \mathbf { z } ^ { l } \right\| _ { 1 }\)

note: \(l_1\) 正則化可以使得引數稀疏但是往往會帶來不可微的問題


為了解決損失函式,超網路中的權值 \(W\) 和偏置 \(B\) 會通過SGD更新。SGD中的梯度來自骨幹網路的反向傳播。而不可微部分則使用近端梯度方法進行更新,公式如下:

\[\mathbf { z } [ k + 1 ] = \operatorname { prox } _ { \lambda \mu \mathcal { R } } ( \mathbf { z } [ k ] - \lambda \mu \nabla \mathcal { L } ( \mathbf { z } [ k ] ) ) \]


這裡的 \(\mu\) 是近端梯度的步長,同時也將它設作為 SGD 的學習率。當稀疏正則化採用的是 \(l_1\),此時這個近端梯度有解析解:

\[\mathbf { z } [ k + 1 ] = \operatorname { sgn } ( \mathbf { z } [ k + \Delta ] ) [ | \mathbf { z } [ k + \Delta ] | - \lambda \mu ] _ { + } \]


note:這裡其實就是軟閾值法計算。


因此,潛在向量會先得到 \(W, B\) 的 SGD 更新,然後近端運算元更新。由於SGD更新和近端運算元都有封閉解,所以可以認為整個解是近似可微的,這保證了演算法與強化學習、進化演算法相比是快速收斂。

2.3 網路剪枝 Network pruning

這塊再看看再寫

2.4 潛在向量共享 Latent vector sharing

... ... the residual blocks are interconnected with each other in the way that their input and output dimensions are related. Therefore, the skip connections are notoriously tricky to deal with.

由於 ResNet、MobileNetV2、SRResNet、EDSR 等殘差網路存在 skip 連線,殘差塊之間以輸入輸出維度相關的方式相互連線。因此,處理跳躍連線十分棘手。

But back to the design of the proposed hypernetwork, a quite simple and straightforward solution to this problem is to let the hypernetworks of the correlated layers share the same latent vector. Note that the weight and bias parameters of the hypernetworks are not shared.

但是在超網路的設計中,這個問題的一個相當簡單和直接的解決方案是讓相關層的超網路共享相同的潛在向量。這裡超網路的權值和偏差引數是不共享的。

Thus, sharing latent vectors does not force the the correlated layers to be identical. By automatically pruning the single latent vector, all of the relevant layers are pruned together.

因此,共享潛在向量並不強迫相關層是相同的。通過自動修剪單個潛在向量,所有相關的層被一起修剪。

2.5 收斂性的討論 Discussion on the convergence property

討論中作者指出幾點:

Compared with reinforcement learning and evolutionary algorithm, proximal gradient may not be the optimal solution for some problems.

  1. 和強化學習和進化演算法相比,近端梯度可能不是某些問題的最優解

But as found by previous works [41,40], automatic network pruning serves as an implicit searching method for the channel configuration of a network.

  1. 根據他人研究,自動網路剪枝是一種關於網路通道配置的隱式搜尋方式

The important factor is the number of remaining channels of the convolutional layers in the network. Thus, it is relatively not important which filter is pruned as long as the number of pruned channels are the same.

  1. 重要的是網路中卷積層剩餘通道的數量,而不是哪個具體過濾器

note:這個點我有些質疑,但作者沒有給出進一步解釋且我也沒有證據反駁。先記著


2.6 Implementation consideration

超網路的緊湊表示

將超網路中第二步公式 \(\mathbf { E } _ { i , j } ^ { l } = \mathbf { Z } _ { i , j } ^ { l } \mathbf { w } _ { 1 } ^ { l } + \mathbf { b } _ { 1 } ^ { l } , i = 1 , \cdots , n , j = 1 , \cdots , c\) 寫成如下:

\[\mathbf { E } ^ { l } = \mathcal { U } ^ { 3 } \left( \mathbf { Z } ^ { l } \right) \circ \mathbf { W } _ { 1 } ^ { l } + \mathbf { B } _ { 1 } ^ { l } \]


這裡 \(\mathcal { U } ^ { 3 } \left( \mathbf { Z } ^ { l } \right) \in \mathbb { R } ^ { n \times c \times 1 }\)\(\mathcal { U } ^ { 3 } ( \cdot )\) 使得二維 \(Z^l\) 變成三維。超網路的第三步公式\(\mathbf { O } _ { i , j } ^ { l } = \mathbf { w } _ { 2 } ^ { l } \cdot \mathbf { E } _ { i , j } ^ { l } + \mathbf { b } _ { 2 } ^ { l } , i = 1 , \cdots , n , j = 1 , \cdots , c\) 寫成如下:

\[\mathbf { O } ^ { l } = \mathbf { W } _ { 2 } ^ { l } * \mathbf { E } ^ { l } + \mathbf { B } _ { 2 } ^ { l } \]


剪枝分析
分析超網路的三層以及隱藏的潛在向量,可以直接瞭解骨幹層是如何自動剪枝的。公式如下:

\[\begin{aligned} \overline { \mathbf { O } } ^ { l } & = \mathbf { W } _ { 2 } ^ { l } * \left[ \mathcal { U } ^ { 3 } \left( \left( \mathbf { m } ^ { l } \circ \overline { \mathbf { z } } ^ { l } \right) \cdot \left( \mathbf { m } ^ { l - 1 } \circ \overline { \mathbf { z } } ^ { l - 1 } \right) ^ { T } \right) \circ \mathbf { W } _ { 1 } ^ { l } \right] \\ & = \mathbf { W } _ { 2 } ^ { l } * \left[ \mathcal { U } ^ { 3 } \left( \mathbf { m } ^ { l } \cdot \mathbf { m } ^ { l - 1 ^ { T } } \right) \circ \mathcal { U } ^ { 3 } \left( \overline { \mathbf { z } } ^ { l } \cdot \overline { \mathbf { z } } ^ { l - 1 ^ { T } } \right) \circ \mathbf { W } _ { 1 } ^ { l } \right]\\ & = \mathcal { U } ^ { 3 } \left( \mathbf { m } ^ { l } \cdot \mathbf { m } ^ { l - 1 ^ { T } } \right) \circ \left[ \mathbf { W } _ { 2 } ^ { l } * \left( \mathcal { U } ^ { 3 } \left( \overline { \mathbf { z } } ^ { l } \cdot \overline { \mathbf { z } } ^ { l - 1 ^ { T } } \right) \circ \mathbf { W } _ { 1 } ^ { l } \right) \right]\end{aligned} \]


如上公式所示,在潛在向量上應用掩碼與在最終輸出上應用掩碼具有相同的效果。

3、實驗結果 Experimental Results

省略一個常見的錯誤率/FLOPs壓縮率/引數壓縮率的圖





note:runtime 加速是真的不錯

相關文章