-
Notifications
You must be signed in to change notification settings - Fork 1
Sketch of generator. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,129 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||||||||||||||||
| from xopt.generator import Generator | ||||||||||||||||||||||||||||||||||||||||||||||
| from xopt import VOCS | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| class AmortizedBOEDGenerator(Generator): | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
| Amortized Bayesian Optimal Experimental Design generator using a pre-trained neural network. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Attributes: | ||||||||||||||||||||||||||||||||||||||||||||||
| ----------- | ||||||||||||||||||||||||||||||||||||||||||||||
| device : str | ||||||||||||||||||||||||||||||||||||||||||||||
| Device to run the model on ('cpu' or 'cuda'). | ||||||||||||||||||||||||||||||||||||||||||||||
| theta_range : tuple | ||||||||||||||||||||||||||||||||||||||||||||||
| Range of theta values where the model is valid. | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| device: str = 'cpu' | ||||||||||||||||||||||||||||||||||||||||||||||
| max_measure: int = 20 # TODO: make this configurable | ||||||||||||||||||||||||||||||||||||||||||||||
| n_thetas: int = 100 # TODO: make this configurable | ||||||||||||||||||||||||||||||||||||||||||||||
| theta_range: tuple = (0.0, 100) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # These are not Pydantic fields - they're set in __init__ | ||||||||||||||||||||||||||||||||||||||||||||||
| model: torch.jit.ScriptModule = None | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, model_path: str, vocs=None, **kwargs): | ||||||||||||||||||||||||||||||||||||||||||||||
| """Initialize generator with a TorchScript model.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| super().__init__(vocs=vocs, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||||||
| # Load the traced TorchScript model | ||||||||||||||||||||||||||||||||||||||||||||||
| self.model = torch.jit.load(model_path, map_location=self.device) | ||||||||||||||||||||||||||||||||||||||||||||||
| self.model.eval() | ||||||||||||||||||||||||||||||||||||||||||||||
| # Generate theta values within the specified range | ||||||||||||||||||||||||||||||||||||||||||||||
| self.__dict__['theta_values'] = torch.linspace( | ||||||||||||||||||||||||||||||||||||||||||||||
| self.theta_range[0], self.theta_range[1], steps=self.n_thetas | ||||||||||||||||||||||||||||||||||||||||||||||
| ).unsqueeze(1).float() | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def pad(self, tensor: torch.Tensor) -> torch.Tensor: | ||||||||||||||||||||||||||||||||||||||||||||||
| """Pad the input tensor to the maximum measurement size.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| pad_size = self.max_measure - tensor.shape[1] | ||||||||||||||||||||||||||||||||||||||||||||||
| if pad_size > 0: | ||||||||||||||||||||||||||||||||||||||||||||||
| padding = torch.zeros(tensor.shape[0], pad_size, tensor.shape[2]).float() | ||||||||||||||||||||||||||||||||||||||||||||||
| return torch.cat([tensor, padding], dim=1) | ||||||||||||||||||||||||||||||||||||||||||||||
| return tensor | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def generate(self, n_candidates: int = 1) -> list[dict]: | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| def generate(self, n_candidates: int = 1) -> list[dict]: | |
| def generate(self, n_candidates: int = 1) -> list[dict]: | |
| """ | |
| Generate candidate experimental designs using the pre-trained neural network model. | |
| Parameters | |
| ---------- | |
| n_candidates : int, optional | |
| Number of candidate designs to generate (default is 1). | |
| Returns | |
| ------- | |
| list of dict | |
| A list of dictionaries, each containing variable names as keys and the generated candidate values. | |
| Example: [{'x': value1}, {'x': value2}, ...] | |
| Notes | |
| ----- | |
| - Currently, only a single candidate is produced per call to the model. To generate multiple candidates, | |
| the method calls the model multiple times or may require modification of the model. | |
| - The output dictionary structure is based on the VOCS variable names. | |
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can also call X.grid_evaluate() to do the same thing if you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is what I did originally, this is just to match how the model was trained. For the time being I've fixed the first measurment to be at T=100, thats something that is easy to change so that the model can handle random first measurements.
Uh oh!
There was an error while loading. Please reload this page.