-
Notifications
You must be signed in to change notification settings - Fork 7
Add Scaled Sigmoid Link and Generic Inverse #80
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
Draft
simon-hirsch
wants to merge
3
commits into
main
Choose a base branch
from
links
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,47 @@ | ||
| from typing import Tuple | ||
|
|
||
| import numpy as np | ||
|
|
||
| from rolch.base import LinkFunction | ||
|
|
||
|
|
||
| class GenericInverseLink(LinkFunction): | ||
| """ | ||
| This link function maps an arbitrary link function to its inverse by swapping the links and derivatives. | ||
| You need to provide the link function and the support. | ||
| This can be used to quickly create inverse links or as base class if you want to implement a custom link function. | ||
| The default will not provide the second derivative, which you need to implement in the subclass. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| link_function: LinkFunction, | ||
| link_support: Tuple, | ||
| ) -> None: | ||
| """Initializes the GenericInverseLink class. | ||
|
|
||
| Args: | ||
| link_function (LinkFunction): The Link function to invert | ||
| link_support (Tuple): The support of the link function | ||
| """ | ||
| self.link_function = link_function | ||
| self._link_support = link_support | ||
|
|
||
| @property | ||
| def link_support(self) -> Tuple[float, float]: | ||
| return (self._link_support[0], self._link_support[1]) | ||
|
|
||
| def link(self, x: np.ndarray) -> np.ndarray: | ||
| return self.link_function.inverse(x) | ||
|
|
||
| def inverse(self, x: np.ndarray) -> np.ndarray: | ||
| return self.link_function.link(x) | ||
|
|
||
| def link_derivative(self, x: np.ndarray) -> np.ndarray: | ||
| return self.link_function.inverse_derivative(x) | ||
|
|
||
| def inverse_derivative(self, x: np.ndarray) -> np.ndarray: | ||
| return self.link_function.link_derivative(x) | ||
|
|
||
| def link_second_derivative(self, x: np.ndarray) -> np.ndarray: | ||
| return super().link_second_derivative(x) | ||
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,84 @@ | ||
| from typing import Tuple | ||
|
|
||
| import numpy as np | ||
|
|
||
| from ..base import LinkFunction | ||
| from .robust_math import robust_log, zero_safe_division | ||
|
|
||
|
|
||
| def sigmoid(y): | ||
| return 1 / (1 + np.fmax(1e-6, np.exp(-np.fmin(y, 25)))) | ||
|
|
||
|
|
||
| class ScaledInverseSigmoidLink(LinkFunction): | ||
|
|
||
| def __init__( | ||
| self, | ||
| lower: float = 0, | ||
| upper: float = 1, | ||
| ): | ||
| self.lower = lower | ||
| self.upper = upper | ||
|
|
||
| @property | ||
| def link_support(self) -> Tuple[float, float]: | ||
| return (self.lower + np.nextafter(0, 1), self.upper - np.nextafter(0, 1)) | ||
|
|
||
| def link(self, x: np.ndarray) -> np.ndarray: | ||
| return robust_log(x - self.lower) - robust_log(self.upper - x) | ||
|
|
||
| def inverse(self, x: np.ndarray) -> np.ndarray: | ||
| return self.lower + (self.upper - self.lower) * sigmoid(x) | ||
|
|
||
| def inverse_derivative(self, x): | ||
| return ( | ||
| (self.upper - self.lower) | ||
| * sigmoid(x) | ||
| * (1 - sigmoid(x)) | ||
| / (self.upper - self.lower) | ||
| ) | ||
|
|
||
| def link_derivative(self, x: np.ndarray) -> np.ndarray: | ||
| return zero_safe_division(1, x - self.lower) - zero_safe_division( | ||
| 1, x - self.upper | ||
| ) | ||
|
|
||
| def link_second_derivative(self, x) -> np.ndarray: | ||
| return super().link_second_derivative(x) | ||
|
|
||
|
|
||
| class ScaledSigmoidLink(LinkFunction): | ||
|
|
||
| def __init__( | ||
| self, | ||
| lower: float = 0, | ||
| upper: float = 1, | ||
| ): | ||
| self.lower = lower | ||
| self.upper = upper | ||
|
|
||
| @property | ||
| def link_support(self) -> Tuple[float, float]: | ||
| return (self.lower + np.nextafter(0, 1), self.upper - np.nextafter(0, 1)) | ||
|
|
||
| def link(self, x: np.ndarray) -> np.ndarray: | ||
| return self.lower + (self.upper - self.lower) * sigmoid(x) | ||
|
|
||
| def inverse(self, x: np.ndarray) -> np.ndarray: | ||
| return np.log(np.fmax(1e-10, x - self.lower)) - np.log( | ||
| np.fmax(1e-10, self.upper - x) | ||
| ) | ||
|
|
||
| def inverse_derivative(self, x): | ||
| return 1 / (x - self.lower) - 1 / (x - self.upper) | ||
|
|
||
| def link_derivative(self, x: np.ndarray) -> np.ndarray: | ||
| return ( | ||
| (self.upper - self.lower) | ||
| * sigmoid(x) | ||
| * (1 - sigmoid(x)) | ||
| / (self.upper - self.lower) | ||
| ) | ||
|
|
||
| def link_second_derivative(self, x) -> np.ndarray: | ||
| return super().link_second_derivative(x) |
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.
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.
This is misleading, we want to pass the support of the inverted link function here, right?