Created
February 6, 2020 22:54
-
-
Save llSourcell/78b9091cdd927e0dc78fcb005e3c71f7 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
| #by MBT on StackOverflow | |
| import numpy as np | |
| # D_in is input dimension; | |
| # H is hidden dimension; | |
| # D_out is output dimension. | |
| Batch_Size, D_in, H, D_out = 12, 1000, 100, 10 | |
| # Create random input and output data | |
| x = np.random.randn(Batch_Size, D_in) | |
| y = np.random.randn(Batch_Size, D_out) | |
| # Randomly initialize weights | |
| w1 = np.random.randn(D_in, H) | |
| w2 = np.random.randn(H, D_out) | |
| learning_rate = 1e-6 | |
| for t in range(500): | |
| # Forward pass: compute predicted y | |
| h = x.dot(w1) | |
| h_relu = np.maximum(h, 0) | |
| y_pred = h_relu.dot(w2) | |
| # Compute and print loss | |
| loss = np.square(y_pred - y).sum() | |
| print(t, loss) | |
| # Backprop to compute gradients of w1 and w2 with respect to loss | |
| grad_y_pred = 2.0 * (y_pred - y) | |
| grad_w2 = h_relu.T.dot(grad_y_pred) | |
| grad_h_relu = grad_y_pred.dot(w2.T) | |
| grad_h = grad_h_relu.copy() | |
| grad_h[h < 0] = 0 | |
| grad_w1 = x.T.dot(grad_h) | |
| # Update weights | |
| w1 -= learning_rate * grad_w1 | |
| w2 -= learning_rate * grad_w2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello this look interesting