Class AnomalyDetectorLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Represents a layer that detects anomalies by comparing predictions with actual inputs.
public class AnomalyDetectorLayer<T> : LayerBase<T>, ILayer<T>, IJitCompilable<T>, IDiagnosticsProvider, IWeightLoadable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
LayerBase<T>AnomalyDetectorLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
The AnomalyDetectionLayer compares the predicted state with the actual state to calculate an anomaly score. This score represents how unexpected or surprising the current input is, given the model's predictions. Higher scores indicate more anomalous (unexpected) inputs.
For Beginners: This layer identifies patterns that don't match what the network expected.
Think of anomaly detection like this:
- The network learns what "normal" looks like from the data
- This layer compares new inputs to what the network expected to see
- If the actual input is very different from the prediction, it's flagged as anomalous
- The output is an "anomaly score" between 0 and 1 (higher means more unusual)
For example, in monitoring network traffic, the system might learn normal patterns and then use this layer to alert when unusual activity is detected that might indicate a security breach.
Constructors
AnomalyDetectorLayer(int, double, int, double, IEngine?)
Initializes a new instance of the AnomalyDetectorLayer<T> class.
public AnomalyDetectorLayer(int inputSize, double anomalyThreshold, int historyCapacity = 100, double smoothingFactor = 0.1, IEngine? engine = null)
Parameters
inputSizeintThe size of the input vector.
anomalyThresholddoubleThe threshold for determining anomalous inputs.
historyCapacityintThe maximum number of anomaly scores to keep in history. Default is 100.
smoothingFactordoubleThe smoothing factor for the exponential moving average. Default is 0.1.
engineIEngine
Remarks
This constructor creates an anomaly detection layer with the specified parameters. The layer will calculate anomaly scores by comparing predicted states with actual states.
For Beginners: This creates a new anomaly detection layer with your settings.
The parameters you provide determine:
- inputSize: How many values the layer expects in its input vector
- anomalyThreshold: The cutoff point for flagging anomalies (0-1)
- historyCapacity: How many recent scores to remember
- smoothingFactor: How quickly to respond to changes (0-1)
These settings let you customize how sensitive the anomaly detection is and how it adapts to changing patterns over time.
Properties
SupportsGpuExecution
Gets a value indicating whether this layer supports GPU execution.
protected override bool SupportsGpuExecution { get; }
Property Value
SupportsJitCompilation
Gets a value indicating whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
- bool
Always
true. AnomalyDetector uses differentiable reconstruction error.
Remarks
JIT compilation for AnomalyDetector computes the anomaly score as the reconstruction error (mean squared error between input and reconstruction). This enables training of anomaly detection models with gradient descent. The stateful historical tracking is not used in JIT mode.
SupportsTraining
The computation engine (CPU or GPU) for vectorized operations.
public override bool SupportsTraining { get; }
Property Value
- bool
falsefor this layer, as it doesn't have trainable parameters.
Remarks
This property indicates whether the anomaly detection layer has trainable parameters. Since this layer simply calculates an anomaly score based on the input and doesn't have weights or biases to update during training, it returns false.
For Beginners: This property tells you if the layer needs training.
A value of false means:
- The layer doesn't have weights or biases that need to be learned
- It performs calculations using fixed algorithms rather than learned parameters
- It operates based on the statistics of the data it sees
This layer works automatically without needing a training phase.
Methods
Backward(Tensor<T>)
Performs the backward pass of the anomaly detection layer.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>The gradient of the loss with respect to the layer's output.
Returns
- Tensor<T>
The gradient of the loss with respect to the layer's input.
Remarks
This method implements the backward pass of the anomaly detection layer, which is used during training to propagate error gradients back through the network. Since the anomaly detection layer doesn't have trainable parameters, it simply passes the gradient through to the previous layer.
For Beginners: This method passes error information backward during training.
The backward pass:
- Takes an error gradient from the next layer
- Propagates it back to the previous layer
- Doesn't modify any parameters since this layer doesn't learn
This method exists to maintain compatibility with the neural network backpropagation mechanism, but it doesn't do much in this layer since there are no weights to adjust.
ExportComputationGraph(List<ComputationNode<T>>)
Exports the layer's computation graph for JIT compilation.
public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)
Parameters
inputNodesList<ComputationNode<T>>List to populate with input computation nodes.
Returns
- ComputationNode<T>
The output computation node representing the layer's operation.
Remarks
This method constructs a computation graph representation of the layer's forward pass that can be JIT compiled for faster inference. All layers MUST implement this method to support JIT compilation.
For Beginners: JIT (Just-In-Time) compilation converts the layer's operations into optimized native code for 5-10x faster inference.
To support JIT compilation, a layer must:
- Implement this method to export its computation graph
- Set SupportsJitCompilation to true
- Use ComputationNode and TensorOperations to build the graph
All layers are required to implement this method, even if they set SupportsJitCompilation = false.
Forward(Tensor<T>)
Performs the forward pass of the anomaly detection layer.
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>The input tensor containing both predicted and actual states.
Returns
- Tensor<T>
A tensor containing the anomaly score.
Remarks
This method implements the forward pass of the anomaly detection layer. It calculates an anomaly score by comparing the predicted states with the actual states. The method assumes that the first half of the input represents actual states and the second half represents predicted states.
For Beginners: This method calculates how unusual the current input is.
The forward pass:
- Takes an input that contains both actual values and predicted values
- Compares them to see how different they are
- Calculates an anomaly score based on this difference
- Updates the smoothed score and history
- Returns the anomaly score and whether it exceeds the threshold
The output is a tensor with just one value: the anomaly score between 0 and 1.
ForwardGpu(params IGpuTensor<T>[])
Performs the forward pass using GPU acceleration.
public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)
Parameters
inputsIGpuTensor<T>[]The input GPU tensor containing both predicted and actual states.
Returns
- IGpuTensor<T>
A GPU tensor containing the anomaly score.
GetAnomalyScore()
Gets the current anomaly score.
public double GetAnomalyScore()
Returns
- double
The current smoothed anomaly score.
Remarks
This method returns the current smoothed anomaly score, which represents how unexpected or surprising the most recent input was.
For Beginners: This method tells you how unusual the current input is.
The anomaly score:
- Is a value between 0 and 1
- Higher values mean more unusual inputs
- Is smoothed to reduce the impact of momentary spikes
This score can be used for monitoring, visualization, or custom anomaly detection logic beyond simple thresholding.
GetAnomalyStatistics()
Gets the statistical properties of recent anomaly scores.
public Dictionary<string, double> GetAnomalyStatistics()
Returns
- Dictionary<string, double>
A dictionary containing statistical properties (mean, standard deviation, min, max).
Remarks
This method calculates and returns statistical properties of the recent anomaly scores stored in the history. These statistics can be useful for adaptive thresholding or monitoring trends in anomaly patterns.
For Beginners: This method provides statistics about recent anomaly scores.
The statistics include:
- Mean (average) of recent anomaly scores
- Standard deviation (measure of variability)
- Minimum and maximum scores
- Current score and whether it's an anomaly
These statistics help understand the pattern of anomalies over time, which can be useful for adjusting thresholds or analyzing trends.
GetParameters()
Gets all parameters of the layer as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
An empty vector as this layer has no trainable parameters.
Remarks
This method returns an empty vector since the anomaly detection layer doesn't have trainable parameters.
For Beginners: This method returns an empty list since this layer doesn't learn parameters.
Since this layer:
- Doesn't have weights or biases
- Uses fixed formulas rather than learned parameters
- Doesn't require training in the traditional sense
The method returns an empty vector to maintain compatibility with the layer interface.
IsAnomaly()
Determines if the current input is anomalous based on the anomaly score.
public bool IsAnomaly()
Returns
- bool
True if the input is anomalous; otherwise, false.
Remarks
This method determines whether the current input should be considered anomalous based on the smoothed anomaly score and the anomaly threshold.
For Beginners: This method decides if the current input is unusual enough to flag.
The anomaly decision:
- Compares the smoothed anomaly score to the threshold
- Returns true if the score is above the threshold
- Returns false if the score is below the threshold
This binary decision can be used to trigger alerts or special handling for unusual inputs that might indicate problems or interesting events.
ResetState()
Resets the internal state of the layer.
public override void ResetState()
Remarks
This method resets the internal state of the anomaly detection layer by clearing the anomaly history and resetting the smoothed anomaly score to zero.
For Beginners: This method clears the layer's memory to start fresh.
When resetting the state:
- The history of anomaly scores is cleared
- The smoothed anomaly score is set to zero
- The layer forgets all past anomaly patterns
This is useful when:
- Processing a new, unrelated sequence
- Adapting to a significant change in data patterns
- Testing the layer with fresh inputs
UpdateParameters(T)
Updates the parameters of the layer.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTThe learning rate for parameter updates.
Remarks
This method is empty in the current implementation as the layer does not have trainable parameters updated through gradient descent.
For Beginners: This method is included for compatibility but doesn't do anything in this layer.
The reason this method is empty:
- This layer doesn't have weights or biases to update
- It performs calculations based on fixed formulas rather than learned parameters
- This method is included only to satisfy the requirements of the LayerBase class