Last active
June 26, 2023 08:46
-
-
Save Mehdi-Amine/61ec3ae19e7df511eeabc7b2235345a6 to your computer and use it in GitHub Desktop.
perceptron class
This file contains 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
class Perceptron: | |
def __init__(self, input_size): | |
np.random.seed(42) | |
self.sizes = [input_size, 1] | |
self.bias = np.random.randn(1, 1) | |
self.weights = np.random.randn(1, input_size) | |
# used for plotting convergence | |
self.parameters_as_they_change = [np.concatenate((self.bias[0], self.weights.squeeze()), axis=0)] | |
print("Generated Perceptron:") | |
print(f"\tSizes: {self.sizes}") | |
print("With random parameters:") | |
print(f"\tBias: {self.bias}") | |
print(f"\tWeights: {self.weights}") | |
print("-------------------------------------------------------------") | |
def feedforward(self, a): | |
return np.dot(self.weights, a) + self.bias.squeeze() | |
def sgd(self, training_data, mini_batch_size, epochs, eta): | |
n = len(training_data) | |
for e in range(epochs): | |
shuffle(training_data) | |
mini_batches = [training_data[k:k + mini_batch_size] for k in range(0, n, mini_batch_size)] | |
for mini_batch in mini_batches: | |
self.update_mini_batch(mini_batch,eta) | |
# Tracking the effect of sgd on the parameters | |
parameters_concatenated = np.concatenate((self.bias[0], self.weights.squeeze()), axis=0) | |
self.parameters_as_they_change.append(parameters_concatenated) | |
print("Optimized parameters:") | |
print(f"\tBias: {self.bias}") | |
print(f"\tWeights: {self.weights}") | |
print("-------------------------------------------------------------") | |
def update_mini_batch(self, mini_batch, eta): | |
nabla_b = np.zeros(self.bias.shape) | |
nabla_w = np.zeros(self.weights.shape) | |
for x, y in mini_batch: | |
delta_nabla_b, delta_nabla_w = self.backprop(x, y) | |
nabla_b = nabla_b + delta_nabla_b | |
nabla_w = nabla_w + delta_nabla_w | |
self.weights = self.weights - (eta/len(mini_batch) * nabla_w) | |
self.bias = self.bias - (eta/len(mini_batch) * nabla_b) | |
''' | |
print("Updated Parameters:") | |
print(f"\tBias: {self.bias}") | |
print(f"\tWeights: {self.weights}") | |
print("-------------------------------------------------------------") | |
''' | |
def backprop(self, x, y): | |
nabla_b = np.zeros(self.bias.shape) | |
nabla_w = np.zeros(self.weights.shape) | |
# Feedforward | |
z = self.feedforward(x) | |
# Backprop | |
delta = self.cost_derivative(z, y) | |
delta = delta[..., None] | |
nabla_b = delta | |
nabla_w = np.dot(delta, x.reshape(1,-1)) | |
return nabla_b, nabla_w | |
def cost_derivative(self, output_activations, y): | |
return (output_activations-y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment