Skip to content

Commit

Permalink
Modified the function to get w to a substraction of logarithmis instead
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Oct 9, 2016
1 parent 42f8c6a commit 03b2e50
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 113 deletions.
63 changes: 41 additions & 22 deletions connectivity_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
epsilon = 1e-10


def log_epsilon(x):

return np.log(np.maximum(x, epsilon))


def calculate_probability(patterns):
"""
Returns the probability from a list of patterns to be learned
Expand Down Expand Up @@ -36,17 +41,13 @@ def calculate_coactivations(patterns):


def get_w(P, p, diagonal_zero=True):
outer = np.outer(p, p)
P_copy = np.copy(P)

outer[outer < epsilon**2] = epsilon**2
P_copy[P < epsilon] = epsilon**2

w = np.log(P_copy / outer)
outer = np.outer(p, p)

#IPython.embed()
w = log_epsilon(P) - log_epsilon(outer)
if diagonal_zero:
w[np.diag_indices_from(w)] = 0

return w


Expand All @@ -65,20 +66,6 @@ def get_w_pre_post(P, p_pre, p_post, diagonal_zero=True):
return w


def get_w_protocol1(P, p):
p_copy = np.copy(p)
P_copy = np.copy(P)

p_copy[p < epsilon] = epsilon
P_copy[P < epsilon] = epsilon * epsilon

aux = np.outer(p_copy, p_copy)
w = np.log(P_copy / aux)
# IPython.embed()

return w


def get_beta(p):

probability = np.copy(p)
Expand Down Expand Up @@ -122,4 +109,36 @@ def softmax(x, t=1.0, minicolumns=2):
dist = e / np.sum(e, axis=1)[:, np.newaxis]

dist = np.reshape(dist, x_size)
return dist
return dist

################
# Old functions
#################

def get_w_old(P, p, diagonal_zero=True):
outer = np.outer(p, p)
P_copy = np.copy(P)

outer[outer < epsilon**2] = epsilon**2
P_copy[P < epsilon] = epsilon**2

w = np.log(P_copy / outer)

#IPython.embed()
if diagonal_zero:
w[np.diag_indices_from(w)] = 0
return w


def get_w_protocol1(P, p):
p_copy = np.copy(p)
P_copy = np.copy(P)

p_copy[p < epsilon] = epsilon
P_copy[P < epsilon] = epsilon * epsilon

aux = np.outer(p_copy, p_copy)
w = np.log(P_copy / aux)
# IPython.embed()

return w
50 changes: 50 additions & 0 deletions connectivity_visualization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable

from connectivity_functions import get_beta, get_w, get_w_old, softmax
from connectivity_functions import calculate_probability, calculate_coactivations
from data_transformer import build_ortogonal_patterns
from network import BCPNN

np.set_printoptions(suppress=True)
np.set_printoptions(precision=2)

hypercolumns = 3
minicolumns = 3

patterns_dic = build_ortogonal_patterns(hypercolumns, minicolumns)
patterns = list(patterns_dic.values())[0:4]


# The total probability
p = calculate_probability(patterns)
P = calculate_coactivations(patterns)

w = get_w(P, p)
x = np.outer(p, p)
w_aux = get_w_old(P, p)

# Plot it
cmap = 'coolwarm'
aux_max = np.max(np.abs(w))

fig = plt.figure(figsize=(16, 12))
ax1 = fig.add_subplot(121)
# plt.imshow(w, cmap=cmap, interpolation='None', vmin=-aux_max, vmax=aux_max)
im1 = ax1.imshow(w, cmap=cmap, interpolation='None')
divider = make_axes_locatable(ax1)
cax1 = divider.append_axes('right', size='5%', pad=0.05)
fig.colorbar(im1, ax=ax1, cax=cax1)


ax2 = fig.add_subplot(122)
im2 = ax2.imshow(w_aux, cmap=cmap, interpolation='None')
divider = make_axes_locatable(ax2)
cax2 = divider.append_axes('right', size='5%', pad=0.05)
fig.colorbar(im2, ax=ax2, cax=cax2)

plt.show()
81 changes: 0 additions & 81 deletions convergence_testing.py

This file was deleted.

2 changes: 1 addition & 1 deletion notebooks/2016-09-05(vanilla BCPNN and digit recall).ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/2016-10-01(Convergence Properties).ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
Expand Down
18 changes: 11 additions & 7 deletions play.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
beta = get_beta(p)

dt = 0.01
T_simulation = 1.0
T_simulation = 10.0
T_training = 1.0
simulation_time = np.arange(0, T_simulation + dt, dt)
training_time = np.arange(0, T_simulation + dt, dt)
Expand All @@ -38,11 +38,8 @@
g_beta_set = np.arange(0, 22, 2)
g_w_set = np.arange(0, 12, 2)

nn = BCPNN(hypercolumns, minicolumns, beta, w, g_a=0, g_beta=5.0, g_w=3.0, g_I=10.0, prng=prng)
nn = BCPNN(hypercolumns, minicolumns, beta, w, g_a=1, g_beta=1.0, g_w=1.0, g_I=10.0, prng=prng)
nn.randomize_pattern()
history = nn.run_network_simulation(training_time, save=True)
o = history['o']
s = history['s']

if False:
final_states = []
Expand All @@ -54,7 +51,7 @@
final_states.append(nn.o)


if True:
if False:
I = patterns[0]

for i in range(20):
Expand All @@ -69,7 +66,7 @@

# x, y = calculate_convergence_ratios(nn, 10, training_time, patterns)

if False:
if True:
for pattern in reversed(patterns):
I = pattern
nn.randomize_pattern()
Expand All @@ -91,3 +88,10 @@



history = nn.run_network_simulation(simulation_time, save=True)
o = history['o']
s = history['s']

plt.imshow(o, aspect='auto', interpolation='None')
plt.colorbar()
plt.show()
2 changes: 1 addition & 1 deletion test_connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_unit_probabilities(self):

def test_coactivations(self):
test_pattern1 = np.array((1, 0, 1, 0))
test_pattern2 = np.array((0, 1, 0, 1))
test_pattern2 = np.array((0, 1, 0, 1))

patterns = [test_pattern1, test_pattern2]

Expand Down

0 comments on commit 03b2e50

Please sign in to comment.