| import network as nw |
| import importlib |
| import numpy as np |
|
|
| importlib.reload(nw) |
|
|
|
|
| class TimeDrivenNetwork(nw.Network): |
| def __init__(self): |
| super().__init__() |
|
|
| def fit(self, cost_train, y_train, epochs, learning_rate, time_usage, day_amount): |
| result = [] |
| samples = len(cost_train) |
|
|
| |
| for i in range(epochs): |
| err = 0 |
|
|
| |
| for j in range(samples): |
| cost_input = cost_train[j] |
| time_input = time_usage[j] |
| da_input = day_amount[j] |
| output = None |
|
|
| |
| for layer in self.layers: |
| output = layer.forward_propagation( |
| cost_input, time_input, da_input) |
| |
| cost_input = output |
|
|
| |
| err += self.loss(y_train[j], output) |
|
|
| |
| error = self.loss_prime(y_train[j], output) |
| weight = [] |
|
|
| |
| |
| for layer in reversed(self.layers): |
| error = layer.backward_propagation(error, learning_rate) |
| weight.append(layer.get_weight()) |
|
|
| |
| err /= samples |
| print("Epoch %d/%d calculate with error = %f" % |
| (i + 1, epochs, err)) |
| result.append({"epoch": i + 1, "error": err}) |
| self.weight_list = np.append(self.weight_list, [weight]) |
|
|
| return result |
|
|
| def fit_on_sample(self, cost_input, y_train, learning_rate, time_input, day_amount): |
| err = 0 |
| output = None |
|
|
| |
| for layer in self.layers: |
| output = layer.forward_propagation( |
| cost_input, time_input, day_amount) |
| |
| cost_input = output |
|
|
| |
| err += self.loss(y_train, output) |
|
|
| |
| error = self.loss_prime(y_train, output) |
|
|
| |
| |
| for layer in reversed(self.layers): |
| error = layer.backward_propagation(error, learning_rate) |
| |
| return (output, err) |
|
|
| def get_weights(self): |
| result = [] |
| for layer in self.layers: |
| result.append(layer.get_weight()) |
| return result |
|
|
| def predict_sample(self, cost_input, time_input, day_amount): |
| output = None |
| for layer in self.layers: |
| output = layer.forward_propagation( |
| cost_input, time_input, day_amount) |
| |
| cost_input = output |
| weight = layer.get_weight() |
| self.weight_list = np.append(self.weight_list, [weight]) |
| return output |
|
|
| def predict(self, cost_input, time_input, day_amount): |
| output = None |
| for layer in self.layers: |
| output = layer.predict(cost_input, time_input, day_amount) |
| |
| cost_input = output |
| return output |
|
|
| def check_type(self, input_name): |
| if input_name == "capital" or input_name == "employee": |
| return True |
| print(f"You go to wrong class this is Time Driven not {input_name}") |
| return False |
|
|