Skip to content

Commit d5cfbee

Browse files
authored
Replace instances of RandomState with random.Generator (numpy#122)
Updates the nlp tutorial and related code to use the random.Generator interface.
1 parent 345cbac commit d5cfbee

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

content/text_preprocessing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import re # (https://docs.python.org/3/library/re.html) for tokenising textual data
55
import string # (https://docs.python.org/3/library/string.html) for string operations
66

7+
# Creating the random instance
8+
rng = np.random.default_rng()
9+
710
class TextPreprocess:
811
"""Text Preprocessing for a Natural Language Processing model."""
912

@@ -84,7 +87,7 @@ def split_data (self, X, y, split_percentile):
8487
8588
"""
8689
y = np.array(list(map(lambda x: 1 if x=="positive" else 0, y)))
87-
arr_rand = np.random.rand(X.shape[0])
90+
arr_rand = rng.random(X.shape[0])
8891
split = arr_rand < np.percentile(arr_rand, split_percentile)
8992
X_train = X[split]
9093
y_train = y[split]

content/tutorial-nlp-from-scratch.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ import string
117117
import re
118118
import zipfile
119119
import os
120+
121+
# Creating the random instance
122+
rng = np.random.default_rng()
120123
```
121124

122125
Next, you'll define set of text preprocessing helper functions.
@@ -458,20 +461,20 @@ Lets start with writing a function to randomly initialize the parameters which w
458461
```python
459462
def initialise_params(hidden_dim, input_dim):
460463
# forget gate
461-
Wf = np.random.randn(hidden_dim, hidden_dim + input_dim)
462-
bf = np.random.randn(hidden_dim, 1)
464+
Wf = rng.standard_normal(size=(hidden_dim, hidden_dim + input_dim))
465+
bf = rng.standard_normal(size=(hidden_dim, 1))
463466
# input gate
464-
Wi = np.random.randn(hidden_dim, hidden_dim + input_dim)
465-
bi = np.random.randn(hidden_dim, 1)
467+
Wi = rng.standard_normal(size=(hidden_dim, hidden_dim + input_dim))
468+
bi = rng.standard_normal(size=(hidden_dim, 1))
466469
# candidate memory gate
467-
Wcm = np.random.randn(hidden_dim, hidden_dim + input_dim)
468-
bcm = np.random.randn(hidden_dim, 1)
470+
Wcm = rng.standard_normal(size=(hidden_dim, hidden_dim + input_dim))
471+
bcm = rng.standard_normal(size=(hidden_dim, 1))
469472
# output gate
470-
Wo = np.random.randn(hidden_dim, hidden_dim + input_dim)
471-
bo = np.random.randn(hidden_dim, 1)
473+
Wo = rng.standard_normal(size=(hidden_dim, hidden_dim + input_dim))
474+
bo = rng.standard_normal(size=(hidden_dim, 1))
472475

473476
# fully connected layer for classification
474-
W2 = np.random.randn(1, hidden_dim)
477+
W2 = rng.standard_normal(size=(1, hidden_dim))
475478
b2 = np.zeros((1, 1))
476479

477480
parameters = {
@@ -575,7 +578,7 @@ def forward_prop(X_vec, parameters, input_dim):
575578
# Retrieve word corresponding to current time step
576579
x = X_vec[t]
577580
# Retrieve the embedding for the word and reshape it to make the LSTM happy
578-
xt = emb_matrix.get(x, np.random.rand(input_dim, 1))
581+
xt = emb_matrix.get(x, rng.random(size=(input_dim, 1)))
579582
xt = xt.reshape((input_dim, 1))
580583

581584
# Input to the gates is concatenated previous hidden state and current word embedding

0 commit comments

Comments
 (0)