TabRNetwork<T>
Models & Types · AiDotNet.NeuralNetworks.Tabular
TabR (Retrieval-Augmented) neural network for tabular data.
Inheritance: NeuralNetworkBase<T> → TabularNeuralNetworkBase<T> → TabRNetwork<T>
For Beginners
TabR is like a student who looks at similar past problems:
Architecture:
- Feature Encoder: Convert features to learned embeddings
- Retrieval: Find K most similar training samples
- Context Encoder: Aggregate neighbor information with attention
- Prediction Head: Combine query and context for prediction
Key insight: Tabular data often has local structure - similar inputs tend to have similar outputs. TabR explicitly uses this by retrieving neighbors and letting the model see both the query and similar training examples.
This combines the strengths of neural networks (learning features) with k-nearest-neighbors (using local structure), often achieving SOTA results.
How It Works
TabR combines neural networks with instance-based learning by retrieving similar training examples and using their information to help make predictions. It encodes both the query sample and retrieved neighbors, then aggregates the information using attention. This implementation follows the standard neural network architecture pattern with:
- Proper inheritance from NeuralNetworkBase
- Layer-based architecture using ILayer components
- Engine-based tensor operations for CPU/GPU acceleration
- Full autodiff and JIT compilation support
Reference: “TabR: Tabular Deep Learning Meets Nearest Neighbors” (2023)
Example
var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 4);
var options = new TabROptions<double> { NumFeatures = 20, NumNeighbors = 96 };
var input = Tensor<float>.CreateRandom(new[] { 1, 20 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
.ConfigureModel(new TabRNetwork<float>(architecture))
.Build(trainX, trainY);
var output = result.Predict(input);
Constructors
| Constructor | Summary |
|---|---|
TabRNetwork() |
Initializes a new TabR network with the specified architecture. |
TabRNetwork(NeuralNetworkArchitecture<T> architecture, TabROptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Properties
| Property | Summary |
|---|---|
TabROptions<T> Options { get; } |
Gets the TabR-specific options. |
int EmbeddingDimension { get; } |
Gets the embedding dimension. |
int NumLayers { get; } |
Gets the number of MLP layers. |
int NumNeighbors { get; } |
Gets the number of neighbors to retrieve. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> GetFeatureImportance() |
|
ModelMetadata<T> GetModelMetadata() |