Soft Merging: A Flexible and Robust Soft Model Merging Approach for
  Enhanced Neural Network Performance
Stochastic Gradient Descent (SGD), a widely used optimization algorithm in
deep learning, is often limited to converging to local optima due to the
non-convex nature of the problem. Leveraging these local optima to improve
model performance remains a challenging task. Given the inherent complexity of
neural networks, the simple arithmetic averaging of the obtained local optima
models in undesirable results. This paper proposes a {\em soft merging} method
that facilitates rapid merging of multiple models, simplifies the merging of
specific parts of neural networks, and enhances robustness against malicious
models with extreme values. This is achieved by learning gate parameters
through a surrogate of the $l_0$ norm using hard concrete distribution without
modifying the model weights of the given local optima models. This merging
process not only enhances the model performance by converging to a better local
optimum, but also minimizes computational costs, offering an efficient and
explicit learning process integrated with stochastic gradient descent. Thorough
experiments underscore the effectiveness and superior performance of the merged
neural networks.
Hao Chen, Yusen Wu, Phuong Nguyen, Chao Liu, Yelena Yesha
Soft Merging: A Flexible and Robust Soft Model Merging Approach for Enhanced Neural Network Performance

###### Abstract

Stochastic Gradient Descent (SGD), a widely used optimization algorithm in deep learning, is often limited to converging to local optima due to the non-convex nature of the problem. Leveraging these local optima to improve model performance remains a challenging task. Given the inherent complexity of neural networks, the simple arithmetic averaging of the obtained local optima models in undesirable results. This paper proposes a _soft merging_ method that facilitates rapid merging of multiple models, simplifies the merging of specific parts of neural networks, and enhances robustness against malicious models with extreme values. This is achieved by learning gate parameters through a surrogate of the \(l_{0}\) norm using hard concrete distribution without modifying the model weights of the given local optima models. This merging process not only enhances the model performance by converging to a better local optimum, but also minimizes computational costs, offering an efficient and explicit learning process integrated with stochastic gradient descent. Thorough experiments underscore the effectiveness and superior performance of the merged neural networks.

Hao Chen\({}^{\dagger}\), Yusen Wu\({}^{*}\), Phuong Nguyen\({}^{*}\), Chao Liu\({}^{\dagger}\), Yelena Yesha\({}^{*}\)\({}^{*}\)Dept. of Computer Science, University of Miami, Miami, FL, USA

\({}^{\dagger}\)Dept. of Computer Science, University of Maryland, Baltimore County, MD, USA Model Merging, Model Optimization

## 1 Introduction

In the recent decade, deep learning has been flourishing in various domains. However, the inherent complexity of neural networks, with their intricate non-linearity and non-convexity, poses formidable challenges. The stochastic gradient descent (SGD) algorithm, despite using identical training data and network architectures, converges to distinct local optima due to different initializations. This leads to a fundamental question: _Can the diverse local optima be leveraged to merge models, enhancing performance and moving closer to a more favorable global optimum?_

Convolutional neural networks exhibit various architectural paradigms like ShuffleNet [1], ResNet [2], UNet [3] and DenseNet [4], each with unique features. Our primary challenge lies in devising an algorithm that accommodates these disparate designs. The secondary challenge involves ensuring the robustness of the merging algorithm across models with vastly varying parameter values. Additionally, we face the third challenge of selectively merging specific components rather than all parameters, aiming for efficiency.

Model merging, as a novel and challenging research direction, has seen limited exploration in existing literature. Unlike model combination and aggregation [5, 6, 7], which fuses different architectures, model merging improves the model performance by integrating the trained ones (local optima) with congruent architectures. Simple techniques like arithmetic averaging fall short due to the intricate nature of neural networks as addressed in the paper [8]. Further, it proposes to merge the models by solving a permutation problem, assuming that local optima exhibit similar performances, they match the neurons of two models. The [9] proposed a general framework for merging the models, with "teacher" and "student" concepts. Primarily the existing methods focus on neuron-level merging, which means they are targeting the weights of the neural network. However, relying solely on this approach has limitations in applicability, flexibility, and robustness, particularly with irregular models.

To address these issues, we introduce a novel paradigm called _soft merging_, known for efficiency, adaptability, and robustness. Our method draws from model merging and channel pruning research [10, 9, 11, 12]. It involves concurrent training of gate parameters for multiple models, using a differentiable surrogate of \(l_{0}\) regularization to identify crucial parts. Instead of updating the weights, it only picks the best ones from the provided set of weights. This enables selective merging across various layers and architectures, with enhanced adaptability. In summary, our contributions include:

* Our proposal outlines a general procedure for selectively soft merging multiple models simultaneously with diverse neural network architectures.
* We present an algorithm that achieves linear complexity for efficient soft merging.
* Extending neural network model merging to accommodate a wide range of deep learning designs ensures robustness, even in the presence of anomalies.
## 2 Proposed Methods

### Problem statement

Suppose there are \(J\) given models denoted as \(\{\mathcal{M}_{j}\}_{j=1}^{J}\) with the same neural network architecture. Given training data \(\mathbf{X}\) and labels \(\mathbf{Y}\), our goal to find the optimal model \(\mathcal{M}^{*}\) from the object function

\[\min_{\{g_{j}\}_{j}}\sum_{j}\mathcal{L}(g_{j}\mathcal{M}_{j}(\mathbf{X}), \mathbf{Y};\mathbf{\theta}_{j}),s.t.\sum_{j}g_{j}=1, \tag{1}\]

where \(g_{j}\in\{0,1\}\) is a gate parameter, \(\mathcal{L}\) is the loss function, and \(\mathbf{\theta}_{j}\) represents the neural networks parameters in the \(j\)-th model. Labels \(\mathbf{Y}\) may not be necessary in some learning tasks, and they can be ignored in specific learning objective functions. The loss function \(\mathcal{L}\) is a general function, which can be utilized in various machine learning methods, including supervised, unsupervised, and semi-supervised approaches. The Eq.(1) is called the model-level merging, if \(\mathbf{\theta}_{j}\) is fixed, which is equivalent to picking the best model among the \(J\) models. Jointly learning \(\mathbf{\theta}_{j}\) and \(g_{j}\) belongs to the wide-sense _hard_ merging, because \(\mathbf{\theta}_{j}\) may change during the merging process. While, if learning gates parameter \(g_{j}\) only, with \(\mathbf{\theta}_{j}\) held fixed, it is referred to as _soft merging_. Especially, here Eq.(1) performs the soft merging on the model, namely picking the best granule from all the granules, with each model as a granule, which is a high-level merging. In the following section, we will introduce soft merging at different levels.

### Model Merging at Various Levels of Granularity

To merge the full model of the neural networks, we can also apply the merging process at a lower level by merging the individual modules or layers. Suppose the model \(\mathcal{M}_{j}\) consists of \(L\) layers; we can disassemble \(\mathcal{M}_{j}\) into individual layers \(\mathcal{M}_{j}:=\mathcal{F}(\{\Lambda_{I,j}\}_{l=1}^{L})\), where \(\mathcal{F}(\cdot)\) is a structural function to connect each layer which bears the same design for all \(J\) models, and \(\Lambda_{l,j}\) is the \(l\)th-layer in the model \(\mathcal{M}_{j}\). Some of the layers in the \(j\)-th model could aggregate to a module, which is defined as \(\Phi_{m,j}:=\mathcal{F}_{m}(\{\Lambda_{l,j}\}_{l=m}^{m^{\prime}})\) as the \(m\)-th module. Here, whether we are referring to the model \(\mathcal{M}\), the module \(\Phi\), or the layer \(\Lambda\), they all share the fundamental characteristic of being composed of linear or non-linear functions. So the model \(\mathcal{M}_{j}\) can be written as

\[\mathcal{M}_{j}=\mathcal{F}_{M}(\{\Phi_{m,j}\}_{m=1}^{M})=\mathcal{F}(\{ \Lambda_{l,j}\}_{l=1}^{L}), \tag{2}\]

where \(\mathcal{F}_{M}(\cdot)\) is the structural function to connect all the modules in the model \(\mathcal{M}_{j}\). The objective of module-level merging is to address the following problem

\[\min_{\{g_{m,j}\}_{m,j}}\mathcal{L}(\mathcal{M}(\mathbf{X}), \mathbf{Y};\mathbf{\theta})\ \ s.t.\sum_{j}g_{m,j}=1\] \[\Phi_{m}=\sum_{j}g_{m,j}\Phi_{m,j},\ \mathcal{M}=\mathcal{F}_{M}(\{ \Phi_{m}\}_{m=1}^{M}) \tag{3}\]

where \(g_{m,j}\) are the module-level gates. The gates are applied after the data through the module \(\Phi_{m}\) and the whole flow is managed by \(\mathcal{F}_{M}\). Similarly, the layer-level problem can be formulated as

\[\min_{\{g_{l,j}\}_{l,j}}\ \mathcal{L}(\mathcal{M}(\mathbf{X}), \mathbf{Y};\mathbf{\theta}),\ \ s.t.\sum_{j}g_{l,j}=1,\] \[\text{with}\ \Lambda_{l}=\sum_{j}g_{l,j}\Lambda_{l,j},\ \mathcal{M}= \mathcal{F}(\{\Lambda_{l}\}_{l=1}^{L}) \tag{4}\]

### Merging Optimization Algorithms

We consider \(g_{j}\) as a random variable following a Bernoulli distribution, however, it is not differentiable. To solve this problem, the constraint can then be reformulated in relation to the set of variables \(\{g_{j}\}_{j}=\mathbf{g}\in\{0,1\}^{J}\), and \(\|\mathbf{g}\|_{0}=1\) indicating that only one element in \(\mathbf{g}\) is allowed to be non-zero. Similarly, in Eq.(3) and (4), the constraint can be restated in terms of \(L_{0}\) norm. However, the \(L_{0}\) norm is not differentiable nor convex. A typical surrogate of \(L_{0}\) norms as \(L_{1}\) norm, as a convex constraint, but imposing the sparsity would introduce another constraint. In the paper by Louizos et al. [10], a surrogate approach was introduced. This approach utilizes a random variable governed by a hard concrete distribution to address the \(L_{0}\) norm constraint. Notably, this surrogate method retains differentiability through the implementation of the reparameterization trick.

**Hard Concrete Distribution**. The probability density function (PDF) of concrete distribution is written as,

\[p(s;\beta,\alpha)=\frac{\alpha\beta s^{\beta-1}(1-s)^{\beta-1}}{(s^{\beta}+ \alpha(1-s)^{\beta})^{2}},\ \ 0<s<1. \tag{5}\]

with the cumulative distribution function (CDF) as

\[F(s;\beta,\alpha)=\frac{1}{e^{\log\alpha+\beta(\log(1-s)-\log s)}} \tag{6}\]

where \(\alpha>0\) and \(0<\beta<1\). The parameter \(\alpha\) controls the distribution This binary-like concrete distribution is a smooth approximation of Bernoulli distribution[13], because it can be reparameterized with uniform distribution \(u\sim\mathcal{U}(0,1)\) as \(s=\text{Sigmoid}(\log(u)-\log(1-u)+\log\alpha)\), where \(\text{Sigmoid}(x)=\frac{1}{1+e^{-x}}\). However, the concrete distribution does not include \(0\) and \(1\). To tackle this problem, the [10] proposed a method stretching \(s\) to \((\gamma,\zeta)\) by \(\bar{s}=s\zeta+(1-s)\gamma\), with \(\gamma<0\) and \(\zeta>1\). Then by folding \(\bar{s}\) into \((0,1)\) by \(g=\min(1,\max(\bar{s},0))\), the hard concrete distribution has the CDF simply as

\[Q(s;\beta,\alpha)=F(\frac{s-\gamma}{\zeta-\gamma}),\ \ 0\leq s\leq 1 \tag{7}\]

and the PDF as

\[q(s;\beta,\alpha)=F(\frac{\gamma}{\gamma-\zeta})\delta(s)+\left(1-F( \frac{1-\gamma}{\zeta-\gamma})\right)\delta(s-1)\] \[+\left(F(\frac{1-\gamma}{\zeta-\gamma})-F(\frac{\gamma}{\gamma- \zeta})\right)p(\frac{s-\gamma}{\zeta-\gamma}),\ \ 0\leq s\leq 1. \tag{8}\]
The comparisons among examples of concrete, stretched concrete, and hard concrete distribution are shown in Fig.1.

**Surrogate Loss Functions** All the gates are replaced by the surrogate probability random variables following the hard concrete distribution. With the reparameterization trick [14], reformulated with the Lagrangian multiplier, the loss function for model-level merging is

\[\min_{\{\alpha_{j},\beta_{j}\}}\sum_{j}\mathcal{L}(\hat{s}_{j} \mathcal{M}_{j}(\mathbf{X}),\mathbf{Y};\boldsymbol{\theta}_{j})+\lambda(\hat{ s}_{j}-\frac{1}{J})\] \[\text{with }\hat{s}_{j}\sim q(s_{j}>0;\alpha_{j},\beta_{j}) \tag{9}\]

Similarly, we can get the module- and layer-level merging loss functions respectively as

\[\min_{\{\alpha_{m,j},\beta_{m,j}\}_{m,j}} \mathcal{L}(\mathcal{M}(\mathbf{X}),\mathbf{Y};\boldsymbol{ \theta})+\lambda\sum_{m,j}(\hat{s}_{m,j}-\frac{M}{J})\] \[\text{with }\hat{s}_{m,j}\sim q(s_{m,j}>0;\alpha_{m,j},\beta_{m,j}) \tag{10}\]

\[\min_{\{\alpha_{l,j},\beta_{l,j}\}_{l,j}} \mathcal{L}(\mathcal{M}(\mathbf{X}),\mathbf{Y};\boldsymbol{\theta })+\lambda\sum_{l,j}(\hat{s}_{l,j}-\frac{L}{J})\] \[\text{with }\hat{s}_{l,j}\sim q(s_{l,j}>0;\alpha_{l,j},\beta_{l,j}) \tag{11}\]

### General training algorithm

We have the full-model soft merging in different levels as shown in Eq.(9) - (11). If one just selects a few layers or modules to perform the soft merging, the loss function should be changed accordingly. For example, supposing to merge the 1st and the 5th layers of the models, which means a layer-level merging, it requires the training parameter \(\alpha_{1,j},\alpha_{5,j},\beta_{1,j}\) and \(\beta_{5,j}\) as the random variable and others as fixed with given gate value as \(1\), using formulation (11). Here we propose the general problem formulation for full-model and selective soft merging in different levels as

\[\min_{\boldsymbol{\alpha},\boldsymbol{\beta}}\mathcal{L}_{1}(\mathbf{X}, \mathbf{Y})+\lambda\mathcal{L}_{2}(\boldsymbol{\alpha},\boldsymbol{\beta}) \tag{12}\]

where \(\mathcal{L}_{1}\) is related to model performance and \(\mathcal{L}_{2}\) is the term controlling the merging, including the sampling process for the reparameterization. In the formulation (12), the parameters \(\boldsymbol{\alpha}\) and \(\boldsymbol{\beta}\) represent two sets of parameters selected for the process of selective soft merging. Notably, the hyper-parameter \(\lambda\) remains fixed as a user-defined tuning parameter and is not learned during training. The training methodology is relatively straightforward, involving the application of SGD in the mini-batch fashion as outlined in Table 1.

## 3 Experiments

We conducted multiple experiments at various levels of merging to demonstrate the performance of multi-model soft merging, assess the robustness of the merging process, and explore selective merging across diverse neural networks. Nevertheless, the tasks of the experiments include supervised classification and unsupervised source separation. We used the ESC-\(50\)[15], and MNIST (with mitture) as the data sets. The neural networks are Audio Spectrogram Transformer (AST) [16], ResNet18 [2], Variational auto-encoder (VAE) [17]. We apply soft merging at various levels to evaluate the performance of our proposed algorithm. By experimenting with different settings, we aim to demonstrate the versatility of our soft merging approach across a broad spectrum of tasks.

**Model-Level: 10 Models Merging**. The proposed algorithm involves model-level soft merging of 10 vision transformer (ViT) models for audio source [16] classification using the ESC-50 dataset. This approach employs parallel selection of the best model post-training, in contrast to the sequential comparison of neural network models. The dataset comprises 50 environmental sound classes, each containing 40 examples, which are divided into 1600 training and 400 validation samples. These models, initially pre-trained on ImageNet, process audio spectrograms using non-overlapping patches. Within the pool of 10 models, ranging from notably underperforming to highly competent ones, the soft-merging technique demonstrates its effectiveness even with limited training data. Furthermore, learning from validation data is accomplished within a mere 5 epochs, thereby reducing computational complexity compared to traditional sequential inference methods. The performance of the merged model, as illustrated in Fig.2,

\begin{table}
\begin{tabular}{l} \hline \hline Input: \(\mathbf{X}\), \(\mathbf{Y}\), \(\{\mathcal{M}_{j}\}\), \(\lambda\), the learning rate \(\eta\) \\ Output: \(\mathcal{M}^{*}\) \\ \hline
1: Initialize \(\boldsymbol{\alpha}\) and \(\boldsymbol{\beta}\) randomly \\
2: For \(b=0,1,\dots\) _/ *b_-th mini-batch */ \\
3: \(\mathcal{L}_{l}=\mathcal{L}_{1}(\mathcal{M}(\mathbf{X}^{(b)}),\mathbf{Y}^{(b) })+\lambda\mathcal{L}_{2}(\boldsymbol{\alpha},\boldsymbol{\beta})\) /*\(\mathbf{X}^{(b)},\mathbf{Y}^{(b)}\) \\  & as the data and labels for current mini-batch, \(\mathcal{L}_{l}\) is the loss function for full-model or selective merging*/ \\
4: \(\boldsymbol{\alpha}=\boldsymbol{\alpha}+\eta\frac{\partial\mathcal{L}_{l}}{ \partial\boldsymbol{\alpha}},\boldsymbol{\beta}=\boldsymbol{\beta}+\eta\frac{ \partial\mathcal{L}_{l}}{\partial\boldsymbol{\beta}}\) \\
5: Next \(b\) \\
6: \(\mathcal{M}^{*}\) contains the gate parameters \(\,\delta^{*}\sim q(\mathbf{s};\boldsymbol{\alpha}^{*},\boldsymbol{\beta}^{*})\) \\ \hline \hline \end{tabular}
\end{table}
Table 1: General training algorithm.
showcases its capabilities, while the learned attention parameters \(\log\alpha\) in Table 2 provide insights into model quality. Remarkably, even with unfavorable initializations, the training process successfully identifies correct gradient directions. Model 10 emerges as the top-performing choice, demonstrating the algorithm's effectiveness in model selection without the necessity for extensive hyperparameter tuning.

**Module-Level: Three Models Merging** This experiment aims to determine if our algorithm could effectively identify correct modules within a collection of both trained and untrained modules. Specifically, we took one trained ResNet18 model with MNIST and two untrained ResNet18 models, splitting each into two halves to create a total of six modules. Among these, two modules held functional values while the other four contained random values. Our objective was to discern the functional modules using learned gate values. Despite the initial poor performance of the three individual models due to the untrained modules, applying soft merging yielded promising outcomes, indicating successful learning of the correct gates. During this experiment, we utilized parameters \(\lambda=5\), with a learning rate of 0.001 across 150 epochs. The initial \(\log\mathbf{\alpha}\) values followed a Gaussian distribution with \(\mathcal{N}(0,0.01)\). The learning curve in Fig.(a)a depicted the merged model's progression, demonstrating that while the initial performance was subpar due to random gate initialization (Fig.(b)b), both training and validation accuracy improved significantly and quickly converged after around 80 epochs. Notably, the convergence of \(\log\mathbf{\alpha}\) values in Fig.(b)b did not occur within 150 epochs, indicating that the parameter does not possess inherent bounds due to the formulation in Eq. (5) and (6).

**Selective Layer-Level Merging** In our unsupervised source separation experiment, we adapted Variational Autoencoders (VAEs) for blind source separation, showcasing the capabilities of our algorithm in such settings. We applied this concept to image data, similar to audio and RF blind source separation problems[18]. By manually creating MNIST mixtures without labels, we mirrored the approach in [17]. We used two trained models with similar signal-to-interference ratio (SIR) performance and chose one layer in the encoder and one layer in the decoder to conduct the soft merging, which requires choosing a primary and secondary model. The VAE KL penalty \(\beta_{KL}\) increased up to 0.5 per epoch for 10 epochs, with \(\mathbf{\beta}=0\) and \(\lambda=1\). The gate values in the last batch are depicted in Fig. 4, where different prime model selections led to varying \(\log\mathbf{\alpha}\), and still maintaining the SIR around \(29\) but better than the one before merged.

## 4 Conclusions

Our research introduces the innovative concept of soft merging, a paradigm that addresses adaptability, efficiency, and robustness challenges in enhancing deep learning models. Our approach provides a versatile method for selectively integrating diverse neural network architectures, ultimately leading to improved model performance and a more favorable achievement of a better local optimum.

\begin{table}
\begin{tabular}{c|c|c|c|c|c} \hline
**Model \#** & \(1\) & \(2\) & \(3\) & \(4\) & \(5\) \\ \hline
**Init.** & \(0.0089\) & \(-0.0185\) & \(-0.0075\) & \(0.0276\) & **0.0059** \\
**Final** & \(-0.7604\) & \(-0.7877\) & \(0.6155\) & \(0.6509\) & \(0.6722\) \\ \hline \hline
**Model \#** & \(6\) & \(7\) & \(8\) & \(9\) & **10** \\ \hline
**Init.** & \(0.0023\) & \(0.0120\) & \(-0.0098\) & \(0.0115\) & \(-0.0003\) \\
**Final** & \(0.7943\) & \(0.7943\) & \(0.7878\) & \(0.8740\) & **0.8870** \\ \hline \end{tabular}
\end{table}
Table 2: Model-level merging \(\log\alpha\) values

Figure 4: The gate values of the last mini-batch training data.

Figure 3: Module-level merging result, using three ResNet18 models and manually split into \(6\) modules, with only two correct ones

Figure 2: Accuracy Comparison of Models and Merged Model