Created
August 20, 2018 18:30
-
-
Save ImadDabbura/0a15f59773318690f14077ddd1bb07da 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 model( | |
| file_path, chars_to_idx, idx_to_chars, hidden_layer_size, vocab_size, | |
| num_epochs=10, learning_rate=0.01): | |
| """Implements RNN to generate characters.""" | |
| # Get the data | |
| with open(file_path) as f: | |
| data = f.readlines() | |
| examples = [x.lower().strip() for x in data] | |
| # Initialize parameters | |
| parameters = initialize_parameters(vocab_size, hidden_layer_size) | |
| # Initialize Adam parameters | |
| s = initialize_rmsprop(parameters) | |
| # Initialize loss | |
| smoothed_loss = -np.log(1 / vocab_size) * 7 | |
| # Initialize hidden state h0 and overall loss | |
| h_prev = np.zeros((hidden_layer_size, 1)) | |
| overall_loss = [] | |
| # Iterate over number of epochs | |
| for epoch in range(num_epochs): | |
| print(f"\033[1m\033[94mEpoch {epoch}") | |
| print(f"\033[1m\033[92m=======") | |
| # Sample one name | |
| print(f"""Sampled name: {sample(parameters, idx_to_chars, chars_to_idx, | |
| 10).capitalize()}""") | |
| print(f"Smoothed loss: {smoothed_loss:.4f}\n") | |
| # Shuffle examples | |
| np.random.shuffle(examples) | |
| # Iterate over all examples (SGD) | |
| for example in examples: | |
| x = [None] + [chars_to_idx[char] for char in example] | |
| y = x[1:] + [chars_to_idx["\n"]] | |
| # Fwd pass | |
| loss, cache = rnn_forward(x, y, h_prev, parameters) | |
| # Compute smooth loss | |
| smoothed_loss = smooth_loss(smoothed_loss, loss) | |
| # Bwd pass | |
| grads, h_prev = rnn_backward(y, parameters, cache) | |
| # Update parameters | |
| parameters, s = update_parameters_with_rmsprop( | |
| parameters, grads, s) | |
| overall_loss.append(smoothed_loss) | |
| return parameters, overall_loss |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment