threshold = 0.5 # any value
learning_rate = 0.1 # any value
weights = [1, 1, 1] # x0 = 1 is constant, x1, x2 any value
# AND GATE
training_set = [((1, 0, 0), 0), ((1, 0, 1), 0), ((1, 1, 0), 0), ((1, 1, 1), 1)]
# OR GATE
#training_set = [((1, 0, 0), 0), ((1, 0, 1), 1), ((1, 1, 0), 1), ((1, 1, 1), 1)]
def dot_product(values, weights):
return sum(value * weight for value, weight in zip(values, weights))
while True:
print('-' * 60)
error_count = 0
for input_vector, desired_output in training_set:
result = dot_product(input_vector, weights) > threshold
error = desired_output - result
if error != 0:
error_count += 1
for index, value in enumerate(input_vector):
weights[index] += learning_rate * error * value
if error_count == 0:
break
print(str(weights[1]) + ", " + str(weights[2])) # w1 & w2
print(threshold-weights[0]) # threshold
learning_rate = 0.1 # any value
weights = [1, 1, 1] # x0 = 1 is constant, x1, x2 any value
# AND GATE
training_set = [((1, 0, 0), 0), ((1, 0, 1), 0), ((1, 1, 0), 0), ((1, 1, 1), 1)]
# OR GATE
#training_set = [((1, 0, 0), 0), ((1, 0, 1), 1), ((1, 1, 0), 1), ((1, 1, 1), 1)]
def dot_product(values, weights):
return sum(value * weight for value, weight in zip(values, weights))
while True:
print('-' * 60)
error_count = 0
for input_vector, desired_output in training_set:
result = dot_product(input_vector, weights) > threshold
error = desired_output - result
if error != 0:
error_count += 1
for index, value in enumerate(input_vector):
weights[index] += learning_rate * error * value
if error_count == 0:
break
print(str(weights[1]) + ", " + str(weights[2])) # w1 & w2
print(threshold-weights[0]) # threshold
Niciun comentariu:
Trimiteți un comentariu