-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from EdanToledo/chore/use_tfp_deterministic_dis…
…tribution chore: use tfd dist instead of created one
- Loading branch information
Showing
10 changed files
with
99 additions
and
146 deletions.
There are no files selected for viewing
This file contains 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,41 +1,70 @@ | ||
from functools import partial | ||
from typing import Any, Callable, Sequence | ||
|
||
import chex | ||
import jax | ||
import jax.numpy as jnp | ||
from flax import linen as nn | ||
from tensorflow_probability.substrates.jax.distributions import Distribution | ||
|
||
# Different to bijectors, postprocessors simply wrap the sample and mode methods of a distribution. | ||
|
||
class RescaleToSpec(nn.Module): | ||
minimum: float | ||
maximum: float | ||
|
||
@nn.compact | ||
def __call__(self, inputs: chex.Array) -> chex.Array: | ||
scale = self.maximum - self.minimum | ||
offset = self.minimum | ||
inputs = 0.5 * (inputs + 1.0) # [0, 1] | ||
output = inputs * scale + offset # [minimum, maximum] | ||
return output | ||
class PostProcessedDistribution(Distribution): | ||
"""A distribution that applies a postprocessing function to the samples and mode. | ||
This is useful for transforming the output of a distribution to a different space, such as | ||
rescaling the output of a tanh-transformed Normal distribution to a different range. However, | ||
this is not the same as a bijector, which also transforms the density function of the | ||
distribution. This is only useful for transforming the samples and mode of the distribution. | ||
For example, for an algorithm that requires taking the log probability of the samples, the | ||
distribution should be transformed using a bijector, not a postprocessor.""" | ||
|
||
class TanhToSpec(nn.Module): | ||
minimum: float | ||
maximum: float | ||
def __init__( | ||
self, distribution: Distribution, postprocessor: Callable[[chex.Array], chex.Array] | ||
): | ||
self.distribution = distribution | ||
self.postprocessor = postprocessor | ||
|
||
@nn.compact | ||
def __call__(self, inputs: chex.Array) -> chex.Array: | ||
scale = self.maximum - self.minimum | ||
offset = self.minimum | ||
inputs = jax.nn.tanh(inputs) # [-1, 1] | ||
inputs = 0.5 * (inputs + 1.0) # [0, 1] | ||
output = inputs * scale + offset # [minimum, maximum] | ||
return output | ||
def sample(self, seed: chex.PRNGKey, sample_shape: Sequence[int] = ()) -> chex.Array: | ||
return self.postprocessor(self.distribution.sample(seed=seed, sample_shape=sample_shape)) | ||
|
||
def mode(self) -> chex.Array: | ||
return self.postprocessor(self.distribution.mode()) | ||
|
||
def __getattr__(self, name: str) -> Any: | ||
if name == "__setstate__": | ||
raise AttributeError(name) | ||
return getattr(self.distribution, name) | ||
|
||
|
||
def rescale_to_spec(inputs: chex.Array, minimum: float, maximum: float) -> chex.Array: | ||
scale = maximum - minimum | ||
offset = minimum | ||
inputs = 0.5 * (inputs + 1.0) # [0, 1] | ||
output = inputs * scale + offset # [minimum, maximum] | ||
return output | ||
|
||
|
||
def clip_to_spec(inputs: chex.Array, minimum: float, maximum: float) -> chex.Array: | ||
return jnp.clip(inputs, minimum, maximum) | ||
|
||
|
||
def tanh_to_spec(inputs: chex.Array, minimum: float, maximum: float) -> chex.Array: | ||
scale = maximum - minimum | ||
offset = minimum | ||
inputs = jax.nn.tanh(inputs) # [-1, 1] | ||
inputs = 0.5 * (inputs + 1.0) # [0, 1] | ||
output = inputs * scale + offset # [minimum, maximum] | ||
return output | ||
|
||
|
||
class ClipToSpec(nn.Module): | ||
class ScalePostProcessor(nn.Module): | ||
minimum: float | ||
maximum: float | ||
scale_fn: Callable[[chex.Array, float, float], chex.Array] | ||
|
||
@nn.compact | ||
def __call__(self, inputs: chex.Array) -> chex.Array: | ||
output = jnp.clip(inputs, self.minimum, self.maximum) | ||
return output | ||
def __call__(self, distribution: Distribution) -> Distribution: | ||
post_processor = partial(self.scale_fn, minimum=self.minimum, maximum=self.maximum) | ||
return PostProcessedDistribution(distribution, post_processor) |
This file contains 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.