nnopinf.operators.SpdOperator#
- class nnopinf.operators.SpdOperator(acts_on, depends_on, n_hidden_layers, n_neurons_per_layer, activation=<built-in method tanh of type object>, siren_omega_0=30.0, siren_first_layer=False, fourier_features=False, fourier_variables=None, fourier_num_frequencies=6, fourier_base=2.0, fourier_scale=1.0, fourier_frequencies=None, positive=True, name='SpdOperator', parameterization='cholesky', residual=True, layer_norm=True)[source]#
Bases:
Module\(f: (v,x) \mapsto L(v)L(v)^T x\)
Constructs an SPD (or NPD) operator \(f: (v,x) \mapsto L(v)L(v)^Tx = A(v)x\) such that \(x^T A(v) x >= 0\)
- Parameters:
acts_on (nnopinf.Variable) – The state the operators acts on, i.e., the
xinA(v) xdepends_on (tuple of nnopinf.Variable) – The variables the operator depends on, i.e., the
vinA(v) xn_hidden_layers (int) – Number of hidden layers in the network
n_neurons_per_layer (int) – Number of nuerons in each hidden layer
activation (PyTorch activation function (e.g., torch.nn.functional.relu)) – Activation function used at each layer
siren_omega_0 (float) – Frequency scale for SIREN activations.
siren_first_layer (bool) – If True, use a SIREN-style first layer even for non-sine activations.
fourier_features (bool) – If True, augment inputs with Fourier features.
fourier_variables (iterable of str, optional) – Variable names to augment with Fourier features. Defaults to all inputs.
fourier_num_frequencies (int) – Number of frequencies per variable when using Fourier features.
fourier_base (float) – Base for geometric progression of Fourier frequencies.
fourier_scale (float) – Scaling applied to Fourier frequencies.
fourier_frequencies (array-like, optional) – Explicit list of Fourier frequencies. Overrides
fourier_num_frequencies.positive (bool) – If operator is SPD or NPD
parameterization (str) – SPD parameterization. Supported values are
"cholesky"(default, uses \(L L^T\)) and"matrix_exp"(uses \(\exp(S)\) with symmetric \(S\)).residual (bool) – If True, use residual connections between hidden layers when shapes match.
layer_norm (bool) – If True, apply LayerNorm after each hidden linear layer.
name (string) – Operator name. Used when saving to file
Examples
>>> import nnopinf >>> import nnopinf.operators >>> x_input = nnopinf.Variable(size=3,name="x") >>> mu_input = nnopinf.Variable(size=2,name="mu") >>> NpdMlp = nnopinf.operators.SpdOperator(acts_on=x_input,depends_on=(x_input,mu_input,),n_hidden_layers=2,n_neurons_per_layer=2,positive=False)
- forward(inputs, return_jacobian=False)[source]#
Forward pass of operator
- Parameters:
inputs (dict(str, np.array)) – Dictionary of input data in the form of arrays referenced by the variable name, i.e., inputs[‘x’] = np.ones(3)
return_jacobian (bool, optional) – If True, return the (approximate) Jacobian in addition to the output.
Examples
>>> import nnopinf >>> import nnopinf.operators >>> import numpy as np >>> x_input = nnopinf.Variable(size=3,name="x") >>> mu_input = nnopinf.Variable(size=2,name="mu") >>> NpdMlp = nnopinf.operators.SpdOperator(acts_on=x_input,depends_on=(x_input,mu_input,),n_hidden_layers=2,n_neurons_per_layer=2,positive=False) >>> inputs = {} >>> inputs['x'] = np.random.normal(3) >>> inputs['mu'] = np.random.normal(2) >>> Av,A = NpdMlp.forward(inputs,True)