Skip to content

Commit 8a28ea2

Browse files
authored
misc (#242)
1 parent 7efed41 commit 8a28ea2

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lectures/jax_intro.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ linalg.inv(B) # Inverse of identity is identity
103103
```
104104

105105
```{code-cell} ipython3
106-
linalg.eigh(B) # Computes eigenvalues and eigenvectors
106+
out = linalg.eigh(B) # Computes eigenvalues and eigenvectors
107+
out.eigenvalues
108+
```
109+
110+
```{code-cell} ipython3
111+
out.eigenvectors
107112
```
108113

109114
### Differences
@@ -233,7 +238,7 @@ If we use the same key again, we initialize at the same seed, so the random numb
233238
random.normal(key, (3, 3))
234239
```
235240

236-
To produce a (quasi-) independent draw, best practice is to "split" the existing key:
241+
To produce a (quasi-) independent draw, we can `split` the existing key.
237242

238243
```{code-cell} ipython3
239244
key, subkey = random.split(key)
@@ -247,14 +252,22 @@ random.normal(key, (3, 3))
247252
random.normal(subkey, (3, 3))
248253
```
249254

255+
As we will see, the `split` operation is particularly useful for parallel
256+
computing, where independent sequences or simulations can be given their own
257+
key.
258+
259+
Another option is `fold_in`, which produces new "independent" keys from a base
260+
key.
261+
250262
The function below produces `k` (quasi-) independent random `n x n` matrices using this procedure.
251263

252264
```{code-cell} ipython3
265+
base_key = random.PRNGKey(42)
253266
def gen_random_matrices(key, n, k):
254267
matrices = []
255-
for _ in range(k):
256-
key, subkey = random.split(key)
257-
matrices.append(random.uniform(subkey, (n, n)))
268+
for i in range(k):
269+
key = random.fold_in(base_key, i) # generate a fresh key
270+
matrices.append(random.uniform(key, (n, n)))
258271
return matrices
259272
```
260273

0 commit comments

Comments
 (0)