Created
August 20, 2018 18:07
-
-
Save ImadDabbura/a59dc61d516b2ad8be95814f14a7d3c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def rnn_forward(x, y, h_prev, parameters): | |
| """Implement one Forward pass on one name.""" | |
| # Retrieve parameters | |
| Wxh, Whh, b = parameters["Wxh"], parameters["Whh"], parameters["b"] | |
| Why, c = parameters["Why"], parameters["c"] | |
| # Initialize inputs, hidden state, output, and probabilities dictionaries | |
| xs, hs, os, probs = {}, {}, {}, {} | |
| # Initialize x0 to zero vector | |
| xs[0] = np.zeros((vocab_size, 1)) | |
| # Initialize loss and assigns h_prev to last hidden state in hs | |
| loss = 0 | |
| hs[-1] = np.copy(h_prev) | |
| # Forward pass: loop over all characters of the name | |
| for t in range(len(x)): | |
| # Convert to one-hot vector | |
| if t > 0: | |
| xs[t] = np.zeros((vocab_size, 1)) | |
| xs[t][x[t]] = 1 | |
| # Hidden state | |
| hs[t] = np.tanh(np.dot(Wxh, xs[t]) + np.dot(Whh, hs[t - 1]) + b) | |
| # Logits | |
| os[t] = np.dot(Why, hs[t]) + c | |
| # Probs | |
| probs[t] = softmax(os[t]) | |
| # Loss | |
| loss -= np.log(probs[t][y[t], 0]) | |
| cache = (xs, hs, probs) | |
| return loss, cache |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment