openasce.discovery.regression_discovery package

Submodules

openasce.discovery.regression_discovery.lbfgsb_optimizer module

class openasce.discovery.regression_discovery.lbfgsb_optimizer.LBFGSBOptimizer(params)[source]

Bases: Optimizer

Wrap L-BFGS-B algorithm, using scipy routines.

__doc__ = 'Wrap L-BFGS-B algorithm, using scipy routines.'
__init__(params)[source]
__module__ = 'openasce.discovery.regression_discovery.lbfgsb_optimizer'
_distribute_flat_params(params)[source]
_gather_flat_bounds()[source]
_gather_flat_grad()[source]
_gather_flat_params()[source]
step(closure)[source]

Performs a single optimization step.

Parameters

closure (callable) – A closure that reevaluates the model and returns the loss.

openasce.discovery.regression_discovery.locally_connected module

class openasce.discovery.regression_discovery.locally_connected.LocallyConnected(num_linear, input_features, output_features, bias=True)[source]

Bases: Module

Local linear layer

Argument:

num_linear: num of local linear layers, i.e. in_features: m1 out_features: m2 bias: whether to include bias or not

Shape:
  • Input: [n, d, m1]

  • Output: [n, d, m2]

weight

[d, m1, m2]

bias

[d, m2]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

__annotations__ = {}
__doc__ = 'Local linear layer\n\n    Argument:\n        num_linear: num of local linear layers, i.e.\n        in_features: m1\n        out_features: m2\n        bias: whether to include bias or not\n\n    Shape:\n        - Input: [n, d, m1]\n        - Output: [n, d, m2]\n\n    Attributes:\n        weight: [d, m1, m2]\n        bias: [d, m2]\n    '
__init__(num_linear, input_features, output_features, bias=True)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

__module__ = 'openasce.discovery.regression_discovery.locally_connected'
forward(input: Tensor)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reset_parameters()[source]

openasce.discovery.regression_discovery.notears_mlp module

class openasce.discovery.regression_discovery.notears_mlp.NotearsMLP(dims, bias=True)[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

__annotations__ = {}
__doc__ = None
__init__(dims, bias=True)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

__module__ = 'openasce.discovery.regression_discovery.notears_mlp'
_bounds()[source]
fc1_l1_reg()[source]

Take l1 norm of fc1 weight

fc1_to_adj() ndarray[source]

Get W from fc1 weights, take 2-norm over m1 dim

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

h_func()[source]

Constrain 2-norm-squared of fc1 weights along m1 dim to be a DAG

l2_reg()[source]

Take 2-norm-squared of all parameters

openasce.discovery.regression_discovery.regression_discovery module

class openasce.discovery.regression_discovery.regression_discovery.CausalRegressionDiscovery[source]

Bases: Discovery

Execute the causal discovery by notears method

Attributes:

Constructor

Arguments:

Returns:

__annotations__ = {}
__doc__ = 'Execute the causal discovery by notears method\n\n    Attributes:\n\n    '
__init__() None[source]

Constructor

Arguments:

Returns:

__module__ = 'openasce.discovery.regression_discovery.regression_discovery'
fit(*, X: Union[ndarray, Callable], **kwargs)[source]

Feed the sample data

Parameters

X (num of samples, features or callable returning np.ndarray) – samples

Returns:

get_result() Tuple[CausalGraph, float][source]

Get the causal graph sample data

Parameters

X (num of samples, features or callable returning np.ndarray) – samples

Returns:

openasce.discovery.regression_discovery.regression_discovery_test module

openasce.discovery.regression_discovery.regression_strategy module

class openasce.discovery.regression_discovery.regression_strategy.Strategy(node_names: List[str], **kwargs)[source]

Bases: object

General class to implement different structure learning methods

Attributes

Contructor

Parameters

node_names – the name of nodes

__dict__ = mappingproxy({'__module__': 'openasce.discovery.regression_discovery.regression_strategy', '__doc__': 'General class to implement different structure learning methods\n\n    Attributes\n\n    ', '__init__': <function Strategy.__init__>, 'run': <function Strategy.run>, 'dual_ascent': <function Strategy.dual_ascent>, '__dict__': <attribute '__dict__' of 'Strategy' objects>, '__weakref__': <attribute '__weakref__' of 'Strategy' objects>, '__annotations__': {}})
__doc__ = 'General class to implement different structure learning methods\n\n    Attributes\n\n    '
__init__(node_names: List[str], **kwargs)[source]

Contructor

Parameters

node_names – the name of nodes

__module__ = 'openasce.discovery.regression_discovery.regression_strategy'
__weakref__

list of weak references to the object (if defined)

dual_ascent(model, X, lambda1, lambda2, rho_max, rho, alpha, h)[source]

Perform one step of dual ascent in augmented Lagrangian.

run(*, model: Module, data: ndarray, max_iteration: int = 3, lambda1: float = 0.1, lambda2: float = 0.1, h_tol: float = 1e-08, rho_max: float = 1e+16, w_threshold: float = 0.3, **kwargs) Tuple[source]

Run the actual strategy

Parameters
  • model – the model used to discover the better graph

  • data – the features of samples

  • **kwargs (dict) – dictionnary with method specific args

Returns:

openasce.discovery.regression_discovery.regression_strategy.squared_loss(output, target)[source]

openasce.discovery.regression_discovery.trace_expm module

class openasce.discovery.regression_discovery.trace_expm.TraceExpm(*args, **kwargs)[source]

Bases: Function

__doc__ = None
__module__ = 'openasce.discovery.regression_discovery.trace_expm'
_backward_cls

alias of TraceExpmBackward

static backward(ctx, grad_output)[source]

Defines a formula for differentiating the operation with backward mode automatic differentiation (alias to the vjp function).

This function is to be overridden by all subclasses.

It must accept a context ctx as the first argument, followed by as many outputs as the forward() returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs to forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.

The context can be used to retrieve tensors saved during the forward pass. It also has an attribute ctx.needs_input_grad as a tuple of booleans representing whether each input needs gradient. E.g., backward() will have ctx.needs_input_grad[0] = True if the first input to forward() needs gradient computated w.r.t. the output.

static forward(ctx, input)[source]

This function is to be overridden by all subclasses. There are two ways to define forward:

Usage 1 (Combined forward and ctx):

@staticmethod
def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any:
    pass
  • It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).

  • See combining-forward-context for more details

Usage 2 (Separate forward and ctx):

@staticmethod
def forward(*args: Any, **kwargs: Any) -> Any:
    pass

@staticmethod
def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None:
    pass
  • The forward no longer accepts a ctx argument.

  • Instead, you must also override the torch.autograd.Function.setup_context() staticmethod to handle setting up the ctx object. output is the output of the forward, inputs are a Tuple of inputs to the forward.

  • See extending-autograd for more details

The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with ctx.save_for_backward() if they are intended to be used in backward (equivalently, vjp) or ctx.save_for_forward() if they are intended to be used for in jvp.