Created
September 15, 2021 00:05
-
-
Save YCAyca/97e8abdaefdd0dede0f708c213b91689 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Dataset """ | |
v1 = [1, 1, 0, 0] | |
v2 = [1, 0, 0, 0] | |
v3 = [0, 0, 0, 1] | |
v4 = [0, 0, 1, 1] | |
inputs = [v1, v2, v3, v4] | |
dataset_length = len(inputs) | |
print(dataset_length) | |
""" Training """ | |
input_length = len(v1) # length of 1 input vector | |
weights0 = [0.09, 0.2, 0.5, 0.95] | |
weights1 = [0.96, 0.15, 0.9, 0.35] | |
weights_length = len(weights0) | |
iteration_number = 100 | |
for i in range(0,iteration_number): | |
if i < 10: | |
learning_rate = 0.6; | |
else: | |
learning_rate = 0.3; | |
for k in range(0,dataset_length): # for 1 iteration, we use each input vector one by one | |
distance0 = 0 | |
distance1 = 0 | |
for j in range(0,input_length): | |
distance0 += pow(inputs[k][j] - weights0[j],2) | |
distance1 += pow(inputs[k][j] - weights1[j], 2) | |
distances = [distance0, distance1]; | |
dmin = min(distances) | |
index = distances.index(min(distances)) | |
if index == 0: #update w0 | |
for j in range(0,weights_length): | |
weights0[k] = weights0[k] + learning_rate* (inputs[k][j] - weights0[k]) | |
else: #update w1 | |
for j in range(0,weights_length): | |
weights1[k] = weights1[k] + learning_rate* (inputs[k][j] - weights1[k]) | |
print(weights0) | |
print(weights1) | |
""" Testing """ | |
test_v1 = [0, 0, 1, 0.9] | |
test_v2 =[0, 0, 0.8, 0.9] | |
test_v3 =[0.7, 0, 0, 0] | |
test_v4 =[0.7, 0.9, 0, 0] | |
test_inputs = [test_v1, test_v2, test_v3, test_v4] | |
test_dataset_length = len(test_inputs) | |
for k in range(0,test_dataset_length): | |
distance0 = 0 | |
distance1 = 0 | |
for j in range(0,input_length): | |
distance0 += pow(test_inputs[k][j] - weights0[j],2) | |
distance1 += pow(test_inputs[k][j] - weights1[j], 2) | |
distances = [distance0, distance1]; | |
dmin = min(distances) | |
index = distances.index(min(distances)) | |
print(test_inputs[k], "vector belongs to", index, ". class") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment