keyboard_arrow_down NOR SIGMOID import numpy as np inputs = np.array([[0,0], [1,0], [0,1], [1,1]]) outputs = np.array([1, 0, 0, 0]) sum1 = np.array([0.0, 0.0]) weights = np.array([0.0, 0.0]) learning_rate = 0.1 def step_function(sum): if (sum >= 1): return 1 return 0 def calculate_output(instance): s = sigmoid(np.dot(instance,weights)) return step_function(s) def calculate_output1(instance): s = sigmoid(np.dot(instance,weights)) step_function(s) if step_function(s) == 1: return 0 return 1 def sigmoid(sum): return 1 / (1 + np.exp(-sum)) def train(): total_error = 1 while (total_error != 0): total_error = 0 for i in range(len(outputs)): prediction = calculate_output(inputs[i]) if (prediction == 0): prediction = 1 else: prediction = 0 error = abs(outputs[i] - prediction) total_error += error if error > 0: for j in range(len(weights)): sum1[j] = weights[j] * inputs[i][j] c1 = sum1[0] c2 = sum1[1] sum2 = c1 + c2 sig = sigmoid(sum2) print('Sigmoid: ' + str(sig)) print(inputs[i][j]) print(learning_rate) print(error) weights[j] = weights[j] + (learning_rate * inputs[i][j] * error) print('Weight updated: ' + str(weights[j])) print('Total error: ' + str(total_error)) print(prediction) train() account_circle 1 Weight updated: 36.60000000000025 Sigmoid: 0.9999999999999998 1 0.1 1 Weight updated: 36.60000000000025 Total error: 2 0 Sigmoid: 1.0 1 0.1 1 Weight updated: 36.70000000000025 Sigmoid: 0.9999999999999998 0 0.1 1 Weight updated: 36.60000000000025 Sigmoid: 0.5 0 0.1 1 Weight updated: 36.70000000000025 Sigmoid: 0.9999999999999998 1 0.1 1 Weight updated: 36.70000000000025 Total error: 2 0 Sigmoid: 1.0 1 0.1 1 Weight updated: 36.80000000000025 Sigmoid: 0.9999999999999998 0 0.1 1 Weight updated: 36.70000000000025 Sigmoid: 0.5 0 0.1 1 Weight updated: 36.80000000000025 Sigmoid: 0.9999999999999998 1 0.1 1 Weight updated: 36.80000000000025 Total error: 2 0 Total error: 0 0 weights array([36.8, 36.8]) calculate_output1(np.array([0, 0])) 1 calculate_output1(np.array([0, 1])) 0 calculate_output1(np.array([1, 0])) 0 calculate_output1(np.array([1, 1])) 0