Torch modulesο
irtorch.torch_modules contains various PyTorch modules. These modules are used in different contexts of the package, but can also be used separately.
- class irtorch.torch_modules.MonotonePolynomialModule(degree: int, in_features: int = 1, out_features: int = 1, intercept: int = False, relationship_matrix: Tensor = None, negative_relationships: bool = False, shared_directions: int = 1)ο
Bases:
ModuleA polynomial with monotonicity constraints.
- Parameters
degree (int) β Degree of the polynomial. Needs to be an uneven number.
in_features (int) β Number of input features.
out_features (int) β Number of output features.
intercept (bool) β Whether to include an intercept term. (Default: False)
relationship_matrix (torch.Tensor, optional) β A boolean tensor of shape (in_features, out_features,) that determines which inputs are related to which outputs. Typically used for IRT models to remove relationships between some items or item categories and latent variables. (Default: None)
negative_relationships (bool, optional) β Whether to allow for negative relationships. (Default: False)
shared_directions (int, optional) β Only when negative_relationships is true. Number of out_features with shared relationship directions. out_features needs to be divisible with this. (Default: 1)
- forward(x: Tensor) Tensorο
Define 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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- get_polynomial_coefficients() Tensorο
Returns the polynomial coefficients.
- Returns
Tuple of tensors containing the coefficients of the polynomial with dimensions (degree, input_dim, output_dim), the second tensor contains the intercept if it exists and None otherwise.
- Return type
tuple[torch.Tensor, torch.Tensor]
- class irtorch.torch_modules.NeuralSplineFlow(transformation: RationalQuadraticSpline, distribution: Distribution)ο
Bases:
ModuleNormalizing flow using rational-quadratic splines [7].
- Parameters
transform (RationalQuadraticSpline) β Transforms values from the observed data to the chosen distribution.
distribution (Distribution) β The base distribution of the flow that generates the observed data.
- forward(inputs) Tensorο
Transforms from observed data into the chosen distribution (noise).
- Parameters
inputs (torch.Tensor) β A tensor of shape [batch_size, β¦], the data to be transformed.
- Returns
A tensor of shape [batch_size, β¦].
- Return type
torch.Tensor
- inverse(inputs) Tensorο
Transforms from the chosen distribution (noise) into observed data.
- Parameters
inputs (torch.Tensor) β A tensor of shape [batch_size, β¦], the data to be transformed.
- Returns
A tensor of shape [batch_size, β¦].
- Return type
torch.Tensor
- log_prob(value) Tensorο
The log-likelihood of the observed data in the chosen distribution after transformation. Usually the negative loss when training.
- Parameters
value (torch.Tensor) β A tensor of shape [batch_size, β¦], the data to be transformed.
- Returns
A tensor of shape [batch_size, β¦].
- Return type
torch.Tensor
- class irtorch.torch_modules.RationalQuadraticSpline(variables: int, num_bins=30, lower_input_bound=0.0, upper_input_bound=1.0, lower_output_bound=0.0, upper_output_bound=1.0, min_bin_width=0.001, min_bin_height=0.001, min_derivative=0.001, free_endpoints=False)ο
Bases:
ModuleThis module implements a rational quadratic spline, as described in the paper βNeural Spline Flowsβ by Durkan et al. [7].
- Parameters
variables (int) β The number of variables (dimensions) to transform.
num_bins (int, optional) β The number of bins to use for the spline (default is 30).
lower_input_bound (float, optional) β The left boundary of the transformation interval (default is 0.0).
upper_input_bound (float, optional) β The right boundary of the transformation interval (default is 1.0).
lower_output_bound (float, optional) β The bottom boundary of the transformation interval (default is 0.0).
upper_output_bound (float, optional) β The top boundary of the transformation interval (default is 1.0).
min_bin_width (float, optional) β The minimum width of each bin (default is 1e-3).
min_bin_height (float, optional) β The minimum height of each bin (default is 1e-3).
min_derivative (float, optional) β The minimum derivative value at the knots (default is 1e-3).
free_endpoints (bool, optional) β When free_endpoints=True, the lower and upper output bounds become trainable parameters (default is False).
- forward(inputs, inverse=False)ο
Define 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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.