Skip to content
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

Argument init in get_parameter is not optional #588

Closed
hylkedonker opened this issue Dec 28, 2022 · 2 comments
Closed

Argument init in get_parameter is not optional #588

hylkedonker opened this issue Dec 28, 2022 · 2 comments

Comments

@hylkedonker
Copy link

hylkedonker commented Dec 28, 2022

Hi,

According to the docs, the init argument of get_parameter is optional, while in reality it raises the error:
ValueError: Initializer must be specified.
(See line 496 in base.py.)

Example
An example where the init=None might occur is if the initialisation is done outside Haiku.
For example:

import haiku as hk
import jax.numpy as jnp

@hk.without_apply_rng
@hk.transform
def foo(x):
    w = hk.get_parameter("w", [1], init=None)
    return x + w

# Initialise params outside haiku, without `foo.init`.
params = {'~': {'w': jnp.array([2.], dtype=jnp.float32)}}

x = jnp.array([1])
foo.apply(params, x)

Kind regards,

Hylke

copybara-service bot pushed a commit that referenced this issue Jan 10, 2023
Fixes #588.

PiperOrigin-RevId: 500935075
@tomhennigan
Copy link
Collaborator

Thank you Hylke, I'll fix this for the next release of Haiku. For now you can work around this by passing in a throwing initializer:

def throw_if_run(shape, dtype):
  del shape, dtype
  raise ValueError("Initializer must be specified.")

w = hk.get_parameter("w", [], init=throw_if_run)

copybara-service bot pushed a commit that referenced this issue Jan 10, 2023
Fixes #588.

PiperOrigin-RevId: 500935075
@hylkedonker
Copy link
Author

Great, thanks a lot!

copybara-service bot pushed a commit that referenced this issue Jan 10, 2023
Fixes #588.

PiperOrigin-RevId: 500935075
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants