Surrogate Model Training
Use a Surrogate Model Training study node () to add functionality for training a data-driven surrogate model. A surrogate model is a simpler and computationally cheaper model, which you can use to approximate the behavior of a more complex and computationally expensive model (the full finite element model). In COMSOL apps, for example, faster model evaluation using the surrogate model provides users of the apps with a more interactive user experience. You create surrogate models by training a deep neural network (DNN), for example. The surrogate model training is typically based on output data from a large parametric sweep of the model for which you want to create a surrogate model. The training of a surrogate model needs a number of inputs and outputs, where the outputs are considered as functions of the inputs, which replace the full finite element solution. A large number of data points are needed in order to fully describe how the inputs map to the outputs. The outputs can be captured using domain point probes, for example.
A straightforward parametric sweep could densely and uniformly distribute input points, but such an approach would be inefficient. Random sampling is another alternative but with inherent drawbacks such as nonuniform sampling and potential failure to cover the entire input space. Instead, a more strategic approach is to use a design of experiments (DOE) method, carefully sampling within the parameter space. The Surrogate Model Training study uses Latin hypercube sampling (LHS), which is a DOE method that generates a dataset that uniformly covers the input space without requiring an excessive number of finite element computations. This property of LHS makes it an efficient method for data generation intended for the purpose of training a surrogate model.
The more data points that are available, the more accurately the surrogate model will be able to represent the actual solution. However, generating a large number of data points requires a large number of simulations to be run, so there is a tradeoff between the time it takes to generate all the data points and the desired accuracy the surrogate model.
Tubular Reactor Surrogate Model Application: Application Library path COMSOL_Multiphysics/Applications/tubular_reactor_surrogate.
If you have the Battery Design Module, see Surrogate Model Training of a Battery Rate Capability Model: Application Library path Battery_Design_Module/Applications/lib_rate_capability_surrogate.
For additional information about surrogate models in connection with uncertainty quantification, see the Uncertainty Quantification Module User’s Guide.
To add a Surrogate Model Training study, right-click a Study node and choose Surrogate Model Training. You can only have one Surrogate Model Training node in each study.
Click the Compute button () to initiate the surrogate model training and create a surrogate model.
If you choose Show Default Solver from the Study node, the Surrogate Model Training study generates and sets up a Design Of Experiments job and its subsequence node (see the Uncertainty Quantification Module User’s Guide). The Design Of Experiments job is controlled by and synchronized to the Surrogate Model Training study. If you click Compute from the Study node, the Surrogate Model Training study identifies and runs its Design Of Experiments job if it exists; else it first creates a Design Of Experiments job.
The Settings window for the Surrogate Model Training study include the following sections:
Study Settings
From the Compute actions list, choose one of the following options: Compute and build surrogate model (the default) or Improve and build surrogate model. Compute and build surrogate model will discard data from the previous run (if the same table is used) or start a new run using a new table. Improve and build surrogate model will add more data to an existing table, which can be useful if you realize after having examined the trained SM function that it does not seem accurate enough. The Surrogate Model Training study outputs training data tables and trained surrogate models for each quantity of interest. If you choose Design of experiments (No surrogate model) for the surrogate model in the Surrogate model list below, a training data table will be the only result.
From the Output table group list, choose New to create a new output table group or choose any existing table group. All tables generated by Surrogate Model Training study will be grouped under this output table group.
From the Surrogate model list, choose None (Design of Experiments) (the default) to only generate training data, or choose Gaussian process, Polynomial chaos expansion, Deep neural network, or Least-squares fit to additionally set up surrogate models. Gaussian process and Polynomial chaos expansion require a license for the Uncertainty Quantification Module.
For Gaussian process, also specify a covariance and a mean. From the Covariance list, choose Squared exponential, Matérn 3/2 (the default), Matérn 5/2, or Single-layer neural network. From the Mean list, choose Constant (the default), Linear, or Quadratic.
For Polynomial chaos expansion, also specify a Settings method. Choose Automatic (the default) or Manual. If you chose Manual, specify a value in the Maximum polynomial degree field (default value: 30), which terminates the increase of order for the PCE construction. You can also specify a value in the q-norm field (default: 0.5), which determines the truncation level of the polynomial basis. If you chose Automatic, the training will automatically determine the required polynomial degree needed to obtain suitable accuracy.
For Deep neural network, under Layers, define the layers in the DNN. The first layer is always the input layer of the network and specifies the number of input features of the network, which will be automatically detected. The last layer is the output layer, and the layers in between are the hidden layers. In the Type column, choose Input for the input layer (the default), and for the hidden layers and output layer, choose Dense (the default). In the Settings column for the current layer, you can see its settings. You can edit the table using the buttons under the table: Use the Move Up (), Move Down (), and Delete () buttons and the fields under tables to edit the table contents. Or right-click a table cell and select Move Up, Move Down, or Delete. The Add Hidden Layer button () adds a new hidden layer to the list right before the output layer. Use the Clear Hidden Layers button () to clear all the hidden layers of the table and revert the input and output layers to their default. Underneath the table of layers, in the Output features field for the hidden layers, specify the number of output features, which are the number of nodes (default: 1). Choosing the number of layers and nodes in a neural network is often an iterative process that involves a combination of knowledge about the specific problem and data, empirical testing, and a bit of trial and error.
You can also specify an activation function using the Activation list. An activation function in a DNN defines how the weighted sum of the input is transformed into an output from a node or nodes in a layer of the network. The default activation is tanh, for a hyperbolic tangent function, which is an S-shaped function. You can also choose Linear (none) for a linear (that is, no) activation function; ReLU for a rectified linear unit, an activation function defined as the positive part of its argument; ELU for an exponential linear unit; or Sigmoid for a sigmoid function. The default activation, tanh, is usually a good choice. ELU and ReLU are less smooth than the other functions, so avoid them if the trained function later needs to be differentiated. Choosing Linear (none) is only useful for the last layer.
For Least-squares fit, specify the definition of the function in the Expression field. For example, a1*x1+a0 for a linear function with two parameters, a0 and a1, and one input parameter to the surrogate model, x1, defined under the Input Parameters section. In the table underneath, add the parameters that are used in the expression in the Parameter column. For the linear function example above, this would correspond to adding a0 and a1 in the Parameter column. In the Values column, choose the initial values (default: 0) for the parameters. The Scale column can be used to rescale each parameter with its specified scale. By default there is no scaling. Use the Lower bound and Upper bound columns to set lower and upper bounds on the parameters. Use the Move Up (), Move Down (), Delete (), and Clear Table () buttons underneath the table to move and remove rows and to clear the table. Use the Load from File () and Save to File () buttons to load or save data to or from the table. You can also click the downward arrow beside the Load from File button and choose Load From () to open the fullscreen Select File window.
Select the Train model automatically checkbox if the surrogate models should also be trained after the training data has been generated and the surrogate models have been set up. By default, this checkbox is not selected.
Quantities of Interest
In the table under Quantities of Interest (Outputs), define the outputs from the surrogate models (quantities of interest). The quantities of interest can be either global or nonglobal. Global quantities of interest can only depend on global input parameters (for example, point probe expressions). Nonglobal quantities of interest can additionally depend on space, time, and other study-dependent inputs.
In the Expression column, type the expression for the output (comp1.ppb1, for example, for a Point Probe Expression 1 in Component 1). In the Include study-dependent input column, select Reduce to single global output if the quantity of interest is global or Configure study-dependent input if the quantity of interest is nonglobal. In the Study-dependent input description column you can see a small summary of the settings for each quantity of interest. If the Surrogate model setting is not Design of experiments (No surrogate model), meaning it is either Gaussian process, Polynomial chaos expansion, Deep neural network, or Least-squares fit, there is an additional Function name column. Here, you can give the name for the corresponding output in the surrogate model. To avoid name clashing, the resulting function name in the surrogate model will be the concatenation of the tag of the surrogate model and the name given in this column. For example, if the surrogate model has the tag dnn1, and the name provided in this table is func1, the resulting function name in the surrogate model corresponding to this quantity of interest will be dnn1_func1. If left empty, a valid default function name will be provided. Use the Move Up (), Move Down (), and Delete () buttons and the fields under tables to edit the table contents. Or right-click a table cell and select Move Up, Move Down, or Delete. The Add button () adds a new output to the list. Use the Clear Table button () to clear the entire table.
Underneath the table various settings are available — for each quantity of interest — based on whether the quantity of interest is global or nonglobal.
Reduce to Single Global Output
When there is a parametric sweep in the study, a selection can be made over the outer solutions (parametric solutions). From the Individual outer solution selection list, choose Summation, Maximum, or Minimum. For Summation, the quantity of interest is defined as the summation of the expression over all the outer solutions. For Maximum (or Minimum), the quantity of interest is defined as the maximum (or minimum) of the expression taken over all the outer solutions.
A selection can also be made over the inner solutions. First the selection is made over the outer solutions and then over the inner solutions. From the Individual solution selection list, choose Automatic (the default), Summation, Minimum, Maximum, Use first, or Use last. The Automatic solution to use will be the last solution for time-dependent and parametric solutions, while for eigenvalue and eigenfrequency solutions, it will be the first solution. You can override this mechanism by selecting any of the other methods. For Summation, the quantity of interest is defined as the summation of the expression over all the solutions. For Maximum (or Minimum), the quantity of interest is defined as the maximum (or minimum) of the expression taken over all the solutions. Also note that evaluation operators like at() and with() can be used in the expression, making it possible to evaluate even more general quantities from dynamic solutions.
Depending on the selection for Surrogate model, an additional setting will be available:
For Design of experiments (No surrogate model), select from the Output table list to which table the data should be written, or select New (the default) if a new table should be generated.
For Gaussian process, Polynomial chaos expansion, Deep neural network, or Least-squares fit, select from the Gaussian process function, PCE function, DNN function, or Least-squares fit list, respectively, which existing surrogate model function to use or select New if a new surrogate model function should be created. If an existing surrogate model function is selected and a table is already attached to it, the data will be written to this table.
Configure Study-Dependent Input
Only quantities of interest evaluated with the same sampling and selection methods can be written to the same table (or file). This means that global and nonglobal quantities of interest will be separated in separate tables (or files). In the same way, not all nonglobal quantities of interest will be written to the same file. Automatically all quantities of interest evaluated with the same sampling and selection methods will be written to the same file. If Surrogate model is not set to Design of experiments (No surrogate model), a separate surrogate model will also be created for every group.
Specify which sampling method to use from the Geometry sampling list to sample the quantity of interest on the geometry. Select None if the quantity of interest does not have spatial dependence. To create a Geometry Sampling node, right-click the Surrogate Model Training node, choose Add Geometry Sampling to, and then the component to which you want to add it. Or create the node directly under the Definitions of the Component using Surrogate Model Sampling > Geometry Sampling.
Specify with Innermost parameter selection a selection for the innermost solutions. In a time-dependent parametric simulation with an auxiliary sweep, the innermost parameter will be time. The parameters in the auxiliary sweep are also inner parameters, but only one parameter can be the innermost parameter. For frequency-dependent simulations, the innermost parameter will be frequency, and for stationary simulations, the innermost parameter will be the continuation parameter. Choose from Summation, Minimum, Maximum, Use last, Use first, and All. For eigenvalue and eigenfrequency simulations the default is Use first; otherwise, the default is All. For Summation, the quantity of interest is defined as the summation of the expression over all the innermost solutions. For Maximum (or Minimum), the quantity of interest is defined as the maximum (or minimum) of the expression over all the innermost solutions. For Use last (or Use first), the quantity of interest is defined as the expression evaluated with the last (or first) innermost solution. Select all innermost solutions with All. If the innermost parameter is time you can also choose Interpolation. The quantity of interest is then defined as the expression interpolated at the times specified in Times. Ranges and vector-valued expressions can be used; see Entering Ranges and Vector-Valued Expressions for more on ranges and vector-valued expressions.
If there are more parameters in the study besides the innermost parameter, the Study-dependent input configuration table can be used to make further selections in the other parameters. In the Study-dependent input parameter name column all the parameters besides the innermost parameter are listed. Specify, in the Parameter selection column, which solutions to select for each parameter. Choose from Last and All (the default). For Last, the quantity of interest is defined as the expression evaluated using the last solution for that parameter. Select all solutions with All. Last can only be selected for inner parameters when the sweep type for the auxiliary sweep is All combinations. Select the checkbox in the Include in training column if the surrogate model should have an input for this parameter. By default, an input will be created for the surrogate model.
As the data generated for nonglobal quantity of interests is typically large, the data is stored on file. Specify with the File list if the file should be saved embedded in the model or external. Choose Embedded file if the file should be stored embedded in the model — the file will be attached to a table. Choose External file to save the file outside the model at the path specified with Filename. It is not possible to directly specify the filename; instead specify the Base file path, which is shared among all quantities of interest. It can be either a folder or a filename, and both relative and absolute paths are allowed. The file where the quantity of interest will be saved is in a file at the base file path with the name as specified in the Filename field. A small identifier has been added to the filename to mark which quantities of interest are in the file. Click the Browse button () to browse the file system.
Depending on the selection for Surrogate model, an additional setting will be available:
For Design of experiments (No surrogate model), select from the Output table to which table the data should be written, or select New (the default) if a new table should be generated.
For Gaussian process, Polynomial chaos expansion, Deep neural network, or Least-squares fit, select from the Gaussian process function, PCE function, DNN function, or Least-squares fit list, respectively, which existing surrogate model function to use or choose New if a new surrogate model function should be created. If an existing surrogate model function is selected and a table is already attached to it, the data will be written to this table.
Input Parameters
In this section you define the input parameters for the surrogate model training.
For the input parameters, in the Parameters column, choose the parameter from a list of all global parameters in the model. In Source type column, choose Analytic (the default) or Data.
If you chose Analytic, you can define the following properties for the selected parameter underneath the table:
In the Distribution list, choose the distribution for the input parameter: Uniform (the default), Normal(μ,σ), LogNormal(μ,σ), Gamma(k,θ), Beta(α,β), Weibull(λ,k), or Gumbel(μ,β). All distributions except the uniform distribution have two distribution parameters shown under the Distribution list, such as the Mean and Standard deviation for a normal distribution and Shape and Scale for a gamma distribution. You specify the distribution parameters in the corresponding text field. All distributions except the uniform distribution and beta distribution have CDF-Lower, CDF-Upper shown under the Distribution list.
In the CDF-Lower list, choose the cumulative distribution function level for your lower bound: 30%, 10%, 1%, 0.1% (the default), 1E-4, 1E-5, 1E-6, 1E-7, or Manual. These bounds automatically compute a lower bound by using the inverse cumulative distribution function.
In the CDF-Upper list, choose the cumulative distribution function level for your upper bound: 70%, 90%, 99%, 99.9% (the default), 1-1E-4, 1-1E-5, 1E-6, 1-1E-7, or Manual. These bounds automatically compute an upper bound by using the inverse cumulative distribution function.
For Manual bounds, you can enter bounds and units for the input parameter in the Lower bound, Upper bound, and Unit columns. For the bounds, you can use unit syntax such as 0.45[mm], and for the unit, add its abbreviation, such as Pa for pascal.
The chosen analytic distribution appears in the Parameter description column.
If you chose Data in the Source type column, you can instead specify the following properties:
From the Data source list, choose Specified values (the default), or choose Result table to take the data from the Design of Experiments results table.
If you chose Specified values, specify the values to use in the Specified values field. Click the Range button () to define a range of values.
If you chose Result table, specify the column to use from the Column list.
Specify a unit for the values in the Unit field. To do so, use its abbreviation, such as Pa for pascal.
If the source type for the parameter is Analytic, you can add correlations groups. Doing so allows sampling of parameters that are not statistically independent by specifying a correlation matrix. You could select a subset of all input parameters in the same correlation group and set the correlation matrix to specify the correlation between each pair of two parameters. Multiple correlation groups can be added in one study, and one parameter can only appear in one correlation group. The correlation matrix is a symmetric semidefinite matrix where all the diagonal elements are equal to 1 and all the off-diagonal elements are between [−1, 1].
Under Sampling settings, specify the following setting if the source type is Analytic:
From the Number of input points type list, choose Manual (the default), or choose Automatic if the Surrogate model list is set to Gaussian process or Polynomial chaos expansion.
If Number of input points type is set to Manual, then in the Number of input points field, if Compute action is set to Compute and build surrogate model, or Number of input points for improve, if Compute action is set to Improve and build surrogate model, specify the number of input points to use (default: 20 or 10). You may want to use a larger number to train the surrogate model to a sufficient degree of accuracy. The number of input points must be chosen empirically.
From the Random seed type list, choose Automatic (the default), Manual, or Current computer time. If you chose Manual, enter a seed in the Initial random seed field. The random seed affects which sampling points you get from the Latin hypercube sampling.
You can edit the table using the buttons under the table:
In general, use the Move Up (), Move Down (), and Delete () buttons and the fields under tables to edit the table contents. Or right-click a table cell and select Move Up, Move Down, or Delete.
The Add button () adds a new input parameter to the list.
Use the Clear Table button () to clear the entire table.
Advanced Settings
Select the Accumulated probe table checkbox to add accumulated probe tables for the result from the surrogate model training. From the Output table list, choose New to create a new table, or choose any existing table. The Use all probes checkbox is selected by default to accumulate all the model probes in the table. If you clear this checkbox, the probes selected from the Probes list are used.
From the Error handling list, choose Stop immediately (the default) or Skip problematic parameters.
From the Keep model evaluations in memory list, choose Only last (the default) to only keep the last model evaluation in memory, or choose All to keep all model evaluations.
From the Default solver sequence generation list choose Using global parameters (the default) or Using each parameter tuple. Use this setting to control whether a solver sequence should be generated using global parameters or each parameter tuple.
Select the Reuse the solution from the previous step checkbox if you want the surrogate model training algorithm to reuse the solution from the previous step. It is useful if you are using an iterative solver and the solutions for different parameter values are similar enough so that it is faster to start from the previous solution instead of starting the solver from scratch.
Select the Distribute model evaluation checkbox if you are running the COMSOL software on a multiprocessor system and want to distribute the model evaluation to each compute node for improved performance. Using Distribute model evaluation is equivalent to using a Distributed parametric sweep.
In order for the Distribute model evaluation checkbox to function, you must start COMSOL with a setup for running COMSOL on clusters or other multicore system using, for example, comsol -nn 2 -nnhost 2 on Linux® or mpiexec -n 2 comsolcluster.exe -nnhost 2 on Windows® to start 2 compute nodes. You can also run COMSOL with a distributed parametric sweep from the command line. See Running COMSOL in Parallel on Clusters for more information.
From the Surrogate model training log list, choose Minimal, Normal (the default), or Detailed as desired.