Created
June 12, 2019 05:14
-
-
Save ridwanbejo/660d066a80b3ec4d7219d22a824561a1 to your computer and use it in GitHub Desktop.
tf_lvq_3.py
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 tensorflow as tf | |
import numpy as np | |
from random import random | |
from keras.datasets import mnist | |
ALPHA = 0.05 | |
LEARNING_RATE = 0.1*ALPHA | |
EPOCHS = 10 | |
NUM_OF_FEATURES = 100 | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
features = np.asarray([x.flatten() for x in x_train][0:NUM_OF_FEATURES]) | |
targets = y_train[0:NUM_OF_FEATURES] | |
classes = set(targets) | |
weights = [] | |
for x in classes: | |
weights.append(np.asarray([random() for x in range(0, features.shape[1])])) | |
# variables | |
x_feature = tf.placeholder('float64') | |
x_target = tf.placeholder('float64') | |
w = tf.placeholder('float64') | |
chosen_weight = tf.placeholder('float64') | |
alpha = tf.placeholder('float64') | |
feature = tf.placeholder('float64') | |
# operation | |
x_distances = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x_feature, w)))) | |
max_distance = tf.reduce_max(x_distances) | |
new_weight_for_same_result = tf.add( | |
chosen_weight, | |
tf.multiply( | |
alpha, | |
tf.subtract(feature, chosen_weight) | |
) | |
) | |
new_weight_for_different_result = tf.subtract( | |
chosen_weight, | |
tf.multiply( | |
alpha, | |
tf.subtract(feature, chosen_weight) | |
) | |
) | |
def learn(x_feature): | |
# 1. calculate distance for every weight against the feature | |
distances = [sess.run(distance, feed_dict={x_feature: features[i], w: weight}) for weight in weights] | |
# print("DISTANCES: ", distances) | |
chosen_distance = sess.run(max_distance, feed_dict={x_distances: np.asarray(distances)}) | |
# print("MAX DISTANCE: ", chosen_distance) | |
target = distances.index(chosen_distance) + 1 | |
# print("TARGET: ", target) | |
selected_weight = weights[target-1] | |
temp_weight = [] | |
# 2. if target and result is same, do addition to the weight | |
if target == targets[i]: | |
temp_weight = sess.run(new_weight_for_same_result, feed_dict={chosen_weight: selected_weight, alpha: ALPHA, feature: features[i]}) | |
# 3. otherwise, do substraction to the weight | |
elif target != targets[i]: | |
temp_weight = sess.run(new_weight_for_different_result, feed_dict={chosen_weight: selected_weight, alpha: ALPHA, feature: features[i]}) | |
weights[target-1] = np.asarray(temp_weight) | |
return x_feature | |
# initialization | |
init = tf.global_variables_initializer() | |
with tf.Session() as sess: | |
sess.run(init) | |
print(weights[0][0]) | |
for epoch in range(0, EPOCHS): | |
print("EPOCH ", epoch+1) | |
sess.run(tf.map_fn(learn, features), feed_dict={w: weights[0]}) | |
# for i in range(0, len(targets)): | |
# # 1. calculate distance for every weight against the feature | |
# distances = [sess.run(distance, feed_dict={x_feature: features[i], w: weight}) for weight in weights] | |
# # print("DISTANCES: ", distances) | |
# chosen_distance = sess.run(max_distance, feed_dict={x_distances: np.asarray(distances)}) | |
# # print("MAX DISTANCE: ", chosen_distance) | |
# target = distances.index(chosen_distance) + 1 | |
# # print("TARGET: ", target) | |
# selected_weight = weights[target-1] | |
# temp_weight = [] | |
# # 2. if target and result is same, do addition to the weight | |
# if target == targets[i]: | |
# temp_weight = sess.run(new_weight_for_same_result, feed_dict={chosen_weight: selected_weight, alpha: ALPHA, feature: features[i]}) | |
# # 3. otherwise, do substraction to the weight | |
# elif target != targets[i]: | |
# temp_weight = sess.run(new_weight_for_different_result, feed_dict={chosen_weight: selected_weight, alpha: ALPHA, feature: features[i]}) | |
# weights[target-1] = np.asarray(temp_weight) | |
print("--------------") | |
print(weights[0][0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment