Skip to content

Commit

Permalink
update_modules()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-g committed Mar 9, 2021
1 parent 44b044a commit 11dc005
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion elegy/hooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def call(self, x):
return x

m = elegy.Model(Module0())
m.predict(jnp.ones(4)) # init
m.init(jnp.ones(4))

with elegy.hooks.context(named_call=True):
jaxpr = jax.make_jaxpr(
Expand Down
1 change: 1 addition & 0 deletions elegy/slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def slice_model(

if not model.initialized:
model.predict(sample_input, initialize=True)
model.update_modules()

with hooks.context(named_call=True), jax.disable_jit():
jaxpr = jax.make_jaxpr(model.pred_step, static_argnums=[2, 3])(
Expand Down
13 changes: 13 additions & 0 deletions elegy/slicing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ def test_basic_nested(self):
assert "module1_linear2" not in submodel.states["net_params"].keys()


def test_no_default_parameters():
x = np.random.random((32, 100)).astype("float32")
module = BasicModule0()
model = elegy.Model(module, seed=np.random.randint(100, 100000))
model.init(x)

submodel = elegy.Model(model.slice("linear0", "linear1", x))
assert submodel.predict(x, initialize=True).shape == (32, 10)

model.update_modules()
assert jnp.allclose(submodel.predict(x), module.test_call0(x))


class BasicModule0(elegy.Module):
def call(self, x):
x = x / 255.0
Expand Down

0 comments on commit 11dc005

Please sign in to comment.