Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from layer import Layer
- import numpy as np
- # inherit from base class Layer
- class FCLayer(Layer):
- # input_shape = (1,i) i the number of input neurons
- # output_shape = (1,j) j the number of output neurons
- def __init__(self, input_shape, output_shape):
- self.input_shape = input_shape;
- self.output_shape = output_shape;
- self.weights = np.random.rand(input_shape[1], output_shape[1]) - 0.5;
- self.bias = np.random.rand(1, output_shape[1]) - 0.5;
- # returns output for a given input
- def forward_propagation(self, input):
- self.input = input;
- self.output = np.dot(self.input, self.weights) + self.bias;
- return self.output;
- # computes dE/dW, dE/dB for a given output_error=dE/dY. Returns input_error=dE/dX.
- def backward_propagation(self, output_error, learning_rate):
- input_error = np.dot(output_error, self.weights.T);
- dWeights = np.dot(self.input.T, output_error);
- # dBias = output_error
- # update parameters
- self.weights -= learning_rate * dWeights;
- self.bias -= learning_rate * output_error;
- return input_error;
Add Comment
Please, Sign In to add comment