Skip to content

Commit

Permalink
Modified the construction of patterns to manage the protocols by numb…
Browse files Browse the repository at this point in the history
…ers and not by patterns, working exampleworking examples is working
  • Loading branch information
h-mayorquin committed Feb 10, 2017
1 parent a4a7d8a commit c5c1e5b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 84 deletions.
16 changes: 10 additions & 6 deletions analysis_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ def calculate_angle_from_history(manager):
in time times the number of pattern stores
"""
history = manager.history
patterns = manager.patterns
patterns_dic = manager.patterns_dic
stored_pattern_indexes = manager.stored_patterns_indexes

o = history['o']

distances = np.zeros((o.shape[0], manager.nn.minicolumns))

for index, state in enumerate(o):
# Obtain the dot product between the state of the network at each point in time and each pattern
nominator = [np.dot(state, pattern) for pattern in patterns]
nominator = [np.dot(state, patterns_dic[pattern_index]) for pattern_index in stored_pattern_indexes]
# Obtain the norm of both the state and the patterns to normalize
denominator = [np.linalg.norm(state) * np.linalg.norm(pattern) for pattern in patterns]
denominator = [np.linalg.norm(state) * np.linalg.norm(patterns_dic[pattern_index])
for pattern_index in stored_pattern_indexes]

# Get the angles and store them
dis = [a / b for (a, b) in zip(nominator, denominator)]
distances[index, :len(patterns)] = dis
distances[index, stored_pattern_indexes] = dis

return distances

Expand Down Expand Up @@ -78,7 +82,7 @@ def calculate_patterns_timings(winning_patterns, dt, remove=0):
return patterns_timings


def calculate_recall_sucess(manager, T_recalling, I_cue, T_cue, n, pattern):
def calculate_recall_success(manager, T_recalling, I_cue, T_cue, n, patterns_indexes):

n_patterns = manager.n_patterns
successes = 0
Expand All @@ -90,7 +94,7 @@ def calculate_recall_sucess(manager, T_recalling, I_cue, T_cue, n, pattern):
timings = calculate_patterns_timings(winning, manager.dt, remove=0)
pattern_sequence = [x[0] for x in timings]

if pattern_sequence[:n_patterns] == pattern:
if pattern_sequence[:n_patterns] == patterns_indexes:
successes += 1

success_rate = successes * 100.0/ n
Expand Down
1 change: 1 addition & 0 deletions data_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def build_ortogonal_patterns(hypercolumns, minicolumns):
"""

patterns = {}
patterns[None] = None

for pattern_number in range(minicolumns):
patterns[pattern_number] = produce_pattern(pattern_number, hypercolumns, minicolumns)
Expand Down
30 changes: 15 additions & 15 deletions network.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from connectivity_functions import softmax, get_w_pre_post, get_beta, log_epsilon
from data_transformer import build_ortogonal_patterns
import IPython


Expand Down Expand Up @@ -379,7 +380,9 @@ def __init__(self, nn=None, dt=0.001, values_to_save=[]):

# Trained patterns
self.n_patterns = None
self.patterns = None
self.patterns_dic = build_ortogonal_patterns(self.nn.hypercolumns, self.nn.minicolumns)
self.patterns = list(self.patterns_dic)
self.stored_patterns_indexes = None

def get_saving_dictionary(self, values_to_save):
"""
Expand Down Expand Up @@ -486,7 +489,7 @@ def run_network(self, time=None, I=None):
if I is None:
self.nn.I = np.zeros_like(self.nn.o)
else:
self.nn.I = I
self.nn.I = self.patterns_dic[I]

# Create a vector of noise
if self.nn.sigma < self.nn.epsilon:
Expand Down Expand Up @@ -524,14 +527,12 @@ def run_network_protocol(self, protocol, verbose=True, values_to_save_epoch=None
if reset:
self.nn.reset_values(keep_connectivity=True)

patterns = protocol.patterns

times = protocol.times_sequence
patterns_sequence = protocol.patterns_sequence
learning_constants = protocol.learning_constants_sequence # The values of Kappa

self.patterns = patterns
self.n_patterns = len(patterns)
self.stored_patterns_indexes = protocol.patterns_indexes
self.n_patterns = len(protocol.patterns_indexes)
total_time = 0

epoch_history = {}
Expand All @@ -545,10 +546,10 @@ def run_network_protocol(self, protocol, verbose=True, values_to_save_epoch=None
epoch_history[quantity] = []

epochs = 0
for time, pattern, k in zip(times, patterns_sequence, learning_constants):
for time, pattern_index, k in zip(times, patterns_sequence, learning_constants):

# End of the epoch
if pattern == epoch_end_string:
if pattern_index == epoch_end_string:
# Store the values at the end of the epoch
if values_to_save_epoch:
self.append_history(epoch_history, saving_dictionary_epoch)
Expand All @@ -561,7 +562,7 @@ def run_network_protocol(self, protocol, verbose=True, values_to_save_epoch=None
else:
self.nn.k = k
running_time = np.arange(0, time, self.dt)
self.run_network(time=running_time, I=pattern)
self.run_network(time=running_time, I=pattern_index)
total_time += time

# Record the total time
Expand Down Expand Up @@ -605,18 +606,18 @@ class Protocol:

def __init__(self):

self.patterns = None
self.patterns_indexes = None
self.patterns_sequence = None
self.times_sequence = None
self.learning_constants_sequence = None
self.epochs = None

def simple_protocol(self, patterns, training_time=1.0, inter_pulse_interval=0.0,
def simple_protocol(self, patterns_indexes, training_time=1.0, inter_pulse_interval=0.0,
inter_sequence_interval=1.0, epochs=1):
"""
The simples protocol to train a sequence
:param patterns: All the patterns that will be train
:param patterns_indexes: All the indexes patterns that will be train
:param training_time: How long to present the pattern
:param inter_pulse_interval: Time between each pattern
:param inter_sequence_interval: Time between each repetition of the sequence
Expand All @@ -625,15 +626,15 @@ def simple_protocol(self, patterns, training_time=1.0, inter_pulse_interval=0.0,

epsilon = 1e-10
self.epochs = epochs
self.patterns = patterns
self.patterns_indexes = patterns_indexes

patterns_sequence = []
times_sequence = []
learning_constants_sequence = []

for i in range(epochs):
# Let's fill the times
for pattern in patterns:
for pattern in patterns_indexes:
# This is when the pattern is training
patterns_sequence.append(pattern)
times_sequence.append(training_time)
Expand Down Expand Up @@ -671,7 +672,6 @@ def cross_protocol(self, chain, training_time=1.0, inter_sequence_interval=1.0,

self.epochs = epochs
aux = [pattern for patterns in chain for pattern in patterns] # Neat double iteration
self.patterns = []

# Remove the duplicates
for pattern in aux:
Expand Down
Loading

0 comments on commit c5c1e5b

Please sign in to comment.