<jupyter_start><jupyter_text>Initialize simulator<jupyter_code>import numpy as np
import itertools
from backend.simulation_tools import Simulator
from tasks import flip_flop
import matplotlib.pyplot as plt
%matplotlib inline

params = flip_flop.set_params(N_batch= 64, N_rec=10,
                       input_wait=5, stim_dur=5, quiet_gap=200, N_turns=4,
                       rec_noise=0, stim_noise=0.0,
                       dale_ratio=.8, tau=20, dt=10.)

x,y,mask = flip_flop.build_train_batch(params)

sim = Simulator(params, weights_path="./tasks/weights/flipflop.npz")<jupyter_output><empty_output><jupyter_text>Choose a trial<jupyter_code>trial = x[6]
target = y[6]
plt.plot(range(x.shape[1]), trial, range(x.shape[1]), target)
plt.ylim([-.2, 1.2])
plt.show()<jupyter_output><empty_output><jupyter_text>Add selective time masking, run the trial<jupyter_code>t_connectivity = []

for i in range(x.shape[1]):
    if 4 <= i and i <= 3 or 34 <= i and i <= 33:
        t_connectivity.append(np.zeros_like(sim.W_rec))
    else:
        t_connectivity.append(np.ones_like(sim.W_rec))
outputs, states = sim.run_trial(trial, t_connectivity)<jupyter_output><empty_output><jupyter_text>Plot output of network<jupyter_code>plt.plot(range(x.shape[1]), target, range(x.shape[1]), outputs[:, 0])
plt.ylim([-.2, 1.2])
plt.show()<jupyter_output><empty_output><jupyter_text>Plot states over time, without and with RELU<jupyter_code>for i in range(states.shape[2]):
    plt.plot(range(states.shape[0]), states[:,0,i])
plt.show()
for i in range(states.shape[2]):
    plt.plot(range(states.shape[0]), np.maximum(states[:,0,i], np.zeros_like(states[:,0,i])))
plt.show()<jupyter_output><empty_output><jupyter_text>Plot W_rec and then animate effective W_rec given state and RELU<jupyter_code>plt.pcolor(sim.W_rec)
plt.colorbar()
plt.show()
import matplotlib.animation as animation 
from IPython.display import HTML
from matplotlib import rc

fig = plt.figure()
    
ims = []


for i in range(states.shape[0]):
    Wi = np.matmul(np.absolute(sim.W_rec),sim.dale_rec) * t_connectivity[i]
    for j in range(sim.W_rec.shape[0]):
        if states[i, 0, j] < 0:
            Wi[:, j] = 0
    ims.append((plt.pcolor(Wi, vmin = -.8, vmax = .8, cmap='RdBu_r'),))

plt.colorbar()
rc('animation', html='html5')


animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=3000,
                                   blit=True)
Wi = np.matmul(np.absolute(sim.W_rec),sim.dale_rec) * t_connectivity[i]
for j in range(sim.W_rec.shape[0]):
    if states[300, 0, j] < 0:
        Wi[:, j] = 0
np.linalg.eigvals(Wi)<jupyter_output><empty_output><jupyter_text>Effective input and output matrices<jupyter_code>np.absolute(sim.W_in)
np.matmul(np.absolute(sim.W_out),sim.dale_out)<jupyter_output><empty_output><jupyter_text>Hahnloser Fixed Point Strategy<jupyter_code>permutations = np.array(list(itertools.product([0,1], repeat=10)))
input_vec = np.matmul(np.absolute(sim.W_in), np.array([.1,.1]))
identity = np.diag(np.ones(10))

count = 0
for p in permutations:
    #define active weight matrix
    Wp = np.matmul(np.absolute(sim.W_rec),sim.dale_rec)
    for i in p:
        if i == 0:
            Wp[:,i] = 0
    
    #check for fixed point
    I = np.matmul(np.linalg.inv(identity - Wp), input_vec)
    fixed = True
    for i in range(10):
        if p[i] == 0 and I[i] >= 0:
            fixed = False
        if p[i] == 1 and I[i] < 0:
            fixed = False
            
    if fixed == True:
        count += 1
        print p, I
        
print count<jupyter_output><empty_output><jupyter_text>"Black Box" paper analysis<jupyter_code>def q(x):
    f = (1.0 / np.array(sim.tau)) * (-x + np.matmul(np.maximum(x, np.zeros_like(x)),
                                np.transpose(np.matmul(np.absolute(sim.W_rec),sim.dale_rec))))
    q = .5 * np.sum(np.square(f))
    return q

#+ np.matmul(rnn_in, np.transpose(np.absolute(sim.W_in))))
               
def dFidxj(x, i, j):
    if x[j] > 0:
        d = - 1.0 / sim.tau + (1.0 / sim.tau) * np.matmul(np.absolute(sim.W_rec),sim.dale_rec)[i][j]
    else:
        d = -1.0 / sim.tau
    return d
    
def grad_q(x):
    g = np.zeros_like(x)
    for j in range(len(x)):
        for i in range(len(x)):
            f = (1.0 / np.array(sim.tau)) * (-x + np.matmul(np.maximum(x, np.zeros_like(x)),
                                np.transpose(np.matmul(np.absolute(sim.W_rec),sim.dale_rec))))
            g[j] += dFidxj(x,i,j) * f[i]
    return g
        
                           
def hess_q(x):
    h = np.zeros((len(x), len(x)))
    for i in range(len(x)):
        for j in range(len(x)):
            for k in range(len(x)):
                h[i,j] += dFidxj(x,k,i) * dFidxj(x,k,j)
    return h
from scipy.optimize import minimize

def opt(init):
    res = minimize(q, init, method = 'Newton-CG', jac = grad_q, hess = hess_q,
               tol = 1e-10, options = {'disp':True})
    
    if not res.success:
        print "FAILED TO CONVERGE"
    return res.x<jupyter_output><empty_output><jupyter_text>optimize q given some state as initial condition<jupyter_code>trial = x[4]
outputs, states = sim.run_trial(trial)
test = opt(states[375,0,:])
test<jupyter_output><empty_output><jupyter_text>linear stability by taking eigenvalues of jacobian<jupyter_code>def eig_dF(x):
    J = np.zeros((len(x), len(x)))
    for i in range(len(x)):
        for j in range(len(x)):
            J[i,j] = dFidxj(x,i,j)
    return np.linalg.eigvals(J)
eig_dF(test)

np.linalg.eigvals(sim.W_rec)<jupyter_output><empty_output><jupyter_text>visualization of network with zero input, from initial conditions<jupyter_code>tester = Simulator(params, weights_path="./tasks/weights/flipflop.npz")
tester.init_state = states[175,0,:]

zero_trial = .1 * np.zeros_like(trial)

var_trial = .01 * np.random.normal(trial.shape) 

outputs, states = sim.run_trial(zero_trial, use_input=True)
plt.plot(range(x.shape[1]), outputs[:,0])
plt.ylim([-.2, 1])
plt.show()
for i in range(states.shape[2]):
    plt.plot(range(states.shape[0]), states[:,0,i])
plt.show()
states[40]
out_state = states[40,0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1563779269690519, 'fraction_numerical': 0.02014082200753234, 'mean_word_length': 3.3942446043165466, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '571332', 'license': 'MIT', 'n_tokens_mistral': 2498, 'n_tokens_neox': 2381, 'n_words': 449, 'path': 'notebooks/forward pass.ipynb', 'repo_name': 'dbehrlich/sisyphus2', 'size': '6107'}