Skip to content

Commit de152db

Browse files
committed
Change loss_function_wrapper(.) to use new dm-haiku v0.0.2 .apply structure
1 parent 958f6fe commit de152db

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

jax_toolkit/losses/tests/test_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def net_function(x: jnp.ndarray) -> jnp.ndarray:
2121
return predictions
2222

2323
net_transform = hk.transform(net_function)
24-
net_transform = hk.without_apply_rng(net_transform) # as deterministic outcome
2524
actual_loss_function_wrapper = get_haiku_loss_function(net_transform, loss="mean_squared_error")
2625

2726
# Check works

jax_toolkit/losses/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ def get_haiku_loss_function(
4848
loss_function = SUPPORTED_LOSSES[loss]
4949

5050
@jax.jit
51-
def loss_function_wrapper(params: hk.Params, x: jnp.ndarray, y_true: jnp.ndarray) -> jnp.ndarray:
52-
y_pred: jnp.ndarray = net_transform.apply(params, x)
51+
def loss_function_wrapper(
52+
params: hk.Params, x: jnp.ndarray, y_true: jnp.ndarray, rng: jnp.ndarray = None
53+
) -> jnp.ndarray:
54+
# rng argument can be used is net_transform.apply() is non-deterministic, and you require and "random seed"
55+
y_pred: jnp.ndarray = net_transform.apply(params, rng, x)
5356
loss_value: jnp.ndarray = loss_function(y_true, y_pred)
5457
return loss_value
5558

0 commit comments

Comments
 (0)