Skip to content

Commit

Permalink
Modified the test convergence
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Mar 2, 2017
1 parent 3ec013a commit 55fdd0b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 105 deletions.
24 changes: 24 additions & 0 deletions benchmarking/benchmark_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
sys.path.append('../')

import numpy as np
from network import BCPNNFast, NetworkManager

# Patterns parameters
hypercolumns = 4
minicolumns = 20

# Manager properties
dt = 0.001
running_time = 20
values_to_save = []

# Build the network
nn = BCPNNFast(hypercolumns, minicolumns)
nn.k_inner = False
nn.k = 0.0

# Build the manager
manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)
time = np.arange(0, running_time, dt)
manager.run_network(time, I=0)
8 changes: 4 additions & 4 deletions play.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from network import BCPNN, NetworkManager, BCPNNFast, Protocol
from plotting_functions import plot_state_variables_vs_time, plot_network_activity, plot_network_activity_angle
from plotting_functions import plot_adaptation_dynamics, plot_weight_matrix, plot_winning_pattern, plot_sequence
from plotting_functions import plot_adaptation_dynamics, plot_weight_matrix, plot_winning_pattern, plot_sequence
from analysis_functions import calculate_recall_success
# np.set_printoptions(suppress=True, precision=2)

Expand All @@ -20,7 +20,7 @@

# Manager properties
dt = 0.001
T_recall = 5.0
T_recall = 4.0
values_to_save = ['o']


Expand All @@ -32,7 +32,7 @@

# Build the network
nn = BCPNNFast(hypercolumns, minicolumns)
nn.k_inner = True
nn.k_inner = False

# Build the manager
manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)
Expand All @@ -51,7 +51,7 @@
manager.run_network_recall(T_recall=T_recall, T_cue=0.1, I_cue=0, reset=True, empty_history=True)


success = calculate_recall_success(manager, T_recall=T_recall, I_cue=0, T_cue=0.100, n=5, patterns_indexes=patterns)
success = calculate_recall_success(manager, T_recall=T_recall, I_cue=0, T_cue=0.100, n=25, patterns_indexes=patterns)
print(success)


Expand Down
106 changes: 32 additions & 74 deletions test_convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
import numpy.testing as npt
import unittest

from connectivity_functions import get_beta, get_w
from connectivity_functions import calculate_probability, calculate_coactivations
from data_transformer import build_ortogonal_patterns
from convergence_functions import calculate_convergence_ratios
from network import BCPNN
from analysis_functions import calculate_recall_success
from network import BCPNNFast, NetworkManager, Protocol


class TestConvergence(unittest.TestCase):
Expand All @@ -17,87 +14,48 @@ class TestConvergence(unittest.TestCase):

def test_basic_convergence(self):
"""
This will test convergence for the most basic
configuration. Only three parameters and no-adaptation
"""

hypercolumns = 10
minicolumns = 10

patterns_dic = build_ortogonal_patterns(hypercolumns, minicolumns)
patterns = list(patterns_dic.values())

P = calculate_coactivations(patterns)
p = calculate_probability(patterns)

w = get_w(P, p)
beta = get_beta(p)

dt = 0.001
T = 1
time = np.arange(0, T + dt, dt)

g_a = 0 # No adaptation
g_beta = 1.0 # No bias gain
g_w = 1.0 # No weight gain
prng = np.random.RandomState(seed=0)

nn = BCPNN(hypercolumns, minicolumns, beta, w, p_pre=p, p_post=p, p_co=P,
g_a=g_a, g_beta=g_beta, g_w=g_w, prng=prng)

# Run the network 50 times
N = 50
fraction_of_convergence, fraction_of_well_behaved = calculate_convergence_ratios(nn, N, time, patterns)

self.assertAlmostEqual(first=fraction_of_convergence, second=1.0)
self.assertAlmostEqual(first=fraction_of_well_behaved, second=1.0)

def test_clamping_vs_bias_convergence_to_clamping(self):
"""
This test that the BCPNN converges to the clamped value no matter the value
of the biases of the adaptation, weights or bias.
This tests a simple network for recall percentage.
"""

# Patterns parameters
hypercolumns = 4
minicolumns = 4

patterns_dic = build_ortogonal_patterns(hypercolumns, minicolumns)
patterns = list(patterns_dic.values())
minicolumns = 20

P = calculate_coactivations(patterns)
p = calculate_probability(patterns)
# Manager properties
dt = 0.001
T_recall = 4.0
values_to_save = ['o']

w = get_w(P, p)
beta = get_beta(p)
# Protocol
training_time = 0.1
inter_sequence_interval = 2.0
inter_pulse_interval = 0.0
epochs = 1

dt = 0.01
T_simulation = 1.0
simulation_time = np.arange(0, T_simulation + dt, dt)
# Build the network
nn = BCPNNFast(hypercolumns, minicolumns)
nn.k_inner = False

prng = np.random.RandomState(seed=0)
# Build the manager
manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

g_a_set = np.arange(0, 110, 10)
g_beta_set = np.arange(0, 5, 0.5)
g_w_set = np.arange(0, 3.2, 0.2)
# Build the protocol for
patterns = [0, 1, 2, 3, 4]
protocol = Protocol()
protocol.simple_protocol(patterns_indexes=patterns, training_time=training_time,
inter_pulse_interval=inter_pulse_interval,
inter_sequence_interval=inter_sequence_interval,
epochs=epochs)

# Pattern to clamp
g_I = 20.0
I = np.array((1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1))
# Train
manager.run_network_protocol(protocol, verbose=False, values_to_save_epoch=None, reset=True, empty_history=True)

# Test the error
for index_1, g_a in enumerate(g_a_set):
for index_2, g_beta in enumerate(g_beta_set):
for index_3, g_w in enumerate(g_w_set):
nn = BCPNN(hypercolumns, minicolumns, beta, w, p_pre=p, p_post=p, p_co=P,
g_a=g_a, g_beta=g_beta, g_w=g_w, g_I=g_I, prng=prng, k=0)
manager.run_network_recall(T_recall=T_recall, T_cue=0.1, I_cue=0, reset=True, empty_history=True)

nn.randomize_pattern()
success = calculate_recall_success(manager, T_recall=T_recall, I_cue=0, T_cue=0.100, n=25,
patterns_indexes=patterns)

# This is the training
nn.run_network_simulation(simulation_time, I=I)
final = nn.o
point_error = np.linalg.norm(I - final)
self.assertAlmostEqual(point_error, 0)
self.assertAlmostEqual(success, 100.0)

if __name__ == '__main__':
unittest.main()
27 changes: 0 additions & 27 deletions weight_script.py

This file was deleted.

0 comments on commit 55fdd0b

Please sign in to comment.