Compressing deep quaternion neural networks with targeted regularization
Riccardo Vecchi Simone Scardapane Danilo Comminiello Aurelio Uncini
cs.LG stat.ML







Compressing deep quaternion neural networks with targeted regularization
    Riccardo Vecchi, Simone Scardapane, 
 Danilo Comminiello, and Simone ScardapaneAuthors are with the Department of Information Engineering, Electronics, and Telecommunications, Sapienza University of Rome, Via Eudossiana 18, 00184, Rome, Italy.
E-mail: {firstname.lastname}@uniroma1.it.
    December 30, 2023
=================================================================================================================================================================================================================================================================================================

titlepage


In recent years, hyper-complex deep networks (such as complex-valued and quaternion-valued neural networks) have received a renewed interest in the literature. They find applications in multiple fields, ranging from image reconstruction to 3D audio processing. Similar to their real-valued counterparts, quaternion neural networks (QVNNs) require custom regularization strategies to avoid overfitting. In addition, for many real-world applications and embedded implementations, there is the need of designing sufficiently compact networks, with few weights and neurons. However, the problem of regularizing and/or sparsifying QVNNs has not been properly addressed in the literature as of now. In this paper, we show how to address both problems by designing targeted regularization strategies, which are able to minimize the number of connections and neurons of the network during training. To this end, we investigate two extensions of ℓ_1 and structured regularization to the quaternion domain. In our experimental evaluation, we show that these tailored strategies significantly outperform classical (real-valued) regularization approaches, resulting in small networks especially suitable for low-power and real-time applications.




§ INTRODUCTION


Deep neural networks have achieved remarkable results in a variety of tasks and applicative scenarios over the last years <cit.>. Several breakthroughs have been obtained by designing custom neural modules for exploiting structure in data, such as the spatial organization of pixels in an image, temporal or sequential information, and so on. The field of quaternion deep learning aims at extending these results to problems for which a hyper-complex representation (as opposed to a real-valued representation) is more adequate <cit.> (see also <cit.> for earlier works on the field). Among these approaches, the most common is the use of quaternion values (a straightforward extension of the complex algebra) for representing input values, weights, and output values of the network. The resulting quaternion-valued neural networks (QVNNs) have been successfully applied to, among others, image classification <cit.>, image coloring and forensics <cit.>, natural language processing <cit.>, graph embeddings <cit.>, human motion recognition <cit.>, and 3D audio processing <cit.>. By exploiting the properties of the quaternion algebra, QVNNs can achieve similar or higher accuracy than their real-valued counterparts, while requiring fewer parameters and computations. In fact, outside of the deep learning field, the use of quaternion representations is well established in, e.g., the robotics community for linear filtering processing <cit.>.

empty

The majority of the literature on QVNNs up to this point has focused on extending standard deep learning operations, such as convolution <cit.>, batch normalization <cit.>, or weight initialization <cit.>, to the quaternion domain. Less attention, however, has been devoted to properly extending other aspects of the training process, including accelerated optimization algorithms <cit.> and regularization strategies. In particular, in many real-world scenarios (e.g., embedded devices) users need to take into careful consideration of complexity and computational costs, by making the networks as small as possible while maintaining a good degree of accuracy <cit.>.

In the real-valued case, these constraints have been analysed in detail, and several strategies have been developed. Most commonly, compression of neural networks can be achieved while training by applying several regularization strategies, such as ℓ_2, ℓ_1, or group sparse norms <cit.>, which can target either single weights or entire neurons. A direct extension of these strategies to the case of QVNN, as done in the current literature, applies them independently on the four components of each quaternion weight. However, in this paper, we argue and show experimentally later on that this trivial extension results in highly sub-optimal regularization procedures, which do not sufficiently exploit the properties of the quaternion algebra. In fact, the problem of sparsifying a quaternion is not restricted to neural networks, but it has received attention from other disciplines, most notably quaternion extensions of matching pursuit <cit.>, and compressive sensing <cit.>. To the best of our knowledge, however, almost no work has been devoted to the specific problem of targeting quaternion-valued sparsity in QVNNs, possibly hindering their application in certain applicative scenarios.



 §.§ Contributions of the paper


In this paper, we leverage on prior works on compression of real-valued networks and quaternion-valued norms to propose two targeted regularization strategies for QVNNs, filling an important gap in the literature.


	
  * The first regularization approach that we propose (Section <ref>) extends the use of ℓ_1 regularization to consider a single quaternion weight as a unitary component, and it is akin to a structured form of regularization in the real-valued case. It allows to remove entire quaternion weights simultaneously, instead of each of its four components independently.
	
  * The second strategy that we propose is instead defined at the level of a single quaternion neuron, extending ideas from <cit.> to a quaternion domain, thus allowing to remove entire units from the network at once. We consider real-valued regularization to be applied on quaternion extensions of batch normalization (BN), in which every neuron is scaled by a single real-valued coefficient and is eventually removed by the optimization process. Thus, we achieve a better stability (thanks to the use of BN) and sparsity at the same time.
 

In our experimental evaluation, we show that these two proposed strategies significantly outperform the naive application of classical regularization strategies on two standard image recognition benchmarks. The resulting QVNNs are thus smaller (both in terms of neurons and weights) and require a markedly smaller computational footprint when run in inference mode, with up to 5x reductions in the number of connections and 3x speedups in the inference time.



 §.§ Organization of the paper

Section <ref> recalls quaternion-algebra and QVNNs. Section <ref> describes our two proposed regularization strategies. We provide an experimental evaluation in Section <ref>, concluding in Section <ref>.



 §.§ Notation


In the rest of the paper, the use of a subscript {i,j,k} always refers to the respective imaginary component of the corresponding quaternion number, as explained in Section <ref>. x^* is the conjugate of x. We use bold letters, e.g., 𝐱, for vectors, and uppercase bold letters, e.g., 𝐗, for matrices. For indexing, we use a superscript l to refer to the lth layer in a network, while we use bracket notation to refer to an element inside a set (e.g., 𝐱(n) is the nth input element of the training set).



§ PRELIMINARIES





 §.§ Quaternion algebra



A quaternion-valued number x ∈ℍ can be represented by a tuple of four real-valued numbers (x_r, x_i, x_j, x_k) ∈ℝ^4 as <cit.>:


    x = x_r + ix_i + jx_j + kx_k  ,


where the three imaginary units i, j, k satisfy the fundamental axiom of quaternion algebra  i^2 = j^2 = k^2 = ijk = -1. Given two quaternions x and y, we can define their sum as:


    z = (x_r + y_r) + i (x_i + y_i) + j(x_j + y_j) + k(x_k + y_k)  ,


and similarly for multiplication by a real number. More importantly, the (Hamilton) product between the two quaternions is given by:


    x ⊗ y  =    ( x_ry_r - x_iy_i - x_jy_j - x_ky_k ) + 
        i ( x_ry_i + x_iy_r + x_j y_k + x_ky_j ) + 
        j ( x_ry_j - x_iy_k + x_jy_r + x_ky_i ) + 
        k ( x_ry_k + x_iy_j - x_jy_i + x_ky_r )  .


Note that the product is not commutative, setting apart quaternion algebra from its complex- and real-valued restrictions.



 §.§ Quaternion-valued neural networks


QVNN are flexible models for transforming quaternion-valued vector inputs 𝐱∈ℍ^d to a desired target value 𝐲, which in the majority of cases are real-valued (e.g., a probability distribution over a certain number of classes). A standard, fully-connected layer of a QVNN is given by:


    g(𝐡^l+1) = σ( 𝐖⊗𝐡^l + 𝐛)  ,


where 𝐡^l is the input to the layer, 𝐖 is a quaternion-valued matrix of adaptable coefficients with components (𝐖_r, 𝐖_i, 𝐖_j, 𝐖_k) (and similarly for 𝐛), ⊗ performs matrix-vector multiplication according to the Hamilton product in (<ref>), and σ(·) is a proper element-wise non-linearity. Similarly to the complex-valued case <cit.>, choosing an activation function is more challenging than for real-valued NNs, and most works adopt a split-wise approach where a real-valued function σ_r is applied component-wise:


    σ(s) = σ_r(s_r) + i σ_r(s_i) + j σ_r(s_j) + k σ_r(s_k)  ,


where s is a generic activation value. Customarily, the input to the first layer is set to 𝐡^1 = 𝐱, while the output of the last layer is the desired target 𝐡^L = 𝐲. If the target is real-valued, one can transform 𝐡^L to a real-valued vector by taking the absolute value element-wise, and eventually apply one or more real-valued layers afterwards. In addition, (<ref>) can be easily extended to consider convolutive layers <cit.> and recurrent formulations <cit.>.



 §.§ Optimization of QVNNs


Now consider a generic QVNN f(𝐱) obtained by composing an arbitrary number of layers in the form of (<ref>) or its extensions. We receive a dataset of N examples {𝐱(n), 𝐲(n)}_n=1^N, and we train the network by optimizing:


    J(θ) = 1/N∑_n=1^N l( 𝐲(n), f(𝐱(n)) ) + λ· r(θ)  ,


where θ is the set of all (quaternion-valued) parameters of the network, l is a loss function (e.g., mean-squared error, cross-entropy loss), and r is a regularization function weighted by a scalar λ≥ 0. Because the loss function in (<ref>) is non-analytic, one has to resort to the generalized QR-calculus to define proper gradients for optimization <cit.>. Luckily, these gradients coincide with the partial derivatives of (<ref>) with respect to all the real-valued components of the quaternions, apart from a scale factor. For this reason, it is possible to optimize (<ref>) using standard tools from stochastic optimization popular in the deep learning literature, such as Adam or momentum-based optimizers.

While most components described up to now have received considerable attention in the literature, the design of a correct regularization term r(·) in (<ref>) has been mostly ignored, and it is the focus of the next section.



§ TARGETED REGULARIZATION FOR QVNNS



In the real-valued case, a classical choice for the regularizer r(·) in (<ref>) is the ℓ_2 norm. Whenever sparsity is desired, it can be replaced with the ℓ_1 norm, or a proper group version acting at a neuron level <cit.>. In most implementations of QVNNs, these regularizers are applied element-wise on the four components of each quaternion weight. For example, ℓ_1 regularization in this form can be written as:


    r(θ) = ∑_w ∈θ( | w_r | + | w_i | +| w_j | + | w_k |)  .


We argue that, because of the decoupling across the four components, this operation results in far less regularization and sparsity than one could expect. This is inconvenient both from the generalization point of view, and from an implementation perspective, where smaller, more compact networks are desired. In this section, we present two targeted regularization strategies, acting on each quaternion as a unitary component, resulting in a more principled form of regularization for QVNNs.



 §.§ ℓ_1 regularization for quaternion weights



Given any weight w ∈ℍ of the QVNN (i.e., a single element of θ from (<ref>)), the first method we explore is to regularize its norm as:


    r(w) = 1/Q√(w_r^2 + w_i^2 + w_j^2 + w_k^2) = 1/Q√(w^* ⊗ w) ,


where Q is the number of weights in the network (allowing to split the influence of (<ref>) on each weight with respect to the loss function (<ref>)), and w^* is the conjugate of w, i.e., w^* = w_r - iw_i - jw_j - kw_k (combining the definition of a conjugate with (<ref>), and removing all terms except the first line, shows the second equality in (<ref>)). 

This method can be seen as the natural extension of ℓ_1 norm minimization on a quaternionic signal <cit.>. It is also equivalent to a structured form of sparsity  <cit.>, where we group all the components of the quaternion w together. As a result, minimizing (<ref>) will tend to bring the entire quaternion weight to 0, instead of each component independently (similarly to how structured sparsity in a real-valued network brings all the incoming or outgoing weights of the network towards zero together <cit.>).



 §.§ Sparse regularization with quaternion batch normalization



The method described in Section <ref> is effective for removing single quaternion weights, but in many real-world scenarios we also require a principled way to remove entire neurons during the training process <cit.>. To this end, we investigate a hyper-complex extension of the technique originally proposed in <cit.>. The basic idea is to compose each layer in (<ref>) with a batch normalization (BN) layer <cit.>, and then perform sparse regularization on the parameters of the BN layer, indirectly removing the original neurons in the network. We briefly recall that BN, originally introduced in <cit.>, allows each neuron to adapt the mean and variance of its activation values, with the general effect of stabilizing training and possibly simplifying optimization.

For implementing the BN model in the quaternion domain, we build on <cit.> after we consider a single output in (<ref>), i.e., the quaternion-valued output of a single neuron in the network. During training, we observe a mini-batch of B inputs 𝐱(1), …, 𝐱(B) (a subset of the full dataset) and corresponding outputs of the neuron h(1), …, h(B) (we do not use an index for the neuron for notational simplicity). We can compute the mean and variance of the mini-batch as:


    μ̂    = 1/B∑_n=1^B [ h_r(n) + i h_i(n) + j h_j(n) + k h_k(n) ]  , 
    σ̂^2     = 1/B∑_n=1^B ( h(n) - μ̂) ⊗( h(n) - μ̂)^*  .


These values are computed dinamically during training, while they are set to a fixed (pre-computed) value during inference. The output of the BN layer is defined as <cit.>:


    h_BN(n) = ( h(n) - μ̂/√(σ̂^2 + ε))γ + β ,


where ε is a small value added to ensure stability, while γ∈ℝ and β∈ℍ are trainable parameters initialized at 1 and 0 respectively. Key for our proposal, the γ parameter in (<ref>) is real-valued, allowing us to apply standard real-valued regularization. In particular, similarl to <cit.>, we apply (real-valued) ℓ_1 regularization on the γs, since pushing a single γ to zero effectively allows us to remove the entire neuron in the QVNN. Thus, denoting with Γ the set of all γ parameters in the network, we regularize them as:


    r(Γ) = 1/|Γ|∑_γ∈Γ|γ| ,


where |Γ| is the cardinality of the set Γ.



 §.§ Mixed regularization strategies


The strategies described in the previous sections are not exclusive, and we can explore several mixed strategies with different regularization weights. In our experimental section, we consider combining the two strategies, as long as one of the two strategies are combined with a classical ℓ_1 regularization to be applied independently on each component.



§ EXPERIMENTAL RESULTS





 §.§ Experimental setup


We evaluate our proposal on two quaternion-valued image recognition benchmarks taken from <cit.>. Firstly, we use the standard MNIST dataset by converting every image pixel to a quaternion  with 0 imaginary components, i.e., we encode one grey-valued pixel g as x = g + i0 + j0 + k0. Secondly, we consider the more challenging CIFAR-10 dataset by converting its RGB representation to the three imaginary components of a pure quaternion with 0 real part, i.e., we encode a single pixel with RGB values (r,g,b) to x = 0 + ir + jg + kb.

Similar to previous literature, for MNIST, we use a quaternion convolutional network with two convolutive layers having 16 and 32 filters respectively, interleaved by (quaternion-valued) max-pooling operations. After the second convolutive layer we apply a dropout operation for regularization and a final quaternion fully connected layer for obtaining the class probabilities. For CIFAR-10, we increase this to five convolutive layers having respectively 32, 64, 128, 256, and 512 filters. In this case, we also apply droput every two convolutive layers. Overall, the MNIST network has ≈ 10k parameters, while the CIFAR-10 network has ≈ 500k parameters.

All networks use ReLU applied component-wise as in (<ref>). After the last layer, we take the absolute values of each output to obtain a real-valued score (equivalent to the classical logit value in a real-valued network), and we apply a softmax activation function to convert these to probabilities. The networks are trained to minimize the average cost with a cross-entropy loss function using the Adam optimization algorithm.

All experiments are implemented in the PyTorch framework extending the QVNN library from <cit.>.[<https://github.com/Orkis-Research/Pytorch-Quaternion-Neural-Networks>] For replicability, we release our demo files on a separate repository online.[<https://github.com/Riccardo-Vecchi/Pytorch-Quaternion-Neural-Networks>] All hyper-parameters are fine-tuned independently for each network and dataset using the corresponding validation data. Importantly, this means that all regularization coefficients are optimized separately for every method.
 
 

 §.§ Results for the quaternion-level sparsity







We start by evaluating the quaternion-level regularization strategy described in Section <ref>, denoted as R_Q in the experiments. We compare classical ℓ_2 and ℓ_1 regularizations, which are applied independently on every component. In addition, we evaluate a mixed regularization strategy combining our proposed R_Q method with an additional ℓ_1 regularization on the components, denoted as R_QL, which is similar to the sparse group sparse technique in <cit.>. For this case, we consider a single, shared regularization factor to be optimized to make comparisons fair.

Results, averaged over 5 different repetitions of the experiments, are presented in Tab. <ref>. We see that applying a regularization has only a marginal effect on accuracy in the MNIST test accuracy, while it improves the accuracy in the more challenging CIFAR-10 case, possibly counter-acting any overfitting effect. In terms of sparsification effects, we show both the component sparsity (i.e., ratio of zero-valued quaternion components) and quaternion sparsity (i.e., ratio of quaternions where all components have been set to 0). We can see that the proposed R_Q strategy results in significantly sparser architectures in both cases, with corresponding gains when considering computational power and inference speed. The mixed strategy R_QL performs very well on MNIST and poorer on CIFAR-10, possibly because we are using only a single shared regularization factor. For a clearer visualization, in Fig. <ref> we show the corresponding sparsity levels during training (for the first 20 epochs of training).

In addition, in Tab. <ref> we report the average training time on different experiments. As expected, adding a regularization term adds only a small overhead in terms of computational time, while we see no statistical difference between  different approaches, further validating the use of a targeted strategy.



 §.§ Results for the neuron-level sparsity






Next, we evaluate the inclusion of the neuron-level sparsity strategy described in Section <ref>. We consider the R_Q strategy from the previous section, and compare a network where we add BN layers after every convolutive layer, penalizing the γ coefficients with an ℓ_1 strategy. For fairness, we also compare two additional baselines where we add the BN layers, but regularize only with the R_Q or R_QL strategies. For space constraints, we only consider the CIFAR-10 dataset, which was the most challenging in the previous section.

The averaged results are presented in Fig. <ref>. We see that, when considering only quaternion-sparsity, the proposed neuron-level strategy (denoted as L_1(BN)) is marginally superior to the proposed R_QL applied on the network having BN layers. However, when evaluating the level of structure in this sparsity, we see that the proposed neuron-level technique in Fig. <ref> vastly outperforms all other strategies, leading to a network having less than 17% of neurons than the original one, as long as having less than 15k remaining parameters. As a result, the final network has a memory footprint of only 1/5 of the original one, with an inference time speedup of approximately 3x. From Fig. <ref>, we also see that this is achieved with no loss in terms of test accuracy of the final networks and, similarly to Tab. <ref>, with no significant increase in computational training time.



§ CONCLUSIONS



The field of quaternion neural networks explores the extensions of deep learning to handle quaternion-valued data processing. This has shown to be especially promising in the image domain and similar fields with highly structured data that lend itself to representation in a hyper-complex domain. While several models and training algorithms have been already extended to this new challenging domain, less attention has been provided to the tasks of regularizing and compressing the networks, which is essential in time-critical and embedded applications. 

In this paper, we proposed two regularization techniques that are specific to quaternion-valued networks. In the first case, we apply some results from quaternion compressive sensing, regularizing each quaternion weight with an ℓ_1-level norm. In the second case, we consider the problem of removing entire neurons from the network, by regularizing appropriately inserted batch normalization layers. Our experimental results on two image classification benchmarks show that these two techniques vastly outperform standard regularization methods when they are applied to quaternion networks, allowing to obtain networks that are extremely smaller (and cheaper to implement) with no loss in testing accuracy at inference time and in computational cost at training time.

For future work, we plan on extending these results to other applications of quaternion-based deep networks (e.g., text processing), as long as evaluating the benefits of sparsifying the networks on custom hardware.

ieeetr




