Skip to content

Commit

Permalink
modified animation, added visualization of simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Aug 19, 2016
1 parent 1edfa2d commit ae918e0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
3 changes: 2 additions & 1 deletion animation_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def animate(i):
image_clock = np.reshape(aux, (6, 10))
im.set_array(image_clock)

ani = animation.FuncAnimation(fig, animate, frames=60, interval=2000)
ani = animation.FuncAnimation(fig, animate, frames=10, interval=2000)
ani.save('clock.mp4', fps=1.0, dpi=200)
# ani.save('clock.gif', writer='imagemagick', fps=1.0, dpi=200)
plt.show()

"""Interval is for the printing animation. So in this case, the animation that will be reproduced
Expand Down
35 changes: 15 additions & 20 deletions digit_recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import matplotlib.cm as cm

from sklearn import datasets
from matplotlib import ticker

from network import BCPNN
from connectivity_functions import get_beta, get_w, softmax
from data_transformer import transform_normal_to_neural_single
from data_transformer import transform_neural_to_normal_single
from connectivity_functions import calculate_probability, calculate_coactivations



digits = datasets.load_digits()

images = digits['images']
Expand All @@ -22,7 +26,7 @@
data[data >= 8] = 1

# Let's get two images and learn them as a pattern
number_of_patterns = 2
number_of_patterns = 4
patterns = []
for i in range(number_of_patterns):
patterns.append(data[i])
Expand All @@ -41,26 +45,12 @@
tau_m = 1.0
G = 1.0

o = np.random.rand(p.size)
# Save initial image
initial_image = np.copy(transform_neural_to_normal_single(o).reshape(8, 8))
m = np.zeros_like(o)

network = BCPNN(beta, w, G=G, tau_m=tau_m)
initial_image = np.copy(transform_neural_to_normal_single(network.o).reshape(8, 8))

def update_system():
global o, s, m
# Update S
s = beta + np.dot(w, o)
# Evolve m, trailing s
m += (dt / tau_m) * (s - m)
# Softmax for m
o = softmax(m, t=(1 / G))
network.update_discrete(T)


for t in range(T):
update_system()

final_image = transform_neural_to_normal_single(o).reshape(8, 8)
final_image = transform_neural_to_normal_single(network.o).reshape(8, 8)

# Plot the two patterns and the final result
gs = gridspec.GridSpec(number_of_patterns, number_of_patterns, wspace=0.1, hspace=0.1)
Expand Down Expand Up @@ -89,6 +79,11 @@ def update_system():

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im11, cax=cbar_ax)
cb = fig.colorbar(im11, cax=cbar_ax)

if False:
tick_locator = ticker.MaxNLocator(nbins=2)
cb.locator = tick_locator
cb.update_ticks()

plt.show()
5 changes: 3 additions & 2 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
import IPython


class BCPNN:
Expand Down Expand Up @@ -28,21 +29,21 @@ def __init__(self, beta, w, o=None, s=None, a=None, z_pre=None, z_post=None,
if o is None:
self.o = np.random.rand(beta.size)
if s is None:
self.s = np.zeros_like(o)
self.s = np.zeros_like(self.o)

if G is None:
self.G = 1.0

if tau_m is None:
self.tau_m = 1.0


def update_discrete(self, N=1):
for n in range(N):
self.s = self.beta + np.dot(self.w, self.o)
self.o = softmax(self.s, t=(1/self.G))

def update_continuous(self, dt=1.0):
# IPython.embed()
self.s += (dt / self.tau_m) * (self.beta + np.dot(self.w, self.o) - self.s)
self.o = softmax(self.s, t=(1/self.G))

Expand Down
50 changes: 43 additions & 7 deletions play.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
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, softmax
from connectivity_functions import calculate_probability, calculate_coactivations
from data_transformer import transform_normal_to_neural_single
Expand All @@ -16,11 +21,42 @@
w = get_w(P, p)
beta = get_beta(p)

network = BCPNN(beta, w)
print(network.o)
network.update_discrete()
print(network.o)
network.update_discrete()
print(network.o)
nn = BCPNN(beta, w)

dt = 0.1
T = 1.0
time = np.arange(0, T + dt, dt)

history_o = np.zeros((time.size, beta.size))
history_s = np.zeros_like(history_o)

for index_t, t in enumerate(time):
nn.update_continuous(dt)
history_o[index_t, :] = nn.o
history_s[index_t, :] = nn.s

x = transform_neural_to_normal_single(nn.o)

# Plotting goes here
gs = gridspec.GridSpec(1, 2)
fig = plt.figure(figsize=(16, 12))

ax1 = fig.add_subplot(gs[0, 0])
im1 = ax1.imshow(history_o.T, cmap='gray', interpolation='nearest')
divider1 = make_axes_locatable(ax1)
cax1 = divider1.append_axes("right", size='5%', pad=0.05)
fig.colorbar(im1, cax=cax1)

ax2 = fig.add_subplot(gs[0, 1])
im2 = ax2.imshow(history_s.T, cmap='gray', interpolation='nearest')
divider2 = make_axes_locatable(ax2)
cax2 = divider2.append_axes('right', size='5%', pad=0.05)
fig.colorbar(im2, cax=cax2)


if False:
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im2, cax=cbar_ax)

x = transform_neural_to_normal_single(network.o)
plt.show()

0 comments on commit ae918e0

Please sign in to comment.