# /!\ I'm using Python 3
import plotly
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib import animation, rc
import scipy as scipy
from scipy import signal
plotly.offline.init_notebook_mode(connected=True)
from IPython.core.display import display, HTML, Markdown
# The polling here is to ensure that plotly.js has already been loaded before
# setting display alignment in order to avoid a race condition.
display(HTML(
'<script>'
'var waitForPlotly = setInterval( function() {'
'if( typeof(window.Plotly) !== "undefined" ){'
'MathJax.Hub.Config({ SVG: { font: "STIX-Web" }, displayAlign: "center" });'
'MathJax.Hub.Queue(["setRenderer", MathJax.Hub, "SVG"]);'
'clearInterval(waitForPlotly);'
'}}, 250 );'
'</script>'
))
# Colorscales
def colorscale_list(cmap, number_colors, return_rgb_only=False):
cm = plt.get_cmap(cmap)
colors = [np.array(cm(i/number_colors)) for i in range(1, number_colors+1)]
rgb_colors_plotly = []
rgb_colors_only = []
for i, c in enumerate(colors):
col = 'rgb{}'.format(tuple(255*c[:-1]))
rgb_colors_only.append(col)
rgb_colors_plotly.append([i/number_colors, col])
rgb_colors_plotly.append([(i+1)/number_colors, col])
return rgb_colors_only if return_rgb_only else rgb_colors_plotly
import plotly.figure_factory as ff
def formatted(f):
return format(f, '.2f').rstrip('0').rstrip('.')
!pip install --force-reinstall git+https://github.com/youqad/Chaotic_Neural_Networks.git#egg=chaotic_neural_networks
Network equations for the generator network for each neuron $i ∈ [1, N_G]$:
$$τ \dot{x}_i = -x_i + g_{GG} \sum\limits_{j=1}^{N_G} J_{ij}^{GG} \underbrace{\tanh(x_j)}_{≝ r_j} + g_{G_z} J_i^{G_z} z + g_{GF} \sum\limits_{a=1}^{N_F} J_{ia}^{GF} s_a + \sum\limits_{μ=1}^{N_I} J_{iμ}^{GI} I_μ$$
For the feedback network with $j∈[1, N_F]$ :
$$τ \dot{y}_a = -y_a + g_{FF} \sum\limits_{b=1}^{N_F} J_{ab}^{FF} s_b + g_{FG} \sum\limits_{i=1}^{N_G} J_{ai}^{FG} \underbrace{\tanh(x_i)}_{≝ r_i} + \sum\limits_{μ=1}^{N_I} J_{aμ}^{FI} I_μ$$
where:
from chaotic_neural_networks import utils, networkA
t_max=2400
def animation_training(network, t_max=t_max, lw_f=4, lw_z=1.5, big_steps=50):
dt = network.dt
ts_train, ts_test = np.arange(0, t_max, dt), np.arange(t_max, 2*t_max, dt)
lw_f, lw_z = 4, 1.5
big_steps=50
# TRAIN Phase
f_train = network.f(ts_train)
#------------------------------------------------------------
# set up figure and animation
fig = plt.figure(figsize=(15, 5))
ax1 = fig.add_subplot(121, ylim=(-5.3, 5.3), xlim=(0, t_max))
ax2 = fig.add_subplot(122, ylim=(0, .03), xlim=(0, t_max))
ax2.grid()
ax1.plot(ts_train, f_train, lw=lw_f, color='green')
line1, = ax1.plot([], [], lw=lw_z, color='red')
ax1.set_title('Training phase')
ax1.legend(['$f$', '$z$'])
ax1.set_xlabel('Time (ms)')
ax1.set_ylabel('$f$ and $z$')
line2, = ax2.plot([], [], color='orange', lw=2)
ax2.legend(['$|\dot{w}|$'])
ax2.set_xlabel('Time (ms)')
ax2.set_ylabel('$|\dot{w}|$')
time_text = ax1.text(0.02, 0.95, '', transform=ax1.transAxes)
def init():
"""initialize animation"""
line1.set_data([], [])
line2.set_data([], [])
time_text.set_text('')
return line1, line2, time_text
def animate(i):
"""perform animation step"""
global network
for _ in range(big_steps):
network.step()
line1.set_data(*zip(*network.z_list['train']))
line2.set_data(*zip(*network.w_dot_list))
time_text.set_text('time = {:.1f} ms'.format(network.time_elapsed))
return line1, line2, time_text
frames = int(t_max/(big_steps*dt))-1
ani = animation.FuncAnimation(fig, animate, frames=frames, interval=20, blit=True, init_func=init)
return ani
#plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg'
#print(animation.writers.list())
seed = 2
network = networkA.NetworkA(seed=seed, f=utils.periodic)
ani = animation_training(network)
HTML(ani.to_html5_video())
#plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg'
#print(animation.writers.list())
seed = 2
network = networkA.NetworkA(seed=seed, f=utils.triangle)
ani = animation_training(network)
HTML(ani.to_html5_video())