About
input_size = 2 h_size = 3 o_size=1 weights_layer_1 = np.array([[-0.2, -0.1],[-1.4, 0.9],[0.6, 1.2]]) bias_1 = np.array([-1.4, -0.8, 0.2]) weights_layer_2 = np.array([0.2, -1.1, 0.5]) bias_2 = np.array([-0.6])
inputs = np.array([[-0.3, -1.5], [-1.7, 0.7]]) ground_truth = np.array([0.1, -0.3]) learn_rate = 0.1
def calculate_output(inputs, weights, bias,input_size): output = [] for i in range(input_size): temp = np.sum(np.dot(inputs, weights[i])) + bias[i] output.append(sigmoid(temp)) return output
def calculate_error(predicted, target): error = 0.5 * np.sum((predicted - target)**2) return error
def weight_update(weight_o, weight_h, bias_o, bias_h, h_size, o_size,h_output,output): o_delta = (output - ground_truth) sig_derivative(output) h_delta = sig_derivative(h_output) for i in range(h_size): temp_sum = 0 temp_sum += np.sum(o_delta weight_o[i]) h_delta[i] = temp_sum for j in range(o_size): for i in range(input_size): weight_h[i][j] -= learn_rate h_delta[j] inputs[i][j] bias_h -= learn_rate h_delta for j in range(o_size): for i in range(h_size): weight_o[i] -= learn_rate o_delta[j] h_output[i] bias_o -= learn_rate * o_delta[0] return weight_o, weight_h, bias_o, bias_h
def print_update(layer): if layer == 'output': print(f'Updated Weights and bias of output layer:\nWeights: \n{weights_layer_2}\nBias: {bias_2}') elif layer == 'hidden': print(f'Updated Weights and bias of hidden layer:\nWeights: \n{weights_layer_1}\nBias: {bias_1}')
epochs = 10 for i in range(epochs): for j in range(len(inputs)): h_output = np.array(calculate_output(inputs, weights_layer_1, bias_1,h_size)) output = np.array(calculate_output(h_output, weights_layer_2, bias_2,o_size)) weights_layer_2, weights_layer_1, bias_2, bias_1 = weight_update(weights_layer_2, weights_layer_1, bias_2, bias_1, h_size, o_size,h_output,output) print(f'Error in iteration {i+1} = {calculate_error(output, ground_truth)}')
print_update('output') print_update('hidden')
h_output_1 = np.array(calculate_output(inputs, weights_layer_1, bias_1, h_size)) h_output_1
output_1 = np.array(calculate_output(h_output, weights_layer_2, bias_2, o_size)) output_1
calculate_error(output_1, ground_truth)