Class HyperbolicNeuralNetwork<T>
- Namespace
- AiDotNet.NeuralNetworks
- Assembly
- AiDotNet.dll
Represents a Hyperbolic Neural Network for learning hierarchical representations in Poincare ball space.
public class HyperbolicNeuralNetwork<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations (typically float or double).
- Inheritance
-
HyperbolicNeuralNetwork<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
A Hyperbolic Neural Network operates in hyperbolic space (specifically the Poincare ball model) rather than Euclidean space. This allows it to naturally capture hierarchical and tree-like structures in data with lower distortion than traditional networks.
For Beginners: Hyperbolic neural networks are designed for data that has a natural hierarchy or tree-like structure. Examples include: - Taxonomies (e.g., animal kingdom classification) - Organizational hierarchies - Social networks with community structures - Knowledge graphs
In hyperbolic space, the "distance" near the center is smaller than near the edges, allowing hierarchies to be represented more efficiently than in regular flat space. Points near the center represent "root" concepts, while points near the edge represent more specific "leaf" concepts.
Constructors
HyperbolicNeuralNetwork(NeuralNetworkArchitecture<T>, double, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?, double)
Initializes a new instance of the HyperbolicNeuralNetwork class.
public HyperbolicNeuralNetwork(NeuralNetworkArchitecture<T> architecture, double curvature = -1, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, double maxGradNorm = 1)
Parameters
architectureNeuralNetworkArchitecture<T>The architecture defining the structure of the neural network.
curvaturedoubleThe curvature of hyperbolic space (default -1.0, must be negative).
optimizerIGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>The optimization algorithm to use for training. If null, Adam optimizer is used.
lossFunctionILossFunction<T>The loss function to use for training. If null, MSE is used.
maxGradNormdoubleThe maximum gradient norm for gradient clipping during training.
Remarks
The curvature parameter controls how "curved" the hyperbolic space is. More negative values mean stronger curvature, which can better capture deep hierarchies but may be harder to optimize. A curvature of -1.0 is a good default for most applications.
Properties
SupportsTraining
Indicates whether this network supports training.
public override bool SupportsTraining { get; }
Property Value
Methods
Backward(Tensor<T>)
Performs a backward pass through the network to calculate gradients.
public Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>The gradient of the loss with respect to the network's output.
Returns
- Tensor<T>
The gradient of the loss with respect to the network's input.
Remarks
The backward pass in hyperbolic space uses Riemannian gradients, which are automatically handled by the HyperbolicLinearLayer. This ensures that gradient updates respect the geometry of hyperbolic space.
CreateNewInstance()
Creates a new instance of the HyperbolicNeuralNetwork with the same configuration.
protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()
Returns
- IFullModel<T, Tensor<T>, Tensor<T>>
DeserializeNetworkSpecificData(BinaryReader)
Deserializes hyperbolic neural network-specific data from a binary reader.
protected override void DeserializeNetworkSpecificData(BinaryReader reader)
Parameters
readerBinaryReader
Exceptions
- InvalidOperationException
Thrown when deserialized curvature is not negative.
Forward(Tensor<T>)
Performs a forward pass through the network with the given input tensor.
public Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>The input tensor to process.
Returns
- Tensor<T>
The output tensor after processing through all layers.
GetModelMetadata()
Retrieves metadata about the hyperbolic neural network model.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetaData object containing information about the network.
InitializeLayers()
Initializes the layers of the hyperbolic neural network based on the provided architecture.
protected override void InitializeLayers()
IsValidInputLayer(ILayer<T>)
Determines if a layer can serve as a valid input layer for this network.
protected override bool IsValidInputLayer(ILayer<T> layer)
Parameters
layerILayer<T>
Returns
IsValidOutputLayer(ILayer<T>)
Determines if a layer can serve as a valid output layer for this network.
protected override bool IsValidOutputLayer(ILayer<T> layer)
Parameters
layerILayer<T>
Returns
Predict(Tensor<T>)
Makes a prediction using the hyperbolic neural network for the given input tensor.
public override Tensor<T> Predict(Tensor<T> input)
Parameters
inputTensor<T>The input tensor to make a prediction for.
Returns
- Tensor<T>
The predicted output tensor.
Remarks
Input points should be inside the Poincare ball (norm less than 1/sqrt(-curvature)). Points near the origin represent high-level concepts; points near the boundary represent more specific concepts.
SerializeNetworkSpecificData(BinaryWriter)
Serializes hyperbolic neural network-specific data to a binary writer.
protected override void SerializeNetworkSpecificData(BinaryWriter writer)
Parameters
writerBinaryWriter
Train(Tensor<T>, Tensor<T>)
Trains the hyperbolic neural network using the provided input and expected output.
public override void Train(Tensor<T> input, Tensor<T> expectedOutput)
Parameters
inputTensor<T>The input tensor for training.
expectedOutputTensor<T>The expected output tensor for the given input.
Remarks
Training uses Riemannian gradient descent, where parameter updates follow geodesics (shortest paths) in hyperbolic space rather than straight lines.
UpdateParameters(Vector<T>)
Updates the parameters of all layers in the network.
public override void UpdateParameters(Vector<T> parameters)
Parameters
parametersVector<T>A vector containing all parameters for the network.