machine learning
neural networks
dimensionality reduction
truncated SVD
fully-connected layer

How to reduce a fully-connected InnerProduct layer using truncated SVD

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Reducing the complexity of neural network models is a crucial task in the field of deep learning, especially when deploying models in environments where computational resources are limited. A common approach to achieving this is by compressing fully-connected layers, also known as "InnerProduct" layers. One effective method of reducing these layers' complexity is through the application of Truncated Singular Value Decomposition (SVD). In this article, we will delve into the mechanism of Truncated SVD in the context of neural networks and illustrate its implementation with technical examples.

Fully-Connected Layers

A fully-connected layer is a fundamental building block in neural networks, where each neuron is connected to every neuron in the previous layer. This results in a dense matrix of weights, which can often be overparameterized. Let us consider a fully-connected layer that transforms an input vector x∈Rnx \in \mathbb{R}^n into an output vector y∈Rmy \in \mathbb{R}^m using a weight matrix W∈Rm×nW \in \mathbb{R}^{m \times n} and a bias vector b∈Rmb \in \mathbb{R}^m:

y=Wx+by = Wx + b

The objective is to approximate this layer efficiently while retaining the layer's functional characteristics.

Singular Value Decomposition (SVD)

SVD is a linear algebra technique that factorizes a matrix WW into three other matrices: UU, Σ\Sigma, and VTV^T, given by:

W=UΣVTW = U \Sigma V^T

Here, $U \in \mathbb\{R\}^\{m \times m\}$ and $V \in \mathbb\{R\}^\{n \times n\}$ are orthogonal matrices, and Σ∈Rm×n\Sigma \in \mathbb{R}^{m \times n} is a diagonal matrix containing the singular values of WW. The singular values indicate the intrinsic dimensionality of the data represented by WW.

Truncated SVD

Truncated SVD reduces the dimensionality of these matrices by keeping only the top kk singular values, where k<min⁡(m,n)k < \min(m, n). The truncated approximation of WW is:

W_k=U_kΣ_kV_kTW\_k = U\_k \Sigma\_k V\_k^T

Here, Uk∈Rm×kU_k \in \mathbb{R}^{m \times k}, Σk∈Rk×k\Sigma_k \in \mathbb{R}^{k \times k}, and Vk∈Rn×kV_k \in \mathbb{R}^{n \times k}. The choice of kk determines the trade-off between the complexity and the accuracy of the model.

Reducing the Fully-Connected Layer

The original weight matrix WW can now be approximated using Truncated SVD:

  1. Compute SVD: Decompose W=UΣVTW = U \Sigma V^T.
  2. Truncate the Matrices: Select top kk singular values and corresponding vectors UkU_k, Σk\Sigma_k, and VkV_k.
  3. Approximate WkW_k: Form the reduced matrix Wk=UkΣkVkTW_k = U_k \Sigma_k V_k^T.

This reduced representation can now be interpreted as a chain of two smaller fully-connected layers:

• Transform the input xx using VkTV_k^T, then through a diagonal layer with Σk\Sigma_k. • Finally, transform the result using UkU_k.

These transformations reduce the computational complexity while maintaining most of the significant information.

Practical Example

Let's assume you have a fully-connected layer with the following weight matrix:

W=(322 23−2 −223 )W = \begin{pmatrix} 3 & 2 & 2 \ 2 & 3 & -2 \ -2 & 2 & 3 \ \end{pmatrix}

  1. Compute the SVD to obtain UU, Σ\Sigma, and VTV^T.
  2. Select k=2k=2 top singular values σ1,σ2\sigma_1, \sigma_2 and corresponding vectors to form UkU_k, Σk\Sigma_k, and VkV_k.
  3. Construct WkW_k: The reduced matrix represents the layer with significantly fewer parameters while preserving key features.

Key Trade-Offs

FeatureOriginal LayerReduced Layer Using SVD
Parameter Countm×nm \times nk(m+n+1)k(m + n + 1)
Computational ComplexityO(m⋅n)O(m \cdot n)O(k⋅(m+n))O(k \cdot (m + n))
Preservation of InformationFull rankDepends on kk
ApplicabilityGeneral purposeEffective for redundancy

Conclusion

By opting for Truncated SVD, we gain a balance between reducing network complexity and maintaining model performance—a critical advantage when deploying neural networks in resource-constrained settings. It's an invaluable tool when optimizing deep learning models for efficiency while ensuring they remain viable for their intended applications.

Truncated SVD offers both simplicity and power by seamlessly integrating mathematical rigor into practical deep learning solutions. By understanding and correctly applying this technique, we can significantly enhance the performance and deployability of neural network models.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design