Generating Coherent Patterns of Activity from Chaotic Neural Networks

In [70]:
# /!\ 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('.')
In [71]:
!pip install --force-reinstall git+https://github.com/youqad/Chaotic_Neural_Networks.git#egg=chaotic_neural_networks
Collecting chaotic_neural_networks from git+https://github.com/youqad/Chaotic_Neural_Networks.git#egg=chaotic_neural_networks
  Cloning https://github.com/youqad/Chaotic_Neural_Networks.git to /private/var/folders/6z/mr5_h8cs5knf_vymb7jct8040000gn/T/pip-install-f3ffnqen/chaotic-neural-networks
Building wheels for collected packages: chaotic-neural-networks
  Running setup.py bdist_wheel for chaotic-neural-networks ... done
  Stored in directory: /private/var/folders/6z/mr5_h8cs5knf_vymb7jct8040000gn/T/pip-ephem-wheel-cache-3i5wdfje/wheels/9e/45/0b/b02fc60ca8cc4e9d87a32b43285cb7af8a8f7659aa76cf3a2e
Successfully built chaotic-neural-networks
tensorflow-tensorboard 0.4.0rc3 has requirement bleach==1.5.0, but you'll have bleach 2.1.3 which is incompatible.
tensorflow-tensorboard 0.4.0rc3 has requirement html5lib==0.9999999, but you'll have html5lib 1.0.1 which is incompatible.
Installing collected packages: chaotic-neural-networks
  Found existing installation: chaotic-neural-networks 0.0.1
    Uninstalling chaotic-neural-networks-0.0.1:
      Successfully uninstalled chaotic-neural-networks-0.0.1
Successfully installed chaotic-neural-networks-0.0.1

Recurrent Neural Network A

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:

  • $τ = 10 \text{ ms}$
  • $x_i$ is the neuron's membrane potential
  • Nonzero elements of $J^{GG}, J^{GF}, J^{FG}, J^{FF}$ are drawn independently from Gaussian distributions with zero means and variances equal to the inverses of $p_{GG} N_G, p_{GF}N_F, p_{FG}N_G$ and $p_{FF}N_F$ respectively
  • Rows of $J^{GI}$ and $J^{FI}$ have a single nonzero element drawn from a Gaussian distribution with zero mean and unit variance
  • Elements of $J^{G_z}$ are drawn from a uniform distribution between $-1$ and $1$
  • Nonzero elements of $\textbf{w}$ are set initially either to zero or to values generated by a Gaussian distribution with zero mean and variance $1/(p_zN)$.
In [72]:
from chaotic_neural_networks import utils, networkA

t_max=2400
In [73]:
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
In [74]:
#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())
Out[74]:
In [75]:
#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())
Out[75]:
In [57]:
 
Out[57]:
[<matplotlib.lines.Line2D at 0x11d3118d0>]