Comments (5)
Hello @trevorcai, @tomhennigan. I like a lot out of the box solutions, but I struggle with extending haiku
at the moment. I need constrained parameters like variance (only positive) for Gaussian distributions. The parameter can be represented as a composition constraint: unconstrained_parameter -> bijector.forward(parameter)
, in my code it is a property of the module. A dictionary with a set of parameters contains only unconstrained version, but for tracking and model printing we need constrained values and there is no way to get it because the model instance is hidden in the function.
class Parameter():
def __init__(self, init_value: float, name: Text):
super().__init__(name="parameter")
self._name = name
self._init = hk.initializer.Constant(jnp.log(init_value))
def __call__(self):
return jnp.exp(hk.get_parameter(f"unconstrained_{self._name}", shape=[], init=self._init))
class Model(hk.Module):
def __init__(self, init_variance: float, name: Text):
super().__init__(name)
self._variance = Parameter(init_variance, "variance")
@property
def variance(self):
return self._variance()
def __call__(self, x: jnp.array) -> jnp.array:
return jnp.sum(self.variance * x)
As you can see, a variance value in a parameter dictionary will not have much meaning without information about a transformation that a model uses (could be exp, softplus or another positive bijector).
1. One solution could be to return a model with transformed functions.
def forward_fn(x):
m = Model(0.1)
hk.link(m)
return m(x)
forward = hk.transoform(forward_fn)
model = forward.linked_objects # get access to read only object
2. Another possible (?) solution could be making hk.transform
a context manager
class Holder(hk.ModuleHolder):
@hk.transform
def forward(self, x):
self.model = Model(0.1)
return self.model(x)
forward = Holder().forward()
PS: for me, it is a very important issue and a deciding factor on how I'm going to use the library.
from dm-haiku.
The primary reason we don't currently allow this is that hk.Module
objects have unique names (within their hk.transform
), accessible via self.name
or self.module_name
. These names route parameters & state into the right place for hk.get_parameter
calls, and are given to the module at construction time (in super().__init__(name=name)
).
Uniquifying names requires us to track some state about the names that have already been created. We've made an attempt towards allowing the construction of modules that don't use hk.get_parameter
and the other provided monads in their given constructors, but we haven't managed to do this without introducing persistent global state.
There are other solutions that we could try here! One idea is to late-bind names inside hk.transform
, but we haven't prioritized this line of work.
Does that make sense? WDYT?
from dm-haiku.
That all makes sense, thanks for the explanation!
One idea is to late-bind names inside hk.transform, but we haven't prioritized this line of work.
I think that would be great if that could be implemented without adding much complexity but I completely agree that it doesn't feel like a priority. I think the current API is just as powerful without this feature once you get use to it (which in my personal experience took me about 3 "oupsies" and cost me no more than 5 min in refactoring).
from dm-haiku.
I'm not sure if you want to keep this issue open for feature request tracking purposes but, if not, feel free to close it.
from dm-haiku.
That's good to hear - that's been my experience as well :)
I'll leave this issue open to track this FR.
from dm-haiku.
Related Issues (20)
- Use of numpy for causal masking in transformer example HOT 2
- More fine-grained mixed-precision configuration HOT 2
- Suggestion: alias `Transformed`(WithState) apply to __call__ HOT 2
- Is there a way to load parameters from Flax model? HOT 2
- Support model examples HOT 7
- Change to jax.interpreters.xla for JAX==0.4.14 HOT 3
- Warning: hk.LayerNorm when used in transformer decoder causes violation of autoregressive property HOT 1
- Reservoir Computing with Haiku
- Efficiency difference in using jax.lax.fori_loop vs looping over identical layers? HOT 2
- Please publish requirements.txt fix to pip
- How to use `apply` with additional parameters? HOT 1
- hk.Conv2DTranspose takes FOREVER to initialize and compile HOT 1
- 0.4.16 timeline HOT 2
- How to export haiku network parameters into Pytorch network?
- Modules got silently "reused" with `hk.vmap` HOT 2
- Wrong gradients in a Haiku network
- Direct Feedback Alignment
- Issue with wheels including docs and examples folder
- `haiku.experimental.flax` is not part of newest pip release HOT 1
- Train multiple hk.nets.MLP with one optimizer HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dm-haiku.