Created
May 23, 2019 09:47
-
-
Save ridwanbejo/66ea55e102d05f1e5eb1116e3036f4cb to your computer and use it in GitHub Desktop.
prototype
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
import math | |
import time | |
class LearningVectorQuantization(): | |
def __init__(self, alpha=0, learning_rate=0): | |
self.weight = {} | |
self.learning_rate = learning_rate | |
self.alpha = alpha | |
def generate_weight(self, feature_len): | |
self.weight = {'0': [1, 1, 1, 0], '1':[1, 0, 1, 1]} | |
def calculate_distance(self, feature, weight): | |
sum_distance = 0 | |
for i in range(0, len(feature)): | |
sum_distance = sum_distance + ((feature[i]-weight[i]) ** 2) | |
return math.sqrt(sum_distance) | |
def fit(self, dataset=[], epoch=1): | |
dataset_len = len(dataset) | |
self.generate_weight(len(dataset[0][0])) | |
for i in range(0, EPOCH): | |
print ("EPOCH --- ", i+1) | |
print ("--------------") | |
for j in range(0, dataset_len): | |
feature = dataset[j][0] | |
target = dataset[j][1] | |
print (feature, ' ', target) | |
# threadable | |
t1 = self.calculate_distance(feature, self.weight['0']) | |
t2 = self.calculate_distance(feature, self.weight['1']) | |
distances = [t1, t2] | |
result = distances.index(min(distances)) | |
print (t1, " --- ", t2) | |
print (j, " --> RESULT: ", result) | |
# threadable | |
if target == result: | |
print "SAMA" | |
target = str(target) | |
self.weight[target][0] = self.weight[target][0] + (self.alpha * (feature[0] - self.weight[target][0])) | |
self.weight[target][1] = self.weight[target][1] + (self.alpha * (feature[1] - self.weight[target][1])) | |
self.weight[target][2] = self.weight[target][2] + (self.alpha * (feature[2] - self.weight[target][2])) | |
self.weight[target][3] = self.weight[target][3] + (self.alpha * (feature[3] - self.weight[target][3])) | |
elif target != result: | |
print "TIDAK SAMA" | |
target = str(target) | |
self.weight[target][0] = self.weight[target][0] - (self.alpha * (feature[0] - self.weight[target][0])) | |
self.weight[target][1] = self.weight[target][1] - (self.alpha * (feature[1] - self.weight[target][1])) | |
self.weight[target][2] = self.weight[target][2] - (self.alpha * (feature[2] - self.weight[target][2])) | |
self.weight[target][3] = self.weight[target][3] - (self.alpha * (feature[3] - self.weight[target][3])) | |
self.alpha = self.learning_rate * self.alpha | |
time.sleep(0.005) | |
print ("--------------") | |
def predict(self, feature): | |
y_in = self.hebb_rule(feature) | |
y = self.activation.activate(y_in) | |
return y | |
ALPHA = 0.1 | |
LEARNING_RATE = 0.05 | |
EPOCH = 1000 | |
dataset = [ | |
([0, 0, 0, 0], 0), | |
([0, 0, 0, 1], 0), | |
([0, 0, 1, 1], 0), | |
([0, 1, 1, 1], 1), | |
([1, 1, 1, 1], 1), | |
([1, 1, 1, 0], 1), | |
([1, 1, 0, 0], 0), | |
([1, 0, 0, 0], 0), | |
([1, 0, 1, 1], 1) | |
] | |
model = LearningVectorQuantization(alpha=ALPHA, learning_rate=LEARNING_RATE) | |
model.fit(dataset=dataset, epoch=EPOCH) | |
# # print (model.predict([1, 1])) | |
# # print (model.predict([0, 0])) | |
# # print (model.predict([0, 1])) | |
# # print (model.predict([1, 0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment