You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow parameters to have different data types and shapes #218
API changesThis impacts the public API of the project (e.g. inference class).enhancementImprovement planned or wanted by the maintainersestimatorsNeural density estimators and the neural_nets modulehackathonFor our in-person hackathons with maintainers present, not a general up-for-grabs list
Our current idea of the parameters of a simulator is either something structured (such as a cross-section matrix) or a plurality of scalars and vectors that can be joined into one long vector and whose prior is a set of independent priors over diffferent coordinates (@janfb?).
Goal
It should be possible to have differently-shaped parameters such as e.g. a positive rate, a covariance matrix, and a speed vector passed in, each of them with their own private factor prior (i.e. no need to build an Independent prior to become, formally, a prior over all of them). This is more expressive, and more clear.
Caveats
This would require to be able to use lists of tensors instead of now just a single tensor, as input, and correspondingly, conforming (same-length, matching-output-shape) prior distributions.
The construction of the neural network, if honoring the structured nature of the inputs, may become complex.
Actually, I think we cannot handle multi-dimensional parameters yet, e.g., no matrices. If a user wants to define a prior over a matrix she has to implement a prior wrapper that returns a flattened version and then change her simulator such that it takes this flattened vector and reshapes it into a matrix for simulation.
changed the title [-]Parameter inputs may not conform into a single tensor.[/-][+]Parameter inputs may not conform into a single tensor[/+]on Jul 2, 2020
Update: we do not require an explicit prior object anymore, at least not for NPE. One can just pass pre-simulated theta and x.
theta must be a tensor though. Thus, if you have differently shaped "sub-priors" you need to take care of how to reshape this into a one-dimensional tensor.
Using pytrees as used in JAX as parameter objects might be an option for adding this feature.
How Pytrees Could Help
Handling Complex Data Structures: Pytrees can easily accommodate complex and nested structures. For your scenario, where parameters might include a positive rate (scalar), a covariance matrix (2D tensor), and a speed vector (1D tensor), Pytrees allow you to organize these into a single nested structure. You can then define operations that will apply uniformly across this structure.
Independent Priors: Each component of the Pytree can have its associated prior distribution. This allows you to manage and manipulate these priors independently, while still treating them as a coherent unit.
Mapping Functions: JAX’s utilities for Pytrees allow you to map functions over the entire structure. This is useful for tasks like computing likelihoods, gradients, or transforming parameters, which can be done in a consistent and automated way without having to manually unpack and repack the structures.
Neural Network Construction: When designing neural networks that process these structured inputs, Pytrees can help manage the complexity. You can define layers or transformations that operate on parts of the Pytree and then combine the results, respecting the structured nature of the inputs.
changed the title [-]Parameter inputs may not conform into a single tensor[/-][+]Allow parameters to have different data types and shapes[/+]on Dec 13, 2024
Thus, a starting point for addressing this issue would be:
understanding how sbi can benefit from using pytrees for theta and x
understand how to use pytrees in pytorch
estimate the impact of the changes we would need to make to get this to work (we need backwards compatibility, i.e., theta and x should be allowed to be Tensors as well).
API changesThis impacts the public API of the project (e.g. inference class).enhancementImprovement planned or wanted by the maintainersestimatorsNeural density estimators and the neural_nets modulehackathonFor our in-person hackathons with maintainers present, not a general up-for-grabs list
Activity
janfb commentedon Jun 8, 2020
Actually, I think we cannot handle multi-dimensional parameters yet, e.g., no matrices. If a user wants to define a prior over a matrix she has to implement a prior wrapper that returns a flattened version and then change her simulator such that it takes this flattened vector and reshapes it into a matrix for simulation.
[-]Parameter inputs may not conform into a single tensor.[/-][+]Parameter inputs may not conform into a single tensor[/+]janfb commentedon Feb 28, 2024
Update: we do not require an explicit prior object anymore, at least not for NPE. One can just pass pre-simulated
thetaandx.thetamust be a tensor though. Thus, if you have differently shaped "sub-priors" you need to take care of how to reshape this into a one-dimensional tensor.janfb commentedon Aug 6, 2024
Using pytrees as used in JAX as parameter objects might be an option for adding this feature.
How Pytrees Could Help
[-]Parameter inputs may not conform into a single tensor[/-][+]Allow parameters to have different data types and shapes[/+]janfb commentedon Mar 13, 2025
Update: We do not depend on JAX and cannot use its pytrees implementation for now. But there is one in PyTorch as well, see here: https://github.com/pytorch/pytorch/blob/main/torch/utils/_pytree.py
Thus, a starting point for addressing this issue would be:
sbican benefit from using pytrees forthetaandxthetaandxshould be allowed to beTensorsas well).