Main Content

train

Train anomaly detector and obtain detection threshold

Since R2025a

Description

detector = train(detector,trainData) trains the deep learning network within an anomaly detector detector with a training data set trainData that contains only normal data, and not any anomalous data. This syntax uses default options.

When the ThresholdMethod property for detector is set to any method but "manual", train also obtains the detection threshold that the detector uses to identify an anomaly.

You can use train to train a newly created detector or continue the training of a previously trained detector.

For an example of using train as part of the detector development workflow, see Train and Test TCN Anomaly Detector.

detector = train(detector,trainData,Name=Value) specifies the training options explicitly, rather than through trainingOptions. The options you can set depend on the detector model you are training.

  • TcnDetector and DeepantDetector accept only a trainingOptions (Deep Learning Toolbox) option set that you configure separately.

  • UsadDetector accepts certain training options as individual name-value arguments.

  • VaelstmDetector accepts both a TrainingOptions set and individual name-value arguments.

Input Arguments

collapse all

All Detectors

Anomaly detector model, specified as a TcnDetector, DeepantDetector, UsadDetector, or VaelstmDetector object.

Training data set used to train the detector model within a detector object, specified as a matrix, a cell array, or a timetable. trainData must contain only normal data, and not any anomalous data.

When trainData contains m sets of signals, each of which contain n channels, the possible formats are these:

  • n-column matrix that consists of a single multichannel signal observation (m=1).

    A sufficiently long single observation can be as effective for training as multiple shorter observations.

  • Cell array with m cells that each contain a matrix that has NumChannels columns.

  • Timetable that contains a single multichannel signal observation, arranged in one of two possible ways.

    • The n channels are distributed in the columns of a matrix that the timetable contains in a single variable

    • The n channels are represented by n timetable variables that each contain a vector.

      In either case, the timetable must contain finite, increasing, and uniformly sampled time values.

Name-Value Arguments

expand all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: detectorUs = train(detectorUs,MaxEpochs=100) sets the value of MaxEpochs to 100 when training the UsadDetector model detectorUs.

Note: in the value summaries below, all default values correspond to the TrainingOptionsADAM (Deep Learning Toolbox) default values. If you choose an alternative solver, see TrainingOptionsRMSProp (Deep Learning Toolbox) or TrainingOptionsSGDM (Deep Learning Toolbox) for default values.

TCN, DeepAnT, and VAE-LSTM Detector Models only

expand all

Training options, specified as a TrainingOptionsADAM (Deep Learning Toolbox), TrainingOptionsRMSProp (Deep Learning Toolbox), or TrainingOptionsSGDM (Deep Learning Toolbox) object returned by the trainingOptions (Deep Learning Toolbox) function.

The detector uses the following options within the training options set. The default values depend on the solver you specify when you first create the option set.

  • Solver

  • MiniBatchSize

  • MaxEpochs

  • InitialLearnRate

To perform the modifications, specify them in the trainingOptions command. For example, to create a TrainingOptionsADAM option set and change the maximum number of epochs, MaxEpochs, from 30 to 100, use the following commands.

trainopts = trainingOptions("adam", MaxEpochs=100)
detectorTcn = train(detector_tcn,trainData,trainingOpts=trainopts)

If you do not specify trainingOpts, the software uses the default TrainingOptionsADAM (Deep Learning Toolbox) option set, but with the LearnRateSchedule option set to "piecewise".

USAD Detector Models Only

expand all

Size of the mini-batch to use for each training iteration, specified as a positive integer. A mini-batch is a subset of the training set that is used to evaluate the gradient of the loss function and update the weights. For more information, see the 'MiniBatchSize' description in trainingOptions (Deep Learning Toolbox)

Maximum number of epochs (full passes of the data) to use for training, specified as a positive integer.

Initial learning rate used for training, specified as a positive scalar.

If the learning rate is too low, then training can take a long time. If the learning rate is too high, then training might reach a suboptimal result or diverge.

Flag to display training progress information, specified as 1 (true) or 0 (false).

The content of the verbose output depends on the solver that you use. For more information, see the 'Verbose' description in trainingOptions (Deep Learning Toolbox).

Plots to display during detector model training, specified as one of these values:

  • "none" — Do not display plots during training.

  • "training-progress" — Plot training progress.

The contents of the plot depends on the solver that you use. For more information, see the 'Plots' description in trainingOptions (Deep Learning Toolbox).

VAE-LSTM Detector Models Only

expand all

Maximum number of epochs (full passes of the data) to use for VAE-model training, specified as a positive integer.

Initial learning rate used for VAE training, specified as a positive scalar.

If the learning rate is too low, then training can take a long time. If the learning rate is too high, then training might reach a suboptimal result or diverge.

Output Arguments

collapse all

Trained anomaly detector model, returned as a trained TcnDetector, DeepantDetector, UsadDetector, or VaelstmDetector object.

Extended Capabilities

expand all

Version History

Introduced in R2025a