<figure><img src="image_1.png" /></figure>

HAL
open science

# Variance-based loss function for improved regularization

John M Hanna, Irene Vignon-Clementel

► To cite this version:

John M Hanna, Irene Vignon-Clementel. Variance-based loss function for improved regularization.
2024. hal-04846756

HAL Id: hal-04846756

https://hal.science/hal-04846756v1

Preprint submitted on 18 Dec 2024

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_2.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
Variance-based loss function for improved regularization

John M. Hannaª, Irene E. Vignon-Clementelª

ªInria, Research Center Saclay Ile-de-France, France

**Abstract**

In deep learning, the mean of a chosen error metric, such as squared or absolute error, is commonly used as a loss function. While effective in reducing the average error, this approach often fails to address localized outliers, leading to significant inaccuracies in regions with sharp gradients or discontinuities. This issue is particularly evident in physics-informed neural networks (PINNs), where such localized errors are expected and affects the overall solution. To overcome this limitation, we propose a novel loss function that combines the mean and the standard deviation of the chosen error metric. By minimizing this combined loss function, the method ensures a more uniform error distribution and reduces the impact of localized high-error regions. The proposed loss function was tested on three problems: Burger's equation, 2D linear elastic solid mechanics, and 2D steady Navier Stokes, demonstrating improved solution quality and lower maximum errors compared to the standard mean-based loss, using the same number of iterations and weight initialization.

**Keywords:** Variance-based loss function, standard deviation, outliers, physics-informed neural networks

# 1. Introduction

Deep learning has been growing rapidly in the past decades. The main reasons for that is the availability of large datasets and the ground breaking technology of graph processing units which made it possible to train large neural network models with huge datasets.

New neural network architectures have made it possible to improve the accuracy of deep learning tasks. For example, graph neural networks [1] made it easier to deal with data that can be organized in graphs which has many applications in molecular biology [2] and social networks [3]. Transformer architectures [4] were also presented with the novel self attention mechanism which improved the accuracy in natural language processing [5] and computer vision [6] tasks.

In the recent years, neural networks where presented to solve partial differential equations (PDE) introducing physics-informed neural networks (PINN) [7]. PINN promises the reduced need of big datasets since the physics, governed by PDES, are used to regularize the predictions. The main idea is using the strong approximation capabilities of neural networks [8] to approximate the solution field of the PDE. The solution is obtained by minimizing a combined loss function which represents the data mismatch (initial/ boundary conditions enforcement and/or sparse data in the domain) and the residual of the PDE over points sampled in the spatiotemporal domain called collocation points. The residual is usually obtained using automatic differentiation [9]. Since, the introduction of PINN, it has been applied in several fields including solid mechanics [10], fluid mechanics [11, 12, 13], flow in porous media [14, 15, 16], composites manufacturing [17, 18], etc.

One of the main hyperparameters in deep learning, whether for supervised learning tasks or physics-informed neural networks tasks, is the loss function. A good choice of the loss function can lead to significant improvement of the optimization process thus good accuracy can be reached in few number of epochs. In general, the choice of the loss function is usually the mean over the training points of a chosen error function. This leads to variety of loss functions including, mean squared error, mean absolute error, or the Huber loss [19].
These common choices are based on mean values and generally not sensitive to outliers, specially the mean absolute error or Huber loss; sometimes these outliers can deteriorate the predictions. This can happen in PINN specifically in regions with high gradients or discontinuities that can lead high localized residuals as compared to other regions, which in turn can deteriorate the solution.

In this work, we present a new loss function that targets this issue. The core idea is based on an addition to the loss function. This addition includes the standard deviation of the chosen error to be minimized. By this addition, we are ensuring that minimizing the loss function can reduce both the mean and standard deviation of the error, thus having less outliers than minimizing only the mean. The method is tested on three PINN problems: Burgers problem, 2D linear elasticity, and 2D steady-state Navier Stokes example.

The article is organized as follows: section 2 is devoted for the introduction of the new proposed loss function, section 3 offers different examples from physics-informed neural networks and their results and comparison to standard loss function. Section 4 offers a discussion and conclusion to the paper.

## 2. Proposed loss function

In general, commonly used loss function take the form of a mean of a chosen error; this takes the form as:

$$ \mathcal{L} = \frac{1}{N} \sum_{i=1}^{N} e_i, \qquad (1) $$

where N is the number of training points, $e_i$ is the chosen error function.

The $e_i$ term can represent squared error $(\hat{Y}(x) - Y)^2$, absolute error $|\hat{Y}(X) - Y|$, etc. where $\hat{Y}$ is the approximation function and Y the label data.

The added term to the modified loss function represents the standard deviation of the error function e. The full loss function with the added term can be written as:

$$ \mathcal{L} = \alpha \frac{1}{N} \sum_{i=1}^{N} e_i + (1 - \alpha) \sqrt{\frac{\sum_{i=1}^{N} (e_i - \bar{e})^2}{N}}, \qquad (2) $$

where $\bar{e}$ is the error mean which can be obtained as $\frac{\sum_{i=1}^{N} e_i}{N}$. $\alpha$ is a hyperparameter that takes values between 0 and 1 which identifies the contribution of each term in the loss function.

## 3. Numerical examples

In this section, we show three problems solved using PINN. In all the shown examples, a network architecture of 5 hidden layers and 20 neurons each is employed. Tanh activation function is used for the hidden layers and linear function for the output layer. Adam optimizer is used with a fixed learning rate of 0.001.

### 3.1. Physics-informed neural networks: Burger's equation

In this example, we solve the Burger's problem in one dimension as introduced by Raissi et al. [7]. The Burger's equation along with the initial and boundary conditions are written as:
$$
\begin{align}
u_t + u u_x &= (0.01/\pi) u_{xx}, & x \in [-1, 1], t \in [0, 1], \nonumber \\
u(x, 0) &= -\sin(\pi x), & x \in [-1, 1], \nonumber \\
u(-1, t) &= u(1, t) = 0, & t \in [0, T]. \tag{3}
\end{align}
$$

The residual of the Burger’s equations is defined as

$$
r(x, t) = u_t + u u_x - \nu u_{xx}, \tag{4}
$$

and the solution field $u(x, t)$ is approximated with a neural network. The parameters of the neural network is optimized
by minimizing a combined loss function that is written as:

$$
\mathcal{L} = \mathcal{L}_{\text{data}} + \mathcal{L}_{\text{PDE}}, \tag{5}
$$

where for classical MSE training

$$
\mathcal{L}_{\text{data}} = \frac{1}{N_u} \sum_{i=1}^{N_u} \left| u_{\text{pred}}(x_u^i, t_u^i) - u_{\text{data}}(x_u^i, t_u^i) \right|^2, \quad (6)
$$

while for the our new loss function

$$
\mathcal{L}_{\text{data}} = \frac{\alpha}{N_u} \sum_{i=1}^{N_u} \left| u_{\text{pred}}(x_u^i, t_u^i) - u_{\text{data}}(x_u^i, t_u^i) \right|^2 + (1-\alpha) \sqrt{\frac{\sum_{i=1}^{N_u} \left( \left| u_{\text{pred}}(x_u^i, t_u^i) - u_{\text{data}}(x_u^i, t_u^i) \right|^2 - \bar{e}^2 \right)}{N_u}}, \quad (7)
$$

where

$$
\bar{e} = \frac{1}{N_u} \sum_{i=1}^{N_u} \left| u_{\text{pred}}(x_u^i, t_u^i) - u_{\text{data}}(x_u^i, t_u^i) \right|^2, \quad (8)
$$

and for classical MSE training

$$
\mathcal{L}_{\text{PDE}} = \frac{1}{N_f} \sum_{j=1}^{N_f} \left| r(x_f^j, t_f^j) \right|^2, \qquad (9)
$$

while for the our new loss function

$$
\mathcal{L}_{\text{PDE}} = \frac{\alpha}{N_f} \sum_{j=1}^{N_f} \left| r(x_f^j, t_f^j) \right|^2 + (1-\alpha) \sqrt{\frac{\sum_{i=1}^{N_f} \left( \left| r(x_f^j, t_f^j) \right|^2 - \bar{e}_r^2 \right)}{N_f}}, \quad (10)
$$

where

$$
\bar{e}_r = \frac{1}{N_f} \sum_{j=1}^{N_f} \left| r(x_f^j, t_f^j) \right|^2, \quad (11)
$$

$N_u$ is the number of training points for the initial and boundary conditions, $N_f$ is the number of collocation points used for the PDE residual, $u_{\text{pred}}$ is the predicted solution from the neural network, $u_{\text{data}}$ is the data for initial and boundary conditions, $x_u^i$ and $t_u^i$ are the spatial and temporal coordinates for data points, $x_f^j$ and $t_f^j$ are the spatial and temporal coordinates for collocation points.
The solutions are obtained using 5000 Adam iterations. The computational cost using MSE loss is 24.9 seconds and 25.1 seconds using the variance-based loss. The PINN solution, the absolute value of the residual field, and log of the absolute residual field using MSE training and the proposed loss are shown in figure 1.

<figure><img src="image_3.png" /><figcaption>Figure 1: On top Burgers problem solution with PINN, absolute residual field, and log of residual field using the classical mean loss function. In the bottom, the solution, absolute residual field, and log of absolute residual field using the new Variance-based loss function.</figcaption></figure>

As shown in figure 1, the maximum value of the absolute residual field is significantly reduced when using the proposed loss as compared to using mean squared error function. This leads to a better approximation of the overall solution.

## 3.2. Physics-informed neural networks: Solid mechanics

In this example, we solve a linear elastic solid mechanics probelm in 2D as introduced by Haghighat et al. [10]. The problem details and definition is shown in figure 2.

<figure><img src="image_4.png" /><figcaption>Figure 2: Solid mechanics problem setup and boundary conditions [10].</figcaption></figure>
As recommended in the article, 5 neural networks are designed to approximate the displacements and the different components of the stress tensor. In our implementation, we enforced the boundary conditions exactly using distance functions. For example, $\sigma_{xx}(x, y)$ is approximated as $x \times (1-x) \times \hat{\sigma}_{xx}(x, y)$, where $\hat{\sigma}_{xx}$ is a neural network approximation.

The solutions using the classical mean error loss function and the new proposed loss is shown in figures 3 and 4.

<figure><img src="image_5.png" /><figcaption>Figure 3: On top Solid mechanics problem solution of $u_x$ with PINN, along with exact solution and absolute error using classical loss function. In the bottom, Solid mechanics problem solution of $u_x$ with PINN, along with exact solution and absolute error using new variance-based loss function.</figcaption></figure>
<figure><img src="image_6.png" /><figcaption>Figure 4: On top Solid mechanics problem solution of $u_y$ with PINN, along with exact solution and absolute error using MSE classical loss function. In the bottom, Solid mechanics problem solution of $u_y$ with PINN, along with exact solution and absolute error using new variance-based loss function.</figcaption></figure>

From figures 3 and 4, it is clear that using the newly proposed loss function provided better solutions than using a classical mean squared error. As can be seen in for the displacement in the x direction in figure 3, the maximum absolute error was nearly reduced by an order of 20. Where it can be noticed, the solution using MSE has localized error region, and this is not the case for the variance-based loss.

For the displacement in the y-direction, the maximum error is reduced by an order of 10 as shown in figure 4.

The solutions are obtained using 20,000 Adam iteration. The computational time using MSE loss is 222 seconds and 225 seconds using the new variance-based loss.

### 3.3. Physics-informed neural networks: Fluid mechanics

In this example, we solve steady state Navier-Stokes equations in 2D. The description of the problem is defined in figure 5. The problem in hand has one inlet velocity with a parabolic velocity profile and one outlet with zero pressure. The rest of the boundary has no slip boundary condition.

<figure><img src="image_7.png" /><figcaption>Figure 5: Fluid mechanics problem setup and boundary conditions.</figcaption></figure>
The PDE system to be solved is written as:

$$ \rho \left( u \frac{\partial u}{\partial x} + v \frac{\partial u}{\partial y} \right) = - \frac{\partial p}{\partial x} + \mu \left( \frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} \right), \quad (12) $$

$$ \rho \left( u \frac{\partial v}{\partial x} + v \frac{\partial v}{\partial y} \right) = - \frac{\partial p}{\partial y} + \mu \left( \frac{\partial^2 v}{\partial x^2} + \frac{\partial^2 v}{\partial y^2} \right), \quad (13) $$

$$ \frac{\partial u}{\partial x} + \frac{\partial v}{\partial y} = 0. \quad (14) $$

Here, $u, v$ are the velocity components in the $x$- and $y$-directions, respectively. $\rho$ is the fluid density, $p$ is the pressure, and $\mu$ is the dynamic viscosity.

In this example the maximum velocity is chosen to be 450 m/s, with a density and viscosity of unity. These choices given the geometry in hand will lead to a non parabolic velocity profile at the outlet after the curvature as observed from the reference solution in figure 6a.

The no-slip boundary condition is enforced automatically using an approximation of the distance function using a separate neural network. In this example, 1000 Adam iterations are employed. The results are shown in figures 6 and 7.
<figure><img src="image_8.png" /><figcaption>(a) Velocity magnitude using Fenics code (reference solution).</figcaption></figure>

<figure><img src="image_9.png" /><figcaption>(b) Velocity magnitude obtained using MSE in PINN.</figcaption></figure>

<figure><img src="image_10.png" /><figcaption>(c) Velocity magnitude obtained using the new loss function in PINN.</figcaption></figure>

<figure><img src="image_11.png" /><figcaption>(d) Absolute Error in velocity magnitude using MSE loss.</figcaption></figure>

<figure><img src="image_12.png" /><figcaption>(e) Absolute Error in velocity magnitude using the new loss.</figcaption></figure>

Figure 6: Velocity magnitude using Fenics as a reference solution along with solutions from PINN using MSE loss and the new variance-based loss, along with the absolute errors.
<figure><img src="image_13.png" /><figcaption>(a) Pressure field using Fenics code (reference solution).</figcaption></figure>

<figure><img src="image_14.png" /><figcaption>(b) Pressure field obtained using PINN with MSE loss function.</figcaption></figure>

<figure><img src="image_15.png" /><figcaption>(c) Pressure field obtained using PINN with the new proposed loss.</figcaption></figure>

<figure><img src="image_16.png" /><figcaption>(d) Absolute error in pressure field using PINN with MSE loss.</figcaption></figure>

<figure><img src="image_17.png" /><figcaption>(e) Absolute error in pressure field using PINN with the new loss.</figcaption></figure>

Figure 7: Pressure fields using Fenics (reference solution), PINN with MSE loss and PINN with the new variance-based loss, along with the absolute errors.

As can be seen from figure 6, the velocity magnitude reference solution has an outlet velocity that has a shifted maximum towards the top due to centrifugal forces. This behavior is captured when using the variance-based loss while not captured when using MSE loss. It can be clearly noticed that the maximum error value is reduced using the new proposed loss.
For the pressure solutions in figure 7, it is noticed the pressure solution using the new loss function is closer to the reference solution from using MSE loss. Moreover, the maximum error in the pressure estimation using the variance-based loss is almost half that of the MSE loss.

# 4. Discussion and conclusion

In the previous examples, we can clearly notice an improvement to the PINN solution when using the new variance-based loss function. This simple modification to the loss function has an effect of reducing the maximum error and having less localized error zone giving an overall improvement of the solution behavior.

In all our cases, we used an $\alpha$ of 0.5, giving equal contribution to the mean and standard deviation contributions. For future work, a proper assessment of the effect of changing $\alpha$ needs to be studied.

Only Adam optimizer was used in all the cases, in the Burgers and linear elasticity examples, it is seen to be enough to obtain good accuracy. For the Navier-Stokes example, Adam did not provide the best solution and for the future a more powerful optimizer can be used along with Adam such as BFGS to improve the solution. But for the purpose of testing our new loss function, Adam optimizer was enough to show our point.

It should be noted that from a practical point of view and implementation wise, the added term to the loss function is simple to introduce. In TensorFlow, for example, this can be implemented using the "tf.math.reduce_std" function. In PyTorch, it can be introduced using "torch.std" function.

In terms of added computational cost, the new loss function has a very low extra cost when compared to traditional MSE loss. A proper estimation is to be studied in the future.

# References

[1] Z. Wu, S. Pan, F. Chen, G. Long, C. Zhang, S. Y. Philip, A comprehensive survey on graph neural networks, IEEE transactions on neural networks and learning systems 32 (1) (2020) 4–24.

[2] X.-M. Zhang, L. Liang, L. Liu, M.-J. Tang, Graph neural networks and their current applications in bioinformatics, Frontiers in genetics 12 (2021) 690049.

[3] W. Fan, Y. Ma, Q. Li, Y. He, E. Zhao, J. Tang, D. Yin, Graph neural networks for social recommendation, in: The world wide web conference, 2019, pp. 417–426.

[4] A. Vaswani, Attention is all you need, Advances in Neural Information Processing Systems (2017).

[5] T. Wolf, L. Debut, V. Sanh, J. Chaumond, C. Delangue, A. Moi, P. Cistac, T. Rault, R. Louf, M. Funtowicz, et al., Transformers: State-of-the-art natural language processing, in: Proceedings of the 2020 conference on empirical methods in natural language processing: system demonstrations, 2020, pp. 38–45.

[6] K. Han, Y. Wang, H. Chen, X. Chen, J. Guo, Z. Liu, Y. Tang, A. Xiao, C. Xu, Y. Xu, et al., A survey on vision transformer, IEEE transactions on pattern analysis and machine intelligence 45 (1) (2022) 87–110.

[7] M. Raissi, P. Perdikaris, G. E. Karniadakis, Physics-informed neural networks: A deep learning framework for solving forward and inverse problems involving nonlinear partial differential equations, Journal of Computational Physics 378 (2019) 686–707.
[8] K. Hornik, M. Stinchcombe, H. White, Multilayer feedforward networks are universal approximators, Neural networks 2 (5) (1989) 359-366.

[9] A. G. Baydin, B. A. Pearlmutter, A. A. Radul, J. M. Siskind, Automatic differentiation in machine learning: a survey, Journal of machine learning research 18 (2018).

[10] E. Haghighat, M. Raissi, A. Moure, H. Gomez, R. Juanes, A physics-informed deep learning framework for inversion and surrogate modeling in solid mechanics, Computer Methods in Applied Mechanics and Engineering 379 (2021) 113741.

[11] Z. Mao, A. D. Jagtap, G. E. Karniadakis, Physics-informed neural networks for high-speed flows, Computer Methods in Applied Mechanics and Engineering 360 (2020) 112789.

[12] S. Cai, Z. Mao, Z. Wang, M. Yin, G. E. Karniadakis, Physics-informed neural networks (pinns) for fluid mechanics: A review, Acta Mechanica Sinica (2022) 1-12.

[13] M. Raissi, A. Yazdani, G. E. Karniadakis, Hidden fluid mechanics: Learning velocity and pressure fields from flow visualizations, Science 367 (6481) (2020) 1026-1030.

[14] J. M. Hanna, J. V. Aguado, S. Comas-Cardona, R. Askri, D. Borzacchiello, Residual-based adaptivity for two-phase flow simulation in porous media using physics-informed neural networks, Computer Methods in Applied Mechanics and Engineering 396 (2022) 115100.

[15] M. M. Almajid, M. O. Abu-Al-Saud, Prediction of porous media fluid flow using physics informed neural networks, Journal of Petroleum Science and Engineering 208 (2022) 109205.

[16] J. M. Hanna, J. V. Aguado, S. Comas-Cardona, R. Askri, D. Borzacchiello, Sensitivity analysis using physics-informed neural networks, Engineering Applications of Artificial Intelligence 135 (2024) 108764.

[17] S. A. Niaki, E. Haghighat, T. Campbell, A. Poursartip, R. Vaziri, Physics-informed neural network for modelling the thermochemical curing process of composite-tool systems during manufacture, Computer Methods in Applied Mechanics and Engineering 384 (2021) 113959.

[18] J. M. Hanna, J. V. Aguado, S. Comas-Cardona, Y. Le Guennec, D. Borzacchiello, A self-supervised learning framework based on physics-informed and convolutional neural networks to identify local anisotropic permeability tensor from textiles 2d images for filling pattern prediction, Composites Part A: Applied Science and Manufacturing 179 (2024) 108019.

[19] P. J. Huber, Robust estimation of a location parameter, in: Breakthroughs in statistics: Methodology and distribution, Springer, 1992, pp. 492-518.