-
Notifications
You must be signed in to change notification settings - Fork 78
Adapter scale and shift #394
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b95428d
add scale and shift transforms
LarsKue 1bfa9a5
add scale and shift to tests
LarsKue e051a48
fix numerical accuracy in adapter test for simple transforms
LarsKue fa0d6c3
add array-like scale test
LarsKue 0ddaad5
make scale and shift serializable
LarsKue f6c3b6d
fix scale and shift dispatch methods for single string input keys
LarsKue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -555,6 +555,24 @@ def rename(self, from_key: str, to_key: str): | |
| self.transforms.append(Rename(from_key, to_key)) | ||
| return self | ||
|
|
||
| def scale(self, keys: str | Sequence[str], by: float | np.ndarray): | ||
| from .transforms import Scale | ||
|
|
||
| if isinstance(keys, str): | ||
| keys = [keys] | ||
|
|
||
| self.transforms.append(MapTransform({key: Scale(scale=by) for key in keys})) | ||
|
||
| return self | ||
|
|
||
| def shift(self, keys: str | Sequence[str], by: float | np.ndarray): | ||
| from .transforms import Shift | ||
|
|
||
| if isinstance(keys, str): | ||
| keys = [keys] | ||
|
|
||
| self.transforms.append(MapTransform({key: Shift(shift=by) for key in keys})) | ||
| return self | ||
|
|
||
| def sqrt(self, keys: str | Sequence[str]): | ||
| """Append an :py:class:`~transforms.Sqrt` transform to the adapter. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from keras.saving import ( | ||
| deserialize_keras_object as deserialize, | ||
| register_keras_serializable as serializable, | ||
| serialize_keras_object as serialize, | ||
| ) | ||
| import numpy as np | ||
|
|
||
| from .elementwise_transform import ElementwiseTransform | ||
|
|
||
|
|
||
| @serializable(package="bayesflow.adapters") | ||
| class Scale(ElementwiseTransform): | ||
| def __init__(self, scale: np.typing.ArrayLike): | ||
| self.scale = np.array(scale) | ||
|
|
||
| @classmethod | ||
| def from_config(cls, config: dict, custom_objects=None) -> "ElementwiseTransform": | ||
| return cls(scale=deserialize(config["scale"])) | ||
|
|
||
| def get_config(self) -> dict: | ||
| return {"scale": serialize(self.scale)} | ||
|
|
||
| def forward(self, data: np.ndarray, **kwargs) -> np.ndarray: | ||
| return data * self.scale | ||
|
|
||
| def inverse(self, data: np.ndarray, **kwargs) -> np.ndarray: | ||
| return data / self.scale |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from keras.saving import ( | ||
| deserialize_keras_object as deserialize, | ||
| register_keras_serializable as serializable, | ||
| serialize_keras_object as serialize, | ||
| ) | ||
| import numpy as np | ||
|
|
||
| from .elementwise_transform import ElementwiseTransform | ||
|
|
||
|
|
||
| @serializable(package="bayesflow.adapters") | ||
| class Shift(ElementwiseTransform): | ||
| def __init__(self, shift: np.typing.ArrayLike): | ||
| self.shift = np.array(shift) | ||
|
|
||
| @classmethod | ||
| def from_config(cls, config: dict, custom_objects=None) -> "ElementwiseTransform": | ||
| return cls(shift=deserialize(config["shift"])) | ||
|
|
||
| def get_config(self) -> dict: | ||
| return {"shift": serialize(self.shift)} | ||
|
|
||
| def forward(self, data: np.ndarray, **kwargs) -> np.ndarray: | ||
| return data + self.shift | ||
|
|
||
| def inverse(self, data: np.ndarray, **kwargs) -> np.ndarray: | ||
| return data - self.shift |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.