Advertisement
Guest User

NN in Python

a guest
Apr 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. import numpy as np
  2. from data_prep import features, targets, features_test, targets_test
  3.  
  4. np.random.seed(21)
  5.  
  6. def sigmoid(x):
  7.     """
  8.    Calculate sigmoid
  9.    """
  10.     return 1 / (1 + np.exp(-x))
  11.  
  12.  
  13. # Hyperparameters
  14. n_hidden = 2  # number of hidden units
  15. epochs = 900
  16. learnrate = 0.005
  17.  
  18. n_records, n_features = features.shape
  19. last_loss = None
  20. # Initialize weights
  21. weights_input_hidden = np.random.normal(scale=1 / n_features ** .5,
  22.                                         size=(n_features, n_hidden))
  23. weights_hidden_output = np.random.normal(scale=1 / n_features ** .5,
  24.                                          size=n_hidden)
  25.  
  26. for e in range(epochs):
  27.     del_w_input_hidden = np.zeros(weights_input_hidden.shape)
  28.     del_w_hidden_output = np.zeros(weights_hidden_output.shape)
  29.     for x, y in zip(features.values, targets):
  30.         ## Forward pass ##
  31.         # Calculate the output
  32.         hidden_input = np.dot(x, weights_input_hidden)
  33.         hidden_output = sigmoid(hidden_input)
  34.         output = sigmoid(np.dot(hidden_output, weights_hidden_output))
  35.  
  36.         ## Backward pass ##
  37.         # Calculate the network's prediction error
  38.         error = y - output
  39.  
  40.         # Calculate error term for the output unit
  41.         output_error_term = error*output*(1 - output)
  42.  
  43.         ## propagate errors to hidden layer
  44.  
  45.         # Calculate the hidden layer's contribution to the error
  46.         hidden_error = np.dot(output_error_term, weights_hidden_output)
  47.        
  48.         # Calculate the error term for the hidden layer
  49.         hidden_error_term = hidden_error*hidden_output*(1-hidden_output)
  50.        
  51.         # Update the change in weights
  52.         del_w_hidden_output += output_error_term*hidden_output
  53.         del_w_input_hidden += hidden_error_term*x[:, None]
  54.  
  55.     # Update weights
  56.     weights_input_hidden += learnrate*del_w_input_hidden/n_records
  57.     weights_hidden_output += learnrate*del_w_hidden_output/n_records
  58.  
  59.     # Printing out the mean square error on the training set
  60.     if e % (epochs / 10) == 0:
  61.         hidden_output = sigmoid(np.dot(x, weights_input_hidden))
  62.         out = sigmoid(np.dot(hidden_output,
  63.                              weights_hidden_output))
  64.         loss = np.mean((out - targets) ** 2)
  65.  
  66.         if last_loss and last_loss < loss:
  67.             print("Train loss: ", loss, "  WARNING - Loss Increasing")
  68.         else:
  69.             print("Train loss: ", loss)
  70.         last_loss = loss
  71.  
  72. # Calculate accuracy on test data
  73. hidden = sigmoid(np.dot(features_test, weights_input_hidden))
  74. out = sigmoid(np.dot(hidden, weights_hidden_output))
  75. predictions = out > 0.5
  76. accuracy = np.mean(predictions == targets_test)
  77. print("Prediction accuracy: {:.3f}".format(accuracy))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement