Guest User

Untitled

a guest
Nov 14th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. from layer import Layer
  2. import numpy as np
  3.  
  4. # inherit from base class Layer
  5. class FCLayer(Layer):
  6. # input_shape = (1,i) i the number of input neurons
  7. # output_shape = (1,j) j the number of output neurons
  8. def __init__(self, input_shape, output_shape):
  9. self.input_shape = input_shape;
  10. self.output_shape = output_shape;
  11. self.weights = np.random.rand(input_shape[1], output_shape[1]) - 0.5;
  12. self.bias = np.random.rand(1, output_shape[1]) - 0.5;
  13.  
  14. # returns output for a given input
  15. def forward_propagation(self, input):
  16. self.input = input;
  17. self.output = np.dot(self.input, self.weights) + self.bias;
  18. return self.output;
  19.  
  20. # computes dE/dW, dE/dB for a given output_error=dE/dY. Returns input_error=dE/dX.
  21. def backward_propagation(self, output_error, learning_rate):
  22. input_error = np.dot(output_error, self.weights.T);
  23. dWeights = np.dot(self.input.T, output_error);
  24. # dBias = output_error
  25.  
  26. # update parameters
  27. self.weights -= learning_rate * dWeights;
  28. self.bias -= learning_rate * output_error;
  29. return input_error;
Add Comment
Please, Sign In to add comment