Skip to content

Instantly share code, notes, and snippets.

@ImadDabbura
Created August 20, 2018 18:29
Show Gist options
  • Select an option

  • Save ImadDabbura/2b53e2f917ec53f878f3ba9b75cb6fef to your computer and use it in GitHub Desktop.

Select an option

Save ImadDabbura/2b53e2f917ec53f878f3ba9b75cb6fef to your computer and use it in GitHub Desktop.
def sample(parameters, idx_to_chars, chars_to_idx, n):
"""
Implements sampling of a squence of n characters characters length.
The sampling will be based on the probability distribution output of RNN.
"""
# Retrienve parameters, shapes, and vocab size
Whh, Wxh, b = parameters["Whh"], parameters["Wxh"], parameters["b"]
Why, c = parameters["Why"], parameters["c"]
n_h, n_x = Wxh.shape
vocab_size = c.shape[0]
# Initialize a0 and x1 to zero vectors
h_prev = np.zeros((n_h, 1))
x = np.zeros((n_x, 1))
# Initialize empty sequence
indices = []
idx = -1
counter = 0
while (counter <= n and idx != chars_to_idx["\n"]):
# Fwd propagation
h = np.tanh(np.dot(Whh, h_prev) + np.dot(Wxh, x) + b)
o = np.dot(Why, h) + c
probs = softmax(o)
# Sample the index of the character using generated probs distribution
idx = np.random.choice(vocab_size, p=probs.ravel())
# Get the character of the sampled index
char = idx_to_chars[idx]
# Add the char to the sequence
indices.append(idx)
# Update a_prev and x
h_prev = np.copy(h)
x = np.zeros((n_x, 1))
x[idx] = 1
counter += 1
sequence = "".join([idx_to_chars[idx] for idx in indices if idx != 0])
return sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment