capsa_tf.sculpt

class capsa_tf.sculpt.Wrapper
__init__(distribution=<class 'capsa_tf.sculpt.distribution.Normal'>, n_layers=3, inline=False)
Parameters:
  • distribution (type[Distribution]) – The type of distribution that the model’s outputs should be set to. (default: <class 'capsa_tf.sculpt.distribution.Normal'>)

  • n_layers (int) – The number of model output layers that should be modified to produce standard deviation outputs. (default: 3)

  • inline (bool) – Whether to inline inner functions. Defaults to inline=False which disallows inner functions. (default: False)

Note

n_layers here only weakly approximates the layers of a neural network.

__call__(fn)

Applies wrapper to tf.function decorated function.

Parameters:

fn (Callable) – The function to wrap

Return type:

WrappedFunction

Returns:

The wrapped function

Example Usage

Wrapping a tf.function
from capsa_tf import sample # or sculpt, vote
wrapper = sample.Wrapper(n_samples=3) # Initialize a wrapper object with your config options

def forward_pass(x):
    ...
    return y

wrapped_forward_pass = wrapper(forward_pass) # wrap your function

y = wrapped_forward_pass(x) # Use the wrapped function as usual
y, risk = wrapped_forward_pass(x, return_risk=True) # Use the wrapped function to obtain risk values
Decorator approach
from capsa_tf import sample # or sculpt, vote

@sample.Wrapper(n_samples=3) # Initialize a wrapper object with your config options
def forward_pass(x):
    ...
    return y

y = forward_pass(x) # Use the wrapped function as usual
y = forward_pass(x,return_risk=True) # Use the wrapped function to obtain risk values
class capsa_tf.sculpt.Normal

Normal Distribution for the Sculpt Wrapper

static loss_function(target, pred, risk, reduction='mean')

Compute the distribution loss from target, pred, and risk values.

Parameters:
  • target (Tensor) – The target value for the model output

  • pred (Tensor) – The predicted output distribution mean value

  • risk (Tensor) – The predicted output distribution sigma value

  • reduction (str) – The reduction approach to use on the output. Options include: [“mean”, “sum”, “none”]. (default: 'mean')

Return type:

Tensor

Returns:

The computed loss value

static sample(risk_out)

Samples from a normal distribution with mean mu and standard deviation sigma

Parameters:

risk_out (RiskOutput) – The output from calling the wrapped model with return_risk=True

Return type:

Any

Returns:

A sample from the distribution

static to_risk(risk)
Return type:

Tensor

class capsa_tf.sculpt.Distribution

Static Base Class to represent a Distribution for the Sculpt Wrapper

abstract static loss_function(*args, **kwargs)
Return type:

Tensor

abstract static sample(risk_out)
Return type:

Any

abstract static to_risk(risk)
Return type:

Tensor