vnn

package
v0.28.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package vnn implements the Vector Neuron Networks operators described in https://arxiv.org/abs/2104.12229

It operates on 3D vectors -- so most tensors will has the last axis with dimension 3.

The operators are equivariant to rotations: that means that for every F, Rotate(F(x)) = F(Rotate(x)).

Index

Constants

View Source
const (
	// ParamReluShareNonLinearity hyperparameter provides the default value for ReluConfig.ShareNonLinearity().
	// It defaults to false.
	ParamReluShareNonLinearity = "vnn_relu_share_non_linearity"

	// ParamReluNegativeSlope hyperparameter provides the default value for the Relu negative slope (the "leak").
	// It defaults to 0.2.
	ParamReluNegativeSlope = "vnn_relu_negative_slope"
)
View Source
const (
	// ParamNumHiddenLayers is the hyperparameter that defines the default number of hidden layers.
	// The default is 0 (int), so no hidden layers.
	ParamNumHiddenLayers = "vnn_num_hidden_layers"

	// ParamNumHiddenNodes is the hyperparameter that defines the default number of hidden nodes for KAN hidden layers.
	// The default is 10 (int).
	ParamNumHiddenNodes = "vnn_num_hidden_nodes"

	// ParamResidual is the hyperparameter that defines whether to use residual connections between consecutive hidden layers.
	// If set, and the feature dimension (the last one) is the same between the operand, it also adds a residual to the
	// operand. Same with the outputDimensions.
	// Default is false (bool).
	ParamResidual = "vnn_residual"

	// ParamNormalization is the name of the normalization to use in between layers.
	// Only those available in vnn package (they have to be SO(3) equivariant, see paper).
	// It is only applied if there are hidden layers.
	//
	// It defaults to the parameter "normalization" (layers.ParamNormalization) and if that is not set, to "none"
	// (same as ""), which is no normalization.
	ParamNormalization = "vnn_normalization"

	// ParamDropoutRate is the name of the dropout rate to use in between layers.
	// It is only applied if there are hidden layers.
	//
	// Defaults to the parameter "dropout_rate" (layers.ParamDropoutRate) and if that is not set, to 0.0 (no dropout)
	ParamDropoutRate = "vnn_dropout_rate"

	// ParamActivation is the name of the activation to use in between layers.
	// It can be "relu" (default), "none" or "" -- the last two mean no activation.
	// Notice "relu" refers to vnn.Relu, which is a rotation-equivariant activation.
	ParamActivation = "vnn_activation"

	// ParamScaler hyperparameter sets whether to use a learnable scaler on the output.
	//
	// It learns two multipliers α and β such that for a 3D vector x: scaler(x) = αx + β(x/Sqrt(L2(x)^2+epsilon)).
	//
	// So if α=0 and β=1, it normalizes x to a unit length.
	ParamScaler = "vnn_scaler"
)

Variables

This section is empty.

Functions

func DropoutNormalize

func DropoutNormalize(scope *model.Scope, operand *Node, dropoutRate *Node, normalize bool) *Node

DropoutNormalize randomly replace the operand with zeros if scope.IsTraining() is true. Otherwise, it's a no op (it returns operand). If normalize is set, it scales the output by 1/(1-dropoutRate) to preserve the mean of the operand values.

It expects the operand to have shape [..., inputFeatures, 3], and the dropout happens on the inputFeatures.

func InvariantDotProduct added in v0.20.1

func InvariantDotProduct(v0, v1 *Node) *Node

InvariantDotProduct does a dot product on the last axis (presumably of dimension 3) of two VNN projections (rotation invariant) from the same tensor of 3d vectors.

func LayerNormalization

func LayerNormalization(operand *Node, epsilon float64) *Node

LayerNormalization perform an equivariant (SO(3)) layer normalization on vectors.

The value epsilon is used in the denominator, and a value of 1e-5 is a good default.

func ReluFromScope added in v0.28.0

func ReluFromScope(scope *model.Scope, operand *Node) *Node

ReluFromScope applies Relu with the default parameters used from the model.

func RotateOnOrigin

func RotateOnOrigin(x, roll, pitch, yaw *Node) *Node

RotateOnOrigin 3D elements arbitrarily around the 3 axes.

It requires that the last axis of x to be of dimension 3. Typically, x will be shaped [batchSize, N, 3].

The rotation angles roll, pitch and yaw (rotations on the axes x, y and z respectively) must either be nil (no rotation on that axis) or a scalar.

TODO: allow the angles to be shaped like x, and have one rotation per 3d coordinates.

Types

type ActivationFn added in v0.20.1

type ActivationFn func(scope *model.Scope, operand *Node) *Node

ActivationFn applies a non-linearity to the operand.

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config is created with New and can be configured with its methods or simply setting the corresponding hyperparameters in the model.

func New

func New(scope *model.Scope, operand *Node, outputChannels int) *Config

New creates a configuration for a VNN (Vector Neural Network), which is SO(3) invariant, which means that a rotation of the operand will yield the same rotation on the output.

It is initialized with good defaults and with hyperparameters from the given Scope. It can then be further configured with its various methods.

Once configured, call Config.Done to add the VNN computation graph and get the output.

The operand is expected to have the shape `[batchSize, ..., inputFeatures, 3]`, and the returned output will have the shape `[batchSize, ..., outputChannels, 3]`.

E.g.: A VNN for a multi-class classification model with NumClasses classes, rotation invariant.

func MyModel(scope *model.Scope, inputs []*Node) (outputs []*Node) {
	pointCloud := inputs[0]  // [batchSize, numPoints, 3]
	scope = scope.In("model")
	base := vnn.New(scope.In("base"), pointCloud, 128).Done()  // [batchSize, 128, 3]
	V := vnn.New(scope.In("V"), base, NumClasses).NumHiddenLayers(0, 0)  // [batchSize, NumClasses, 3]
	T := vnn.New(scope.In("T"), base, NumClasses).NumHiddenLayers(0, 0)  // [batchSize, NumClasses, 3]
	logits := InvariantDotProduct(V, T)  // [batchSize, NumClasses]
	return []*Node{logits}
}

func (*Config) Activation

func (c *Config) Activation(activation string) *Config

Activation sets the activation for the VNN, in between each layer. The operand and output layers don't get an activation layer.

The default and currently the only rotation-equivariant activation defined is "relu", if not defined as a hyperparameter.

Other valid values are "" or "none" for no activation function.

See also ActivationFn.

func (*Config) ActivationFn added in v0.20.1

func (c *Config) ActivationFn(fn ActivationFn) *Config

ActivationFn is an alternative way to set the activation, by providing an activation function. It takes precedence over the one set by Config.Activation.

func (*Config) Done

func (c *Config) Done() *Node

Done takes the configuration and applies the VNN as configured.

func (*Config) Dropout

func (c *Config) Dropout(ratio float64) *Config

Dropout sets the dropout ratio for the VNN, in between each layer. The output layer doesn't get dropout. It uses the normalized form of dropout (see layers.DropoutNormalize).

If set to 0.0, no dropout is used.

The default is 0.0, but it can be overridden by setting the hyperparameter layers.ParamDropoutRate (="dropout_rate") in the model.

func (*Config) Normalization

func (c *Config) Normalization(normalization string) *Config

Normalization sets the normalization type to use in between layers. The operand and output layers don't get a normalization layer.

The default is "none", but it can be overridden by setting the hyperparameter ParamNormalization (="vnn_normalization") in the model.

func (*Config) NumHiddenLayers

func (c *Config) NumHiddenLayers(numLayers, numHiddenNodes int) *Config

NumHiddenLayers configure the number of hidden layers between the operand and the output. Each layer will have numHiddenNodes nodes.

The default is 0 (no hidden layers), but it will be overridden if the hyperparameter ParamNumHiddenLayers is set in the scope (scope). The value for numHiddenNodes can also be configured with the hyperparameter ParamNumHiddenNodes.

func (*Config) Regularizer

func (c *Config) Regularizer(regularizer regularizer.Regularizer) *Config

Regularizer to be applied to the learned weights. Default is none.

To use more than one type of Regularizer, use regularizers.Combine, and set the returned combined regularizer here.

The default is regularizers.FromScope, which is configured by regularizers.ParamL1 and regularizers.ParamL2.

func (*Config) Residual

func (c *Config) Residual(useResidual bool) *Config

Residual configures if residual connections in between layers with the same number of nodes should be used. They are very useful for deep models.

The default is false, and it may be configured with the hyperparameter ParamResidual.

func (*Config) Scaler added in v0.20.1

func (c *Config) Scaler(enabled bool) *Config

Scaler sets whether to use a learnable scaler on the output.

It learns two multipliers α and β such that for a 3D vector x: scaler(x) = αx + β(x/Sqrt(L2(x)^2+epsilon)).

So if α=0 and β=1, it normalizes x to a unit length.

type ReluConfig added in v0.20.1

type ReluConfig struct {
	// contains filtered or unexported fields
}

func Relu

func Relu(scope *model.Scope, operand *Node) *ReluConfig

Relu perform an equivariant to rotation (SO(3)) relu. It involves learning a projection (also equivariant), hence it needs the scope to store the variable.

The Relu forces the initializer of the variable to be random: a zero value here would lead to NaN.

This returns a configuration object. Call ReluConfig.Done when finished and it will compute the Relu of the operand as specified.

func (*ReluConfig) Done added in v0.20.1

func (c *ReluConfig) Done() *Node

Done applies the Relu layer as configured.

func (*ReluConfig) NegativeSlope added in v0.20.1

func (c *ReluConfig) NegativeSlope(negativeSlope float64) *ReluConfig

NegativeSlope sets the amount of "leakage" to allow on the negative side of the Relu non-linearity. Any value <= 0 implies no leakage.

Values should be < 1, the default is 0.2.

func (*ReluConfig) ShareNonLinearity added in v0.20.1

func (c *ReluConfig) ShareNonLinearity(enabled bool) *ReluConfig

ShareNonLinearity makes the relu to share the direction of the non-linearity to all features.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL