ConditionalRandomFieldLayer<T>

Layers · AiDotNet.NeuralNetworks.Layers

Represents a Conditional Random Field (CRF) layer for sequence labeling tasks.

Inheritance: LayerBase<T> → ConditionalRandomFieldLayer<T>

Implements: IShapeContract

For Beginners

A Conditional Random Field (CRF) layer is used when you need to label each item in a sequence while considering how labels relate to each other.

In many sequence tasks, the label for an item depends not just on the item itself, but also on nearby items:

For example, in a sentence like “John Smith lives in New York”:

  • Without CRF: Each word might be labeled independently, potentially creating invalid sequences
  • With CRF: The model considers that “New” followed by “York” is likely a location name

Think of it like:

  • Standard layers ask, “What’s the best label for this word on its own?”
  • CRF layers ask, “What’s the best sequence of labels for the whole sentence?”

CRFs are especially useful for tasks like:

  • Named entity recognition (finding names of people, organizations, locations)
  • Part-of-speech tagging (labeling words as nouns, verbs, etc.)
  • Any task where the correct labels form patterns or follow rules

How It Works

A Conditional Random Field (CRF) layer is a specialized neural network layer designed for sequence labeling tasks such as named entity recognition, part-of-speech tagging, and activity recognition. Unlike standard classification layers that make independent predictions for each element in a sequence, CRF layers model the dependencies between labels in a sequence, leading to more coherent predictions. The layer uses the Viterbi algorithm to find the most likely sequence of labels given the input features and learned transition probabilities between labels.

Constructors

Constructor Summary
ConditionalRandomFieldLayer(int numClasses, IActivationFunction<T> scalarActivation) Lazy constructor with vector activation — resolves sequenceLength from input.Shape[0] on first Forward.
ConditionalRandomFieldLayer(int numClasses, IVectorActivationFunction<T> vectorActivation) Lazy constructor with vector activation — resolves sequenceLength from input.Shape[0] on first Forward.
ConditionalRandomFieldLayer(int numClasses, int sequenceLength, IActivationFunction<T> scalarActivation) Initializes a new instance of the ConditionalRandomFieldLayer class with a vector activation function.
ConditionalRandomFieldLayer(int numClasses, int sequenceLength, IVectorActivationFunction<T> vectorActivation) Initializes a new instance of the ConditionalRandomFieldLayer class with a vector activation function.

Properties

Property Summary
bool SupportsTraining { get; }

Methods

Method Summary
Dictionary<string, string> GetParameterRoles() Returns parameter roles for per-role optimizer configuration (e.g., weight decay exemption for biases).
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank) Derived from this type’s [TensorLayout] declarations.
IReadOnlyList<Tensor<T>> GetTrainableParameters() Returns all trainable parameter tensors marked with [TrainableParameter].
void ClearGradients()
void ResetState() Resets the internal state of the CRF layer.
void SetDecodeTagConstraints(IReadOnlyList<string> labelNames) Derives hard decode-time constraints on the label topology from the label set and applies them to Viterbi decoding, so the layer can never emit a structurally invalid sequence (an orphan I- tag, a sequence starting on I-, an I-X conti…
void SetTrainableParameters(IReadOnlyList<Tensor<T>> parameters) Replaces trainable parameter tensors (e.g., with ParameterBuffer views).
void UpdateParameters(T learningRate) Updates the layer’s parameters using the calculated gradients.
void ZeroGrad() Clears all gradient fields discovered by convention ({paramName}Gradient).