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

_NumpyroOptim.init emits jax type for iter count #603

Merged
merged 2 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion numpyro/optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def init(self, params: _Params) -> _IterOptState:
:return: initial optimizer state.
"""
opt_state = self.init_fn(params)
return 0, opt_state
return np.array(0), opt_state

def update(self, g: _Params, state: _IterOptState) -> _IterOptState:
"""
Expand Down
33 changes: 33 additions & 0 deletions test/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,36 @@ def test_optim_multi_params(optim_class, args):
opt_state = step(opt_state, opt)
for _, param in opt.get_params(opt_state).items():
assert np.allclose(param, np.zeros(3))


# note: this is somewhat of a bruteforce test. testing directly from
# _NumpyroOptim would probably be better
@pytest.mark.parametrize('optim_class, args', [
(optim.Adam, (1e-2,)),
(optim.ClippedAdam, (1e-2,)),
(optim.Adagrad, (1e-1,)),
(optim.Momentum, (1e-2, 0.5,)),
(optim.RMSProp, (1e-2, 0.95)),
(optim.RMSPropMomentum, (1e-4,)),
(optim.SGD, (1e-2,))
])
def test_numpyrooptim_no_double_jit(optim_class, args):

opt = optim_class(*args)
state = opt.init(np.zeros(10))

my_fn_calls = 0

@jit
def my_fn(state, g):
nonlocal my_fn_calls
my_fn_calls += 1

state = opt.update(g, state)
return state

state = my_fn(state, np.ones(10)*1.)
state = my_fn(state, np.ones(10)*2.)
state = my_fn(state, np.ones(10)*3.)

assert my_fn_calls == 1