Skip to content

Commit 84167cc

Browse files
authored
Ensure all tutorials use the recommended Generator API for random number generation (numpy#71)
* Use new random api to sample digits for display. * Change references in text to new random API (w/ links). * Use new random API in training/eval loop. * Use new random API in pong tutorial. * grab mnist data from github mirror.
1 parent 8d0f765 commit 84167cc

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

Diff for: content/tutorial-deep-learning-on-mnist.md

+22-16
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ download it.
8080
headers = {
8181
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0"
8282
}
83-
request_opts = {"headers": headers}
83+
request_opts = {
84+
"headers": headers,
85+
"params": {"raw": "true"},
86+
}
8487
```
8588

8689
```{code-cell} ipython3
@@ -90,7 +93,7 @@ import os
9093
data_dir = "../_data"
9194
os.makedirs(data_dir, exist_ok=True)
9295
93-
base_url = "http://yann.lecun.com/exdb/mnist/"
96+
base_url = "https://github.com/rossbar/numpy-tutorial-data-mirror/blob/main/"
9497
9598
for fname in data_sources.values():
9699
fpath = os.path.join(data_dir, fname)
@@ -153,12 +156,13 @@ plt.show()
153156

154157
```{code-cell} ipython3
155158
# Display 5 random images from the training set.
156-
np.random.seed(0)
157-
indices = list(np.random.randint(x_train.shape[0], size=9))
158-
for i in range(5):
159-
plt.subplot(1, 5, i+1)
160-
plt.imshow(x_train[indices[i]].reshape(28, 28), cmap='gray')
161-
plt.tight_layout()
159+
num_examples = 5
160+
seed = 147197952744
161+
rng = np.random.default_rng(seed)
162+
163+
fig, axes = plt.subplots(1, num_examples)
164+
for sample, ax in zip(rng.choice(x_train, size=num_examples, replace=False), axes):
165+
ax.imshow(sample.reshape(28, 28), cmap='gray')
162166
```
163167

164168
> **Note:** You can also visualize a sample image as an array by printing `x_train[59999]`. Here, `59999` is your 60,000th training image sample (`0` would be your first). Your output will be quite long and should contain an array of 8-bit integers:
@@ -308,7 +312,7 @@ Afterwards, you will construct the building blocks of a simple deep learning mod
308312

309313
> **Note:** For simplicity, the bias term is omitted in this example (there is no `np.dot(layer, weights) + bias`).
310314
311-
- _Weights_: These are important adjustable parameters that the neural network fine-tunes by forward and backward propagating the data. They are optimized through a process called [gradient descent](https://en.wikipedia.org/wiki/Stochastic_gradient_descent). Before the model training starts, the weights are randomly initialized with NumPy's `np.random.random()` function.
315+
- _Weights_: These are important adjustable parameters that the neural network fine-tunes by forward and backward propagating the data. They are optimized through a process called [gradient descent](https://en.wikipedia.org/wiki/Stochastic_gradient_descent). Before the model training starts, the weights are randomly initialized with NumPy's [`Generator.random()`](https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.random.html).
312316

313317
The optimal weights should produce the highest prediction accuracy and the lowest error on the training and test sets.
314318

@@ -318,7 +322,7 @@ Afterwards, you will construct the building blocks of a simple deep learning mod
318322

319323
- _Regularization_: This [technique](https://en.wikipedia.org/wiki/Regularization_(mathematics)) helps prevent the neural network model from [overfitting](https://en.wikipedia.org/wiki/Overfitting).
320324

321-
In this example, you will use a method called dropout — [dilution](https://en.wikipedia.org/wiki/Dilution_(neural_networks)) — that randomly sets a number of features in a layer to 0s. You will define it with NumPy's `np.random.randint()` function and apply it to the hidden layer of the network.
325+
In this example, you will use a method called dropout — [dilution](https://en.wikipedia.org/wiki/Dilution_(neural_networks)) — that randomly sets a number of features in a layer to 0s. You will define it with NumPy's [`Generator.integers()`](https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.integers.html) method and apply it to the hidden layer of the network.
322326

323327
- _Loss function_: The computation determines the quality of predictions by comparing the image labels (the truth) with the predicted values in the final layer's output.
324328

@@ -368,10 +372,12 @@ Here is a summary of the neural network model architecture and the training proc
368372

369373
Having covered the main deep learning concepts and the neural network architecture, let's write the code.
370374

371-
**1.** For reproducibility, initialize a random seed with `np.random.seed()`:
375+
**1.** We'll start by creating a new random number generator, providing a seed
376+
for reproducibility:
372377

373378
```{code-cell} ipython3
374-
np.random.seed(1)
379+
seed = 884736743
380+
rng = np.random.default_rng(seed)
375381
```
376382

377383
**2.** For the hidden layer, define the ReLU activation function for forward propagation and ReLU's derivative that will be used during backpropagation:
@@ -403,11 +409,11 @@ pixels_per_image = 784
403409
num_labels = 10
404410
```
405411

406-
**4.** Initialize the weight vectors that will be used in the hidden and output layers with `np.random.random()`:
412+
**4.** Initialize the weight vectors that will be used in the hidden and output layers with random values:
407413

408414
```{code-cell} ipython3
409-
weights_1 = 0.2 * np.random.random((pixels_per_image, hidden_size)) - 0.1
410-
weights_2 = 0.2 * np.random.random((hidden_size, num_labels)) - 0.1
415+
weights_1 = 0.2 * rng.random((pixels_per_image, hidden_size)) - 0.1
416+
weights_2 = 0.2 * rng.random((hidden_size, num_labels)) - 0.1
411417
```
412418

413419
**5.** Set up the neural network's learning experiment with a training loop and start the training process.
@@ -450,7 +456,7 @@ for j in range(epochs):
450456
# 3. Pass the hidden layer's output through the ReLU activation function.
451457
layer_1 = relu(layer_1)
452458
# 4. Define the dropout function for regularization.
453-
dropout_mask = np.random.randint(0, high=2, size=layer_1.shape)
459+
dropout_mask = rng.integers(low=0, high=2, size=layer_1.shape)
454460
# 5. Apply dropout to the hidden layer's output.
455461
layer_1 *= dropout_mask * 2
456462
# 6. The output layer:

Diff for: content/tutorial-deep-reinforcement-learning-with-pong-from-pixels.md

+14-4
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ Next, you will define the policy as a simple feedforward network that uses a gam
264264

265265
1. Let's instantiate certain parameters for the input, hidden, and output layers, and start setting up the network model.
266266

267+
Start by creating a random number generator instance for the experiment
268+
(seeded for reproducibility):
269+
270+
```{code-cell}
271+
272+
rng = np.random.default_rng(seed=12288743)
273+
```
274+
275+
Then:
276+
267277
+++ {"id": "PbqQ3kPBRfvn"}
268278

269279
- Set the input (observation) dimensionality - your preprocessed screen frames:
@@ -298,13 +308,13 @@ model = {}
298308

299309
In a neural network, _weights_ are important adjustable parameters that the network fine-tunes by forward and backward propagating the data.
300310

301-
2. Using a technique called [Xavier initialization](https://www.deeplearning.ai/ai-notes/initialization/#IV), set up the network model's initial weights with NumPy's [`np.random.randn()`](https://numpy.org/doc/stable/reference/random/generated/numpy.random.randn.html) that return random numbers over a standard Normal distribution, as well as [`np.sqrt()`](https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html?highlight=numpy.sqrt#numpy.sqrt):
311+
2. Using a technique called [Xavier initialization](https://www.deeplearning.ai/ai-notes/initialization/#IV), set up the network model's initial weights with NumPy's [`Generator.standard_normal()`](https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.standard_normal.html) that returns random numbers over a standard Normal distribution, as well as [`np.sqrt()`](https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html?highlight=numpy.sqrt#numpy.sqrt):
302312

303313
```{code-cell} ipython3
304314
:id: wh2pUHZ6FtUe
305315
306-
model['W1'] = np.random.randn(H,D) / np.sqrt(D)
307-
model['W2'] = np.random.randn(H) / np.sqrt(H)
316+
model['W1'] = rng.standard_normal(size=(H,D)) / np.sqrt(D)
317+
model['W2'] = rng.standard_normal(size=H) / np.sqrt(H)
308318
```
309319

310320
+++ {"id": "K4J5Elsiq5Qk"}
@@ -591,7 +601,7 @@ while episode_number < max_episodes:
591601
# 4. Let the action indexed at `2` ("move up") be that probability
592602
# if it's higher than a randomly sampled value
593603
# or use action `3` ("move down") otherwise.
594-
action = 2 if np.random.uniform() < aprob else 3
604+
action = 2 if rng.uniform() < aprob else 3
595605
596606
# 5. Cache the observations and hidden "states" (from the network)
597607
# in separate variables for backpropagation.

0 commit comments

Comments
 (0)