Skip to content

Commit d6034fe

Browse files
committed
replace loop with jax.vmap for vectorized value update
1 parent 6613e32 commit d6034fe

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lectures/mccall_q.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,11 @@ def plot_value_function_seq(mcm, ax, num_plots=8):
214214
v_next = jnp.empty_like(v)
215215
for i in range(num_plots):
216216
ax.plot(mcm.w, v, '-', alpha=0.4, label=f"iterate {i}")
217-
# Update guess
218-
for j in range(n):
219-
v_next = v_next.at[j].set(jnp.max(state_action_values(mcm, j, v)))
220-
v = v_next # handling immutability
217+
218+
indices = jnp.arange(n)
219+
v_next = jax.vmap(
220+
lambda j: jnp.max(state_action_values(mcm, j, v)))(indices)
221+
v = v_next
221222
222223
ax.legend(loc='lower right')
223224
```

0 commit comments

Comments
 (0)