Skip to content

Commit

Permalink
Update run.py
Browse files Browse the repository at this point in the history
  • Loading branch information
farizrahman4u committed Jan 24, 2016
1 parent a832d8e commit d6ace8e
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions neuralkey/run.py
Expand Up @@ -14,44 +14,53 @@
update_rules = ['hebbian', 'anti_hebbian', 'random_walk']
update_rule = update_rules[0]

#Create 2 machines
#Create 3 machines : Alice, Bob and Eve. Eve will try to intercept the communication between
#Alice and Bob.
print("Creating machines : k=" + str(k) + ", n=" + str(n) + ", l=" + str(n))
print("Using " + update_rule + " update rule.")
machine1 = Machine(k, n, l)
machine2 = Machine(k, n, l)
Alice = Machine(k, n, l)
Bob = Machine(k, n, l)
Eve = Machine(k, n, l)

#Random number generator
def random():
return np.random.randint(-l, l + 1, [k, n])

#Function to evaluate the synchronization score between two machines.
def sync_score(m1, m2):
return 1.0 - np.average(1.0 * np.abs(m1.W - m2.W)/(2 * l - 1))
return 1.0 - np.average(1.0 * np.abs(m1.W - m2.W)/(2 * l))

#Synchronize weights

sync = False # Flag to check if weights are sync
nb_updates = 0 # Update counter
nb_eve_updates = 0 # To count the number of times eve updated
start_time = time.time() # Start time
sync_history = [] # to store the sync score after every update

while(not sync):

X = random() # Create random vector of dimensions [k, n]

tauA = machine1(X) # Get output from machine1
tauB = machine2(X) # Get output from machine2
tauA = Alice(X) # Get output from Alice
tauB = Bob(X) # Get output from Bob
tauE = Eve(X) # Get output from Eve

machine1.update(tauB, update_rule) # Update machine1 with machine2's output
machine2.update(tauA, update_rule) # Update machine2 with machine1's output
Alice.update(tauB, update_rule) # Update Alice with Bob's output
Bob.update(tauA, update_rule) # Update Bob with Alice's output

#Eve would update only if tauA = tauB = tauE
if tauA == tauB == tauE:
Eve.update(tauA, update_rule)
nb_eve_updates += 1

nb_updates += 1

score = 100 * sync_score(machine1, machine2) # Calculate the synchronization of the 2 machines
score = 100 * sync_score(Alice, Bob) # Calculate the synchronization of the 2 machines

sync_history.append(score) # Add sync score to history, so that we can plot a graph later.

sys.stdout.write('\r' + "Synchronization = " + str(int(score)) + "% / Updates = " + str(nb_updates))
sys.stdout.write('\r' + "Synchronization = " + str(int(score)) + "% / Updates = " + str(nb_updates) + " / Eve's updates = " + str(nb_eve_updates))
if score == 100: # If synchronization score is 100%, set sync flag = True
sync = True

Expand All @@ -63,6 +72,13 @@ def sync_score(m1, m2):
print ('Time taken = ' + str(time_taken)+ " seconds.")
print ('Updates = ' + str(nb_updates) + ".")

#See if Eve got what she wanted:
eve_score = 100 * int(sync_score(Alice, Eve))
if eve_score > 100:
print("Oops! Nosy Eve synced her machine with Alice's and Bob's !")
else:
print("Eve's machine is only " + str(eve_score) + " % " + "synced with Alice's and Bob's and she did " + str(nb_eve_updates) + " updates.")

#Plot graph
import matplotlib.pyplot as mpl
mpl.plot(sync_history)
Expand Down

0 comments on commit d6ace8e

Please sign in to comment.