| import numpy as np |
|
|
|
|
| |
| class Network: |
| def __init__(self): |
| self.layers = [] |
| self.loss = None |
| self.loss_prime = None |
| self.weight_list = [] |
|
|
| def add(self, layer): |
| self.layers.append(layer) |
|
|
| def use(self, loss, loss_prime): |
| self.loss = loss |
| self.loss_prime = loss_prime |
|
|
| def fit(self, x_train, y_train, epochs, learning_rate): |
| samples = len(x_train) |
|
|
| |
| for i in range(epochs): |
| err = 0 |
|
|
| |
| for j in range(samples): |
| input = x_train[j] |
| output = None |
|
|
| |
| for layer in self.layers: |
| output = layer.forward_propagation(input) |
| |
| input = output |
|
|
| |
| err += self.loss(y_train[j], output) |
|
|
| |
| error = self.loss_prime(y_train[j], output) |
| |
| |
| for layer in reversed(self.layers): |
| error = layer.backward_propagation(error, learning_rate) |
|
|
| |
| err /= samples |
| print("Epoch %d/%d calculate with error = %f" % |
| (i + 1, epochs, err)) |
| print(f"Update weight to {layer.get_weight()} ") |
| print(f"Update Bias to {layer.get_bias()}") |
| print("") |
|
|
| def fit_on_sample(self): |
| raise NotImplementedError |
|
|
| def get_weight_list(self): |
| return self.weight_list |
|
|
| def get_weights(self): |
| raise NotImplementedError |
|
|
| def get_biases(self): |
| result = [] |
| for layer in self.layers: |
| result.append(layer.get_bias()) |
| return result |
|
|
| def predict_sample(self, first_input, second_input): |
| output = None |
|
|
| |
| for layer in self.layers: |
| output = layer.forward_propagation(first_input, second_input) |
| |
| first_input = output |
|
|
| return output |
|
|
| def back_propagate(self, error, learning_rate): |
| |
| for layer in reversed(self.layers): |
| error = layer.backward_propagation(error, learning_rate) |
| |
| |
|
|
| |
|
|
| def predict(self, first_input, second_input): |
| output = None |
|
|
| |
| for layer in self.layers: |
| output = layer.predict(first_input, second_input) |
| |
| first_input = output |
|
|
| return output |
|
|
| def check_type(self): |
| print( |
| "It is in the Initial Class Network Please call this function on the child class" |
| ) |
| return False |
|
|