| from layer import Layer |
| from tsensor import explain as exp |
| import numpy as np |
|
|
| import activation as act |
| import weight_activation as wa |
| import importlib |
|
|
| importlib.reload(act) |
| importlib.reload(wa) |
|
|
|
|
| class MaterialFCLayer(Layer): |
| def __init__(self, input_size, output_size): |
| self.weights = np.full((input_size, output_size), 1.0) |
| self.bias = np.full((1, output_size), 0.0) |
| print(f"weight shape {self.weights.shape}") |
| self.cost = None |
| self.amount = None |
| self.input = None |
|
|
| def annotate(self, cost_data, amount_data): |
| with exp() as c: |
| output = cost_data * amount_data @ self.weights + self.bias |
|
|
| |
| def predict(self, cost_data, amount_data): |
| cost_amount = np.multiply(cost_data, amount_data) |
| output = np.dot(cost_amount, self.weights) + self.bias |
| self.input = output |
| return act.leaky_relu(output) |
|
|
| |
| def forward_propagation(self, cost_data, amount_data): |
| self.cost = cost_data |
| self.amount = amount_data |
| if np.all(cost_data == 0) and np.all(amount_data == 0): |
| self.output = np.zeros((1, 1)) |
| return self.output |
| cost_amount = np.multiply(self.cost, self.amount) |
| self.output = np.dot(cost_amount, self.weights) |
| return act.leaky_relu(self.output) |
|
|
| def predict(self, cost_data, amount_data): |
| cost_amount = np.multiply(cost_data, amount_data) |
| output = np.dot(cost_amount, self.weights) |
| |
| self.input = output |
| |
|
|
| return act.leaky_relu(output) |
|
|
| |
| |
| |
| |
| |
| |
| def backward_propagation(self, output_error, learning_rate): |
| row, col = self.weights.shape |
| input_error = np.dot(output_error, self.weights.T) |
| gradient = np.multiply(self.cost, self.amount) |
|
|
| weight_error = np.dot(output_error, gradient) |
|
|
| weight_error = weight_error.reshape(row, col) |
| activation_input = self.input |
| activation_prime = act.leaky_relu_prime(activation_input) |
| weight_error = np.multiply(weight_error, activation_prime) |
| bias_error = output_error * activation_prime |
|
|
| self.weights -= learning_rate * weight_error |
| self.weights = wa.un_zero_weight(self.weights) |
| self.bias -= learning_rate * bias_error |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| return input_error |
|
|
| def get_weight(self): |
| return self.weights |
|
|
| def get_bias(self): |
| return self.bias |
|
|