def objective(hyperparams): taus_ens = [hyperparams['tau_rise'], hyperparams['ens']] taus_x = [hyperparams['tau_rise'], hyperparams['x']] h_ens = DoubleExp(taus_ens[0], taus_ens[1]) h_x = DoubleExp(taus_x[0], taus_x[1]) A = h_ens.filt(np.load('data/%s_ens.npz'%hyperparams['name'])['ens'], dt=hyperparams['dt_sample']) x = h_x.filt(np.load('data/%s_x.npz'%hyperparams['name'])['x'], dt=hyperparams['dt_sample']) if dt != dt_sample: A = A[::int(dt_sample/dt)] x = x[::int(dt_sample/dt)] d_ens = Lstsq()(A, x)[0] xhat = np.dot(A, d_ens) loss = rmse(xhat, x) loss += penalty * taus_ens[1] return {'loss': loss, 'taus_ens': taus_ens, 'taus_x': taus_x, 'd_ens': d_ens, 'status': STATUS_OK}
def decode(spikes, targets, nTrain, dt=0.001, dtSample=0.001, reg=1e-3, penalty=0, evals=100, name="default", tauRiseMax=3e-2, tauFallMax=3e-1): d, tauRise, tauFall = dfOpt(spikes, targets, nTrain, name=name, evals=evals, reg=reg, penalty=penalty, dt=dt, dtSample=dtSample, tauRiseMax=tauRiseMax, tauFallMax=tauFallMax) print("tauRise: %.3f, tauFall: %.3f" % (tauRise, tauFall)) f = DoubleExp(tauRise, tauFall) A = np.zeros((0, spikes.shape[2])) Y = np.zeros((0, targets.shape[2])) for n in range(nTrain): A = np.append(A, f.filt(spikes[n], dt=dt), axis=0) Y = np.append(Y, targets[n], axis=0) X = np.dot(A, d) error = rmse(X, Y) d = d.reshape((-1, targets.shape[2])) return d, f, tauRise, tauFall, X, Y, error
def objective(hyperparams): tauRise = hyperparams['tauRise'] tauFall = hyperparams['tauFall'] dt = hyperparams['dt'] dtSample = hyperparams['dtSample'] f = DoubleExp(tauRise, tauFall) spikes = np.load('data/%s_spikes.npz' % hyperparams['name'])['spikes'] targets = np.load('data/%s_target.npz' % hyperparams['name'])['target'] A = np.zeros((0, spikes.shape[2])) Y = np.zeros((0, targets.shape[2])) for n in range(hyperparams['nTrain']): A = np.append(A, f.filt(spikes[n], dt=dt), axis=0) Y = np.append(Y, targets[n], axis=0) if dt != dtSample: A = A[::int(dtSample / dt)] Y = Y[::int(dtSample / dt)] d, _ = LstsqL2(reg=hyperparams['reg'])(A, Y) X = np.dot(A, d) loss = rmse(X, Y) loss += penalty * (10 * tauRise + tauFall) return { 'loss': loss, 'd': d, 'tauRise': tauRise, 'tauFall': tauFall, 'status': STATUS_OK }
def objective(hyperparams): taus_ens = [hyperparams['tau_rise'], hyperparams['tau_fall']] h_ens = DoubleExp(taus_ens[0], taus_ens[1]) A = h_ens.filt(np.load('data/%s_ens.npz'%hyperparams['name'])['ens'], dt=hyperparams['dt']) x = np.load('data/%s_x.npz'%hyperparams['name'])['x'] if dt != dt_sample: A = A[::int(dt_sample/dt)] x = x[::int(dt_sample/dt)] if hyperparams['reg']: d_ens = LstsqL2(reg=hyperparams['reg'])(A, x)[0] else: d_ens = Lstsq()(A, x)[0] xhat = np.dot(A, d_ens) loss = rmse(xhat, x) loss += penalty * (10*taus_ens[0] + taus_ens[1]) return {'loss': loss, 'taus_ens': taus_ens, 'd_ens': d_ens, 'status': STATUS_OK}
def run(n_neurons=10000, neuron_type=LIF(), t_train=200, t=200, f=DoubleExp(1e-3, 1e-1), dt=0.001, dt_sample=0.003, tt=1.0, seed=0, smooth=30, reg=1e-1, penalty=0, df_evals=20, load_fd=False): d_ens = np.zeros((n_neurons, 3)) f_ens = f if load_fd: load = np.load(load_fd) d_ens = load['d_ens'] taus_ens = load['taus_ens'] f_ens = DoubleExp(taus_ens[0], taus_ens[1]) d_ens_gauss = load['d_ens_gauss'] else: print('Optimizing ens filters and decoders') data = go(d_ens, f_ens, neuron_type=neuron_type, n_neurons=n_neurons, L=True, t=t_train, f=f, dt=dt, dt_sample=dt_sample, seed=seed) d_ens, f_ens, taus_ens = df_opt(data['x'], data['ens'], f, df_evals=df_evals, reg=reg, penalty=penalty, dt=dt_sample, dt_sample=dt_sample, name='lorenz_%s'%neuron_type) all_targets_gauss = gaussian_filter1d(data['x'], sigma=smooth, axis=0) all_spikes_gauss = gaussian_filter1d(data['ens'], sigma=smooth, axis=0) d_ens_gauss = nengo.solvers.LstsqL2(reg=reg)(all_spikes_gauss, all_targets_gauss)[0] np.savez('data/lorenz_%s_fd.npz'%neuron_type, d_ens=d_ens, taus_ens=taus_ens, d_ens_gauss=d_ens_gauss) f_times = np.arange(0, 1, 0.0001) fig, ax = plt.subplots() ax.plot(f_times, f.impulse(len(f_times), dt=0.0001), label=r"$f^x, \tau_1=%.3f, \tau_2=%.3f$" %(-1./f.poles[0], -1./f.poles[1])) ax.plot(f_times, f_ens.impulse(len(f_times), dt=0.0001), label=r"$f^{ens}, \tau_1=%.3f, \tau_2=%.3f, d: %s/%s$" %(-1./f_ens.poles[0], -1./f_ens.poles[1], np.count_nonzero(d_ens), n_neurons)) ax.set(xlabel='time (seconds)', ylabel='impulse response', ylim=((0, 10))) ax.legend(loc='upper right') plt.tight_layout() plt.savefig("plots/lorenz_%s_filters_ens.pdf"%neuron_type) tar = f.filt(data['x'], dt=dt_sample) a_ens = f_ens.filt(data['ens'], dt=dt_sample) ens = np.dot(a_ens, d_ens) z_tar_peaks, _ = find_peaks(tar[:,2], height=0) # gives time indices of z-component-peaks z_ens_peaks, _ = find_peaks(ens[:,2], height=0) fig = plt.figure() ax = fig.add_subplot(121, projection='3d') ax2 = fig.add_subplot(122, projection='3d') ax.plot(*tar.T, linewidth=0.25) # ax.scatter(*tar[z_tar_peaks].T, color='r', s=1) ax2.plot(*ens.T, linewidth=0.25) # ax2.scatter(*ens[z_ens_peaks].T, color='r', s=1, marker='v') ax.set(xlabel="x", ylabel="y", zlabel="z", xlim=((-20, 20)), ylim=((-10, 30)), zlim=((0, 40))) ax.xaxis.pane.fill = False ax.yaxis.pane.fill = False ax.zaxis.pane.fill = False ax.xaxis.pane.set_edgecolor('w') ax.yaxis.pane.set_edgecolor('w') ax.zaxis.pane.set_edgecolor('w') ax.grid(False) ax2.set(xlabel="x", ylabel="y", zlabel="z", xlim=((-20, 20)), ylim=((-10, 30)), zlim=((0, 40))) ax2.xaxis.pane.fill = False ax2.yaxis.pane.fill = False ax2.zaxis.pane.fill = False ax2.xaxis.pane.set_edgecolor('w') ax2.yaxis.pane.set_edgecolor('w') ax2.zaxis.pane.set_edgecolor('w') ax2.grid(False) plt.savefig("plots/lorenz_%s_train_3D.pdf"%neuron_type) fig, (ax1, ax2, ax3) = plt.subplots(1, 3) ax1.plot(tar[:,0], tar[:,1], linestyle="--", linewidth=0.25) ax2.plot(tar[:,1], tar[:,2], linestyle="--", linewidth=0.25) ax3.plot(tar[:,0], tar[:,2], linestyle="--", linewidth=0.25) # ax2.scatter(tar[z_tar_peaks, 1], tar[z_tar_peaks, 2], s=3, color='r') # ax3.scatter(tar[z_tar_peaks, 0], tar[z_tar_peaks, 2], s=3, color='g') ax1.plot(ens[:,0], ens[:,1], linewidth=0.25) ax2.plot(ens[:,1], ens[:,2], linewidth=0.25) ax3.plot(ens[:,0], ens[:,2], linewidth=0.25) # ax2.scatter(ens[z_ens_peaks, 1], ens[z_ens_peaks, 2], s=3, color='r', marker='v') # ax3.scatter(ens[z_ens_peaks, 0], ens[z_ens_peaks, 2], s=3, color='g', marker='v') ax1.set(xlabel='x', ylabel='y') ax2.set(xlabel='y', ylabel='z') ax3.set(xlabel='x', ylabel='z') plt.tight_layout() plt.savefig("plots/lorenz_%s_train_pairwise.pdf"%neuron_type) plt.close('all') # Plot tent map and fit the data to a gaussian print('Plotting tent map') trans = int(tt/dt) tar_gauss = gaussian_filter1d(data['x'][trans:], sigma=smooth, axis=0) a_ens_gauss = gaussian_filter1d(data['ens'][trans:], sigma=smooth, axis=0) ens_gauss = np.dot(a_ens_gauss, d_ens_gauss) z_tar_peaks = find_peaks(tar_gauss[:,2], height=0)[0][1:] z_tar_values_horz = np.ravel(tar_gauss[z_tar_peaks, 2][:-1]) z_tar_values_vert = np.ravel(tar_gauss[z_tar_peaks, 2][1:]) z_ens_peaks = find_peaks(ens_gauss[:,2], height=0)[0][1:] z_ens_values_horz = np.ravel(ens_gauss[z_ens_peaks, 2][:-1]) z_ens_values_vert = np.ravel(ens_gauss[z_ens_peaks, 2][1:]) # def gaussian(x, mu, sigma, mag): # return mag * np.exp(-0.5*(np.square((x-mu)/sigma))) # p0 = [36, 2, 40] # param_ens, _ = curve_fit(gaussian, z_ens_values_horz, z_ens_values_vert, p0=p0) # param_tar, _ = curve_fit(gaussian, z_tar_values_horz, z_tar_values_vert, p0=p0) # horzs_tar = np.linspace(np.min(z_tar_values_horz), np.max(z_tar_values_horz), 100) # gauss_tar = gaussian(horzs_tar, param_tar[0], param_tar[1], param_tar[2]) # horzs_ens = np.linspace(np.min(z_ens_values_horz), np.max(z_ens_values_horz), 100) # gauss_ens = gaussian(horzs_ens, param_ens[0], param_ens[1], param_ens[2]) # error = entropy(gauss_ens, gauss_tar) fig, ax = plt.subplots() ax.scatter(z_tar_values_horz, z_tar_values_vert, alpha=0.5, color='r', label='target') # ax.plot(horzs_tar, gauss_tar, color='r', linestyle='--', label='target fit') ax.scatter(z_ens_values_horz, z_ens_values_vert, alpha=0.5, color='b', label='ens') # ax.plot(horzs_ens, gauss_ens, color='b', linestyle='--', label='ens fit') ax.set(xlabel=r'$\mathrm{max}_n (z)$', ylabel=r'$\mathrm{max}_{n+1} (z)$')#, title='error=%.5f'%error) plt.legend(loc='upper right') plt.savefig("plots/lorenz_%s_train_tent.pdf"%(neuron_type)) print("testing") data = go(d_ens, f_ens, neuron_type=neuron_type, n_neurons=n_neurons, L=False, t=t, f=f, dt=dt, dt_sample=dt_sample, seed=seed) tar = f.filt(data['x'], dt=dt_sample) a_ens = f_ens.filt(data['ens'], dt=dt_sample) ens = np.dot(a_ens, d_ens) z_tar_peaks, _ = find_peaks(tar[:,2], height=0) # gives time indices of z-component-peaks z_ens_peaks, _ = find_peaks(ens[:,2], height=0) fig = plt.figure() ax = fig.add_subplot(121, projection='3d') ax2 = fig.add_subplot(122, projection='3d') ax.plot(*tar.T, linewidth=0.25) # ax.scatter(*tar[z_tar_peaks].T, color='r', s=1) ax2.plot(*ens.T, linewidth=0.25) # ax2.scatter(*ens[z_ens_peaks].T, color='r', s=1, marker='v') ax.set(xlabel="x", ylabel="y", zlabel="z", xlim=((-20, 20)), ylim=((-10, 30)), zlim=((0, 40))) ax.xaxis.pane.fill = False ax.yaxis.pane.fill = False ax.zaxis.pane.fill = False ax.xaxis.pane.set_edgecolor('w') ax.yaxis.pane.set_edgecolor('w') ax.zaxis.pane.set_edgecolor('w') ax.grid(False) ax2.set(xlabel="x", ylabel="y", zlabel="z", xlim=((-20, 20)), ylim=((-10, 30)), zlim=((0, 40))) ax2.xaxis.pane.fill = False ax2.yaxis.pane.fill = False ax2.zaxis.pane.fill = False ax2.xaxis.pane.set_edgecolor('w') ax2.yaxis.pane.set_edgecolor('w') ax2.zaxis.pane.set_edgecolor('w') ax2.grid(False) plt.savefig("plots/lorenz_%s_test_3D.pdf"%neuron_type) fig, (ax1, ax2, ax3) = plt.subplots(1, 3) ax1.plot(tar[:,0], tar[:,1], linestyle="--", linewidth=0.25) ax2.plot(tar[:,1], tar[:,2], linestyle="--", linewidth=0.25) ax3.plot(tar[:,0], tar[:,2], linestyle="--", linewidth=0.25) # ax2.scatter(tar[z_tar_peaks, 1], tar[z_tar_peaks, 2], s=3, color='r') # ax3.scatter(tar[z_tar_peaks, 0], tar[z_tar_peaks, 2], s=3, color='g') ax1.plot(ens[:,0], ens[:,1], linewidth=0.25) ax2.plot(ens[:,1], ens[:,2], linewidth=0.25) ax3.plot(ens[:,0], ens[:,2], linewidth=0.25) # ax2.scatter(ens[z_ens_peaks, 1], ens[z_ens_peaks, 2], s=3, color='r', marker='v') # ax3.scatter(ens[z_ens_peaks, 0], ens[z_ens_peaks, 2], s=3, color='g', marker='v') ax1.set(xlabel='x', ylabel='y') ax2.set(xlabel='y', ylabel='z') ax3.set(xlabel='x', ylabel='z') plt.tight_layout() plt.savefig("plots/lorenz_%s_test_pairwise.pdf"%neuron_type) plt.close('all') # Plot tent map and fit the data to a gaussian print('Plotting tent map') trans = int(tt/dt) tar_gauss = gaussian_filter1d(data['x'][trans:], sigma=smooth, axis=0) a_ens_gauss = gaussian_filter1d(data['ens'][trans:], sigma=smooth, axis=0) ens_gauss = np.dot(a_ens_gauss, d_ens_gauss) z_tar_peaks = find_peaks(tar_gauss[:,2], height=0)[0][1:] z_tar_values_horz = np.ravel(tar_gauss[z_tar_peaks, 2][:-1]) z_tar_values_vert = np.ravel(tar_gauss[z_tar_peaks, 2][1:]) z_ens_peaks = find_peaks(ens_gauss[:,2], height=0)[0][1:] z_ens_values_horz = np.ravel(ens_gauss[z_ens_peaks, 2][:-1]) z_ens_values_vert = np.ravel(ens_gauss[z_ens_peaks, 2][1:]) # def gaussian(x, mu, sigma, mag): # return mag * np.exp(-0.5*(np.square((x-mu)/sigma))) # p0 = [36, 2, 40] # param_ens, _ = curve_fit(gaussian, z_ens_values_horz, z_ens_values_vert, p0=p0) # param_tar, _ = curve_fit(gaussian, z_tar_values_horz, z_tar_values_vert, p0=p0) # horzs_tar = np.linspace(np.min(z_tar_values_horz), np.max(z_tar_values_horz), 100) # gauss_tar = gaussian(horzs_tar, param_tar[0], param_tar[1], param_tar[2]) # horzs_ens = np.linspace(np.min(z_ens_values_horz), np.max(z_ens_values_horz), 100) # gauss_ens = gaussian(horzs_ens, param_ens[0], param_ens[1], param_ens[2]) # error = entropy(gauss_ens, gauss_tar) fig, ax = plt.subplots() ax.scatter(z_tar_values_horz, z_tar_values_vert, alpha=0.5, color='r', label='target') # ax.plot(horzs_tar, gauss_tar, color='r', linestyle='--', label='target fit') ax.scatter(z_ens_values_horz, z_ens_values_vert, alpha=0.5, color='b', label='ens') # ax.plot(horzs_ens, gauss_ens, color='b', linestyle='--', label='ens fit') ax.set(xlabel=r'$\mathrm{max}_n (z)$', ylabel=r'$\mathrm{max}_{n+1} (z)$')#, title='error=%.5f'%error) plt.legend(loc='upper right') plt.savefig("plots/lorenz_%s_test_tent.pdf"%(neuron_type))
def run(NPre=300, N=100, t=20, tTrans=2, nTrain=1, nEnc=10, nTest=10, neuron_type=LIF(), dt=0.001, f=DoubleExp(1e-3, 1e-1), fS=DoubleExp(1e-3, 1e-1), freq=1, muFreq=1.0, sigmaFreq=0.1, reg=1e-1, tauRiseMax=5e-2, tDrive=0.2, base=False, load=False, file=None): print('\nNeuron Type: %s'%neuron_type) rng = np.random.RandomState(seed=0) if load: d1 = np.load(file)['d1'] tauRise1 = np.load(file)['tauRise1'] tauFall1 = np.load(file)['tauFall1'] f1 = DoubleExp(tauRise1, tauFall1) else: print('readout decoders for pre') spikes = np.zeros((nTrain, int(t/0.001), NPre)) targets = np.zeros((nTrain, int(t/0.001), 2)) for n in range(nTrain): data = go(NPre=NPre, N=N, t=t, dt=0.001, f=f, fS=fS, neuron_type=LIF(), freq=freq, phase=2*np.pi*(n/nTrain)) spikes[n] = data['pre'] targets[n] = f.filt(data['inpt'], dt=0.001) d1, f1, tauRise1, tauFall1, X, Y, error = decode(spikes, targets, nTrain, dt=0.001, tauRiseMax=tauRiseMax, name="oscillateNew") np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1) times = np.arange(0, t*nTrain, 0.001) plotState(times, X, Y, error, "oscillateNew", "%s_pre"%neuron_type, t*nTrain) if load: e1 = np.load(file)['e1'] elif isinstance(neuron_type, Bio): print("ens1 encoders") e1 = np.zeros((NPre, N, 2)) for n in range(nEnc): data = go(d1=d1, e1=e1, f1=f1, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, freq=freq, phase=2*np.pi*(n/nEnc), l1=True) e1 = data['e1'] np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1) plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'], "oscillateNew", "ens") else: e1 = np.zeros((NPre, N, 2)) if load: d2 = np.load(file)['d2'] tauRise2 = np.load(file)['tauRise2'] tauFall2 = np.load(file)['tauFall2'] f2 = DoubleExp(tauRise2, tauFall2) else: print('readout decoders for ens') spikes = np.zeros((nTrain, int((t-tTrans)/dt), N)) targets = np.zeros((nTrain, int((t-tTrans)/dt), 2)) for n in range(nTrain): data = go(d1=d1, e1=e1, f1=f1, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, freq=freq, phase=2*np.pi*(n/nTrain)) spikes[n] = data['ens'][int(tTrans/dt):] targets[n] = f.filt(f.filt(data['tar'], dt=dt), dt=dt)[int(tTrans/dt):] d2, f2, tauRise2, tauFall2, X, Y, error = decode(spikes, targets, nTrain, dt=dt, name="oscillateNew", reg=reg, tauRiseMax=tauRiseMax) np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2) times = np.arange(0, t*nTrain, dt)[:len(X)] plotState(times, X, Y, error, "oscillateNew", "%s_ens"%neuron_type, (t-tTrans)*nTrain) if load: e2 = np.load(file)['e2'] #elif isinstance(neuron_type, Bio): print("ens2 encoders") #e2 = np.zeros((N, N, 2)) for n in range(nEnc): data = go(d1=d1, e1=e1, f1=f1, d2=d2, e2=e2, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, freq=freq, phase=2*np.pi*(n/nEnc), l2=True) e2 = data['e2'] np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2) plotActivity(t, dt, fS, data['times'], data['ens2'], data['tarEns2'], "oscillateNew", "ens2") else: e2 = np.zeros((N, N, 2)) np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2) print("testing") errors = np.zeros((nTest)) for test in range(nTest): data = go(d1=d1, e1=e1, f1=f1, d2=d2, e2=e2, f2=f2, NPre=NPre, N=N, t=t+tTrans, dt=dt, f=f, fS=fS, neuron_type=neuron_type, freq=freq, phase=2*np.pi*(test/nTest), tDrive=tDrive, test=True) # curve fit to a sinusoid of arbitrary frequency, phase, magnitude times = data['times'] A = f2.filt(data['ens'], dt=dt) X = np.dot(A, d2) freq0, phase0, mag0, base0 = fitSinusoid(times, X[:,0], freq, int(tTrans/dt), muFreq=muFreq, sigmaFreq=sigmaFreq, base=base) freq1, phase1, mag1, base1 = fitSinusoid(times, X[:,1], freq, int(tTrans/dt), muFreq=muFreq, sigmaFreq=sigmaFreq, base=base) s0 = base0+mag0*np.sin(times*2*np.pi*freq0+phase0) s1 = base1+mag1*np.sin(times*2*np.pi*freq1+phase1) # freqError0 = np.abs(freq-freq0) # freqError1 = np.abs(freq-freq1) rmseError0 = rmse(X[int(tTrans/dt):,0], s0[int(tTrans/dt):]) rmseError1 = rmse(X[int(tTrans/dt):,1], s1[int(tTrans/dt):]) # error0 = (1+freqError0) * rmseError0 # error1 = (1+freqError1) * rmseError1 error0 = rmseError0 error1 = rmseError1 errors[test] = (error0 + error1)/2 fig, (ax, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True) ax.plot(times, X[:,0], label="estimate (dim 0)") ax.plot(times, s0, label="target (dim 0)") ax.set(ylabel='state', title='freq=%.3f, rmse=%.3f'%(freq0, error0), xlim=((0, t)), ylim=((-1.2, 1.2))) ax.legend(loc='upper left') ax2.plot(times, X[:,1], label="estimate (dim 1)") ax2.plot(times, s1, label="target (dim 1)") ax2.set(xlabel='time', ylabel='state', title='freq=%.3f, rmse=%.3f'%(freq1, error1), xlim=((0, t)), ylim=((-1.2, 1.2))) ax2.legend(loc='upper left') sns.despine() fig.savefig("plots/oscillateNew_%s_test%s.pdf"%(neuron_type, test)) plt.close('all') print('%s errors:'%neuron_type, errors) np.savez("data/oscillateNew_%s.npz"%neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2, errors=errors) return errors
def run(NPre=200, N=100, N2=100, t=10, nTrain=10, nEnc=20, nTest=10, neuron_type=LIF(), dt=0.001, f=DoubleExp(1e-3, 1e-1), fS=DoubleExp(1e-3, 1e-1), tauRiseMax=1e-2, load=False, file=None): print('\nNeuron Type: %s' % neuron_type) if load: d1 = np.load(file)['d1'] tauRise1 = np.load(file)['tauRise1'] tauFall1 = np.load(file)['tauFall1'] f1 = DoubleExp(tauRise1, tauFall1) else: print('readout decoders for pre') spikes = np.zeros((nTrain, int(t / 0.001), NPre)) targets = np.zeros((nTrain, int(t / 0.001), 2)) for n in range(nTrain): stim = makeSignal(t, f, dt=0.001, seed=n) data = go(NPre=NPre, N=N, N2=N2, t=t, dt=0.001, f=f, fS=fS, neuron_type=LIF(), stim=stim) spikes[n] = data['pre'] targets[n] = f.filt(data['inpt'], dt=0.001) d1, f1, tauRise1, tauFall1, X, Y, error = decode(spikes, targets, nTrain, dt=0.001, tauRiseMax=tauRiseMax, name="multiplyNew") np.savez("data/multiplyNew_%s.npz" % neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1) times = np.arange(0, t * nTrain, 0.001) plotState(times, X, Y, error, "multiplyNew", "%s_pre" % neuron_type, t * nTrain) if load: e1 = np.load(file)['e1'] elif isinstance(neuron_type, Bio): print("ens1 encoders") e1 = np.zeros((NPre, N, 2)) for n in range(nEnc): stim = makeSignal(t, f, dt=dt, seed=n) data = go(d1=d1, e1=e1, f1=f1, NPre=NPre, N=N, N2=N2, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim, l1=True) e1 = data['e1'] np.savez("data/multiplyNew_%s.npz" % neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1) plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'], "multiplyNew", "ens") else: e1 = np.zeros((NPre, N, 2)) if load: d2 = np.load(file)['d2'] tauRise2 = np.load(file)['tauRise2'] tauFall2 = np.load(file)['tauFall2'] f2 = DoubleExp(tauRise2, tauFall2) else: print('readout decoders for ens') spikes = np.zeros((nTrain, int(t / dt), N)) targets = np.zeros((nTrain, int(t / dt), 1)) for n in range(nTrain): stim = makeSignal(t, f, dt=dt, seed=n) data = go(d1=d1, e1=e1, f1=f1, NPre=NPre, N=N, N2=N2, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim) spikes[n] = data['ens'] targets[n] = f.filt(data['tar'][:, 0] * data['tar'][:, 1], dt=dt).reshape(-1, 1) d2, f2, tauRise2, tauFall2, X, Y, error = decode(spikes, targets, nTrain, dt=dt, tauRiseMax=tauRiseMax, name="multiplyNew") np.savez("data/multiplyNew_%s.npz" % neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2) times = np.arange(0, t * nTrain, dt) plotState(times, X, Y, error, "multiplyNew", "%s_ens" % neuron_type, t * nTrain) if load: e2 = np.load(file)['e2'] elif isinstance(neuron_type, Bio): print("ens2 encoders") e2 = np.zeros((N, N2, 1)) for n in range(nEnc): stim = makeSignal(t, f, dt=dt, seed=n) data = go(d1=d1, e1=e1, f1=f1, d2=d2, e2=e2, f2=f2, NPre=NPre, N=N, N2=N2, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim, l2=True) e2 = data['e2'] np.savez("data/multiplyNew_%s.npz" % neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2) plotActivity(t, dt, fS, data['times'], data['ens2'], data['tarEns2'], "multiplyNew", "ens2") else: e2 = np.zeros((N, N2, 1)) if load: d3 = np.load(file)['d3'] tauRise3 = np.load(file)['tauRise3'] tauFall3 = np.load(file)['tauFall3'] f3 = DoubleExp(tauRise3, tauFall3) else: print('readout decoders for ens2') spikes = np.zeros((nTrain, int(t / dt), N2)) targets = np.zeros((nTrain, int(t / dt), 1)) for n in range(nTrain): stim = makeSignal(t, f, dt=dt, seed=n) data = go(d1=d1, e1=e1, f1=f1, d2=d2, e2=e2, f2=f2, NPre=NPre, N=N, N2=N2, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim) spikes[n] = data['ens2'] targets[n] = f.filt(data['tar2'], dt=dt) d3, f3, tauRise3, tauFall3, X, Y, error = decode(spikes, targets, nTrain, dt=dt, name="multiplyNew") np.savez("data/multiplyNew_%s.npz" % neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2, d3=d3, tauRise3=tauRise3, tauFall3=tauFall3) times = np.arange(0, t * nTrain, dt) plotState(times, X, Y, error, "multiplyNew", "%s_ens2" % neuron_type, t * nTrain) errors = np.zeros((nTest)) print("testing") for test in range(nTest): stim = makeSignal(t, f, dt=dt, seed=100 + test) data = go(d1=d1, e1=e1, f1=f1, d2=d2, e2=e2, f2=f2, NPre=NPre, N=N, N2=N2, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim) A = f3.filt(data['ens2'], dt=dt) X = np.dot(A, d3) Y = f.filt(data['tar2'], dt=dt) error = rmse(X, Y) errors[test] = error plotState(data['times'], X, Y, error, "multiplyNew", "%s_test%s" % (neuron_type, test), t) A = f2.filt(data['ens'], dt=dt) X = np.dot(A, d2) Y = f.filt(data['tar'][:, 0] * data['tar'][:, 1], dt=dt).reshape(-1, 1) plotState(data['times'], X, Y, rmse(X, Y), "multiplyNew", "%s_pretest%s" % (neuron_type, test), t) print('%s errors:' % neuron_type, errors) np.savez("data/multiplyNew_%s.npz" % neuron_type, d1=d1, tauRise1=tauRise1, tauFall1=tauFall1, e1=e1, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2, d3=d3, tauRise3=tauRise3, tauFall3=tauFall3, errors=errors) return errors
def run(n_neurons=100, t=20, t_test=10, t_enc=30, dt=0.001, f=DoubleExp(1e-2, 2e-1), penalty=0, reg=1e-1, freq=1, tt=5.0, tt_test=5.0, neuron_type=LIF(), load_fd=False, load_w=None, supervised=False): d_ens = np.zeros((n_neurons, 2)) f_ens = f w_ff = None w_fb = None e_ff = None e_fb = None f_smooth = DoubleExp(1e-2, 2e-1) print('Neuron Type: %s' % neuron_type) if isinstance(neuron_type, DurstewitzNeuron): if load_w: w_ff = np.load(load_w)['w_ff'] e_ff = np.load(load_w)['e_ff'] else: print('optimizing encoders from pre to ens') data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_enc + tt, f=f, dt=dt, neuron_type=neuron_type, w_ff=w_ff, e_ff=e_ff, L_ff=True) w_ff = data['w_ff'] e_ff = data['e_ff'] np.savez('data/oscillate_w.npz', w_ff=w_ff, e_ff=e_ff) fig, ax = plt.subplots() sns.distplot(np.ravel(w_ff), ax=ax, kde=False) ax.set(xlabel='weights', ylabel='frequency') plt.savefig("plots/tuning/oscillate_%s_w_ff.pdf" % (neuron_type)) a_ens = f_smooth.filt(data['ens'], dt=dt) a_supv = f_smooth.filt(data['supv'], dt=dt) for n in range(n_neurons): fig, ax = plt.subplots(1, 1) ax.plot(data['times'], a_supv[:, n], alpha=0.5, label='supv') ax.plot(data['times'], a_ens[:, n], alpha=0.5, label='ens') ax.set(ylim=((0, 40))) plt.legend() plt.savefig('plots/tuning/oscillate_pre_ens_activity_%s.pdf' % (n)) plt.close('all') if load_fd: load = np.load(load_fd) d_ens = load['d_ens'] taus_ens = load['taus_ens'] f_ens = DoubleExp(taus_ens[0], taus_ens[1]) else: print('gathering filter/decoder training data for ens') data = go(d_ens, f_ens, n_neurons=n_neurons, t=t + tt, f=f, dt=dt, neuron_type=neuron_type, w_ff=w_ff, L_fd=True) trans = int(tt / dt) d_ens, f_ens, taus_ens = df_opt(data['u'][trans:], data['ens'][trans:], f, dt=dt, name='oscillate_%s' % neuron_type, reg=reg, penalty=penalty) np.savez('data/oscillate_%s_fd.npz' % neuron_type, d_ens=d_ens, taus_ens=taus_ens) times = np.arange(0, 1, 0.0001) fig, ax = plt.subplots() ax.plot(times, f.impulse(len(times), dt=0.0001), label=r"$f^x, \tau_1=%.3f, \tau_2=%.3f$" % (-1. / f.poles[0], -1. / f.poles[1])) ax.plot(times, f_ens.impulse(len(times), dt=0.0001), label=r"$f^{ens}, \tau_1=%.3f, \tau_2=%.3f, d: %s/%s$" % (-1. / f_ens.poles[0], -1. / f_ens.poles[1], np.count_nonzero(d_ens), n_neurons)) ax.set(xlabel='time (seconds)', ylabel='impulse response', ylim=((0, 10))) ax.legend(loc='upper right') plt.tight_layout() plt.savefig("plots/oscillate_%s_filters_ens.pdf" % neuron_type) a_ens = f_ens.filt(data['ens'], dt=dt) x = f.filt(data['u'], dt=dt) xhat_ens = np.dot(a_ens, d_ens) rmse_ens = rmse(xhat_ens, x) fig, ax = plt.subplots() ax.plot(data['times'], x, linestyle="--", label='x') ax.plot(data['times'], xhat_ens, label='ens, rmse=%.3f' % rmse_ens) ax.set(xlabel='time (s)', ylabel=r'$\mathbf{x}$', title="pre_ens") plt.legend(loc='upper right') plt.savefig("plots/oscillate_%s_pre_ens_train.pdf" % neuron_type) if isinstance(neuron_type, DurstewitzNeuron): if load_w: w_fb = np.load(load_w)['w_fb'] e_fb = np.load(load_w)['e_fb'] else: print('optimizing encoders from supv to ens') data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_enc + tt, f=f, dt=dt, neuron_type=neuron_type, w_ff=w_ff, w_fb=w_fb, e_fb=e_fb, L_fb=True) w_fb = data['w_fb'] e_fb = data['e_fb'] np.savez('data/oscillate_w.npz', w_ff=w_ff, e_ff=e_ff, w_fb=w_fb, e_fb=e_fb) fig, ax = plt.subplots() sns.distplot(np.ravel(w_fb), ax=ax, kde=False) ax.set(xlabel='weights', ylabel='frequency') plt.savefig("plots/tuning/oscillate_%s_w_fb.pdf" % (neuron_type)) a_ens = f_smooth.filt(data['ens'], dt=dt) a_supv = f_smooth.filt(data['supv'], dt=dt) # a_supv2 = f_smooth.filt(data['supv2'], dt=dt) for n in range(n_neurons): fig, ax = plt.subplots(1, 1) ax.plot(data['times'], a_supv[:, n], alpha=0.5, label='supv') # ax.plot(data['times'], a_supv2[:,n], alpha=0.5, label='supv2') ax.plot(data['times'], a_ens[:, n], alpha=0.5, label='ens') ax.set(ylim=((0, 40))) plt.legend() plt.savefig('plots/tuning/oscillate_supv_ens_activity_%s.pdf' % (n)) plt.close('all') print("Testing") if supervised: data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_test + tt_test, f=f, dt=dt, neuron_type=neuron_type, w_ff=w_ff, w_fb=w_fb, supervised=True) a_ens = f_ens.filt(data['ens'], dt=dt) a_supv = f_ens.filt(data['supv'], dt=dt) # a_supv2 = f_ens.filt(data['supv2'], dt=dt) xhat_ens_0 = np.dot(a_ens, d_ens)[:, 0] xhat_ens_1 = np.dot(a_ens, d_ens)[:, 1] xhat_supv_0 = np.dot(a_supv, d_ens)[:, 0] xhat_supv_1 = np.dot(a_supv, d_ens)[:, 1] # xhat_supv2_0 = np.dot(a_supv2, d_ens)[:,0] # xhat_supv2_1 = np.dot(a_supv2, d_ens)[:,1] x_0 = f.filt(data['u'], dt=dt)[:, 0] x_1 = f.filt(data['u'], dt=dt)[:, 1] x2_0 = f.filt(x_0, dt=dt) x2_1 = f.filt(x_1, dt=dt) times = data['times'] fig, ax = plt.subplots() ax.plot(times, x_0, linestyle="--", label='x_0') ax.plot(times, x2_0, linestyle="--", label='x2_0') ax.plot(times, xhat_supv_0, label='supv') ax.plot(times, xhat_ens_0, label='ens') # ax.plot(times, xhat_supv2_0, label='supv2') ax.set(xlim=((0, t_test)), ylim=((-1, 1)), xlabel='time (s)', ylabel=r'$\mathbf{x}$') plt.legend(loc='upper right') plt.savefig("plots/oscillate_%s_supervised_0.pdf" % neuron_type) fig, ax = plt.subplots() ax.plot(times, x_1, linestyle="--", label='x_1') ax.plot(times, x2_1, linestyle="--", label='x2_1') ax.plot(times, xhat_supv_1, label='supv') ax.plot(times, xhat_ens_1, label='ens') # ax.plot(times, xhat_supv2_1, label='supv2') ax.set(xlim=((0, t_test)), ylim=((-1, 1)), xlabel='time (s)', ylabel=r'$\mathbf{x}$') plt.legend(loc='upper right') plt.savefig("plots/oscillate_%s_supervised_1.pdf" % neuron_type) else: data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_test + tt_test, f=f, dt=dt, neuron_type=neuron_type, w_ff=w_ff, w_fb=w_fb) a_ens = f_ens.filt(data['ens'], dt=dt) xhat_ens_0 = np.dot(a_ens, d_ens)[:, 0] xhat_ens_1 = np.dot(a_ens, d_ens)[:, 1] x_0 = f.filt(data['u'], dt=dt)[:, 0] x_1 = f.filt(data['u'], dt=dt)[:, 1] x2_0 = f.filt(x_0, dt=dt) x2_1 = f.filt(x_1, dt=dt) times = data['times'] # fig, ax = plt.subplots() # ax.plot(times, x_0, linestyle="--", label='x0') # # ax.plot(times, sinusoid_0, label='best fit sinusoid_0') # ax.plot(times, xhat_ens_0, label='ens') # ax.set(xlim=((0, t_test)), ylim=((-1, 1)), xlabel='time (s)', ylabel=r'$\mathbf{x}$') # plt.legend(loc='upper right') # plt.savefig("plots/oscillate_%s_test_0.pdf"%neuron_type) # fig, ax = plt.subplots() # ax.plot(times, x_1, linestyle="--", label='x1') # # ax.plot(times, sinusoid_1, label='best fit sinusoid_1') # ax.plot(times, xhat_ens_1, label='ens') # ax.set(xlim=((0, t_test)), ylim=((-1, 1)), xlabel='time (s)', ylabel=r'$\mathbf{x}$') # plt.legend(loc='upper right') # plt.savefig("plots/oscillate_%s_test_1.pdf"%neuron_type) # curve fit to a sinusoid of arbitrary frequency, phase, magnitude print('Curve fitting') trans = int(tt_test / dt) step = int(0.001 / dt) def sinusoid(t, freq, phase, mag, dt=dt): # mag return f.filt(mag * np.sin(t * 2 * np.pi * freq + 2 * np.pi * phase), dt=dt) p0 = [1, 0, 1] param_0, _ = curve_fit(sinusoid, times[trans:], xhat_ens_0[trans:], p0=p0) param_1, _ = curve_fit(sinusoid, times[trans:], xhat_ens_1[trans:], p0=p0) print('param0', param_0) print('param1', param_1) sinusoid_0 = sinusoid(times, param_0[0], param_0[1], param_0[2]) sinusoid_1 = sinusoid(times, param_1[0], param_1[1], param_1[2]) # error is rmse of xhat and best fit sinusoid times freq error of best fit sinusoid to x freq_error_0 = np.abs(freq - param_0[1]) freq_error_1 = np.abs(freq - param_1[1]) rmse_0 = rmse(xhat_ens_0[trans::step], sinusoid_0[trans::step]) rmse_1 = rmse(xhat_ens_1[trans::step], sinusoid_1[trans::step]) scaled_rmse_0 = (1 + freq_error_0) * rmse_0 scaled_rmse_1 = (1 + freq_error_1) * rmse_1 fig, ax = plt.subplots() ax.plot(times, x_0, linestyle="--", label='x0') ax.plot(times, sinusoid_0, label='best fit sinusoid_0') ax.plot(times, xhat_ens_0, label='ens, scaled rmse=%.3f' % scaled_rmse_0) ax.axvline(tt_test, label=r"$t_{transient}$") ax.set(ylim=((-1, 1)), xlabel='time (s)', ylabel=r'$\mathbf{x}$') plt.legend(loc='upper right') plt.savefig("plots/oscillate_%s_test_0.pdf" % neuron_type) fig, ax = plt.subplots() ax.plot(times, x_1, linestyle="--", label='x1') ax.plot(times, sinusoid_1, label='best fit sinusoid_1') ax.plot(times, xhat_ens_1, label='ens, scaled rmse=%.3f' % scaled_rmse_1) ax.axvline(tt_test, label=r"$t_{transient}$") ax.set(ylim=((-1, 1)), xlabel='time (s)', ylabel=r'$\mathbf{x}$') plt.legend(loc='upper right') plt.savefig("plots/oscillate_%s_test_1.pdf" % neuron_type) print('scaled rmses: ', scaled_rmse_0, scaled_rmse_1) mean = np.mean([scaled_rmse_0, scaled_rmse_1]) fig, ax = plt.subplots() sns.barplot(data=np.array([mean])) ax.set(ylabel='Scaled RMSE', title="mean=%.3f" % mean) plt.xticks() plt.savefig("plots/oscillate_%s_scaled_rmse.pdf" % neuron_type) np.savez('data/oscillate_%s_results.npz' % neuron_type, scaled_rmse_0=scaled_rmse_0, scaled_rmse_1=scaled_rmse_1) return mean
def run(NPre=100, N=30, t=10, nTrain=5, nEnc=5, nTest=10, dt=0.001, neuron_type=LIF(), fPre=DoubleExp(1e-3, 1e-1), fS=DoubleExp(2e-2, 2e-1), Tff=0.3, reg=1e-1, tauRiseMax=1e-1, tauFallMax=3e-1, load=[], file="data/integrate"): print('\nNeuron Type: %s' % neuron_type) file = file + f"{neuron_type}.npz" if 0 in load: dPreA = np.load(file)['dPreA'] # fix indexing of decoders dPreB = np.load(file)['dPreB'] else: print('readout decoders for preInptA and preInptB') spikesInptA = np.zeros((nTrain, int(t / 0.001), NPre)) spikesInptB = np.zeros((nTrain, int(t / 0.001), NPre)) targetsInptA = np.zeros((nTrain, int(t / 0.001), 1)) targetsInptB = np.zeros((nTrain, int(t / 0.001), 1)) for n in range(nTrain): stimA, stimB = makeSignal(t, fPre, dt=0.001, seed=n) data = go(NPre=NPre, N=N, t=t, dt=0.001, neuron_type=neuron_type, fPre=fPre, fS=fS, stimA=stimA, stimB=stimB) spikesInptA[n] = data['preInptA'] spikesInptB[n] = data['preInptB'] targetsInptA[n] = fPre.filt(Tff * data['inptA'], dt=0.001) targetsInptB[n] = fPre.filt(data['inptB'], dt=0.001) dPreA, X1a, Y1a, error1a = decodeNoF(spikesInptA, targetsInptA, nTrain, fPre, dt=0.001, reg=reg) dPreB, X1b, Y1b, error1b = decodeNoF(spikesInptB, targetsInptB, nTrain, fPre, dt=0.001, reg=reg) np.savez(file, dPreA=dPreA, dPreB=dPreB) times = np.arange(0, t * nTrain, 0.001) plotState(times, X1a, Y1a, error1a, "integrate", "%s_preInptA" % neuron_type, t * nTrain) plotState(times, X1b, Y1b, error1b, "integrate", "%s_preInptB" % neuron_type, t * nTrain) if 1 in load: ePreB = np.load(file)['ePreB'] elif isinstance(neuron_type, Bio): print("encoders for preInptB-to-ens") ePreB = np.zeros((NPre, N, 1)) for n in range(nEnc): stimA, stimB = makeSignal(t, fPre, dt=dt, seed=n) data = go(dPreA=dPreA, dPreB=dPreB, ePreB=ePreB, NPre=NPre, N=N, t=t, dt=dt, neuron_type=neuron_type, fPre=fPre, fS=fS, stimA=stimA, stimB=stimB, stage=1) ePreB = data['ePreB'] plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'], "integrate", "preIntgToEns") np.savez(file, dPreA=dPreA, dPreB=dPreB, ePreB=ePreB) else: ePreB = np.zeros((NPre, N, 1)) if 2 in load: ePreA = np.load(file)['ePreA'] elif isinstance(neuron_type, Bio): print("encoders for preInptA-to-ens") ePreA = np.zeros((NPre, N, 1)) for n in range(nEnc): stimA, stimB = makeSignal(t, fPre, dt=dt, seed=n) data = go(dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB, fPre=fPre, NPre=NPre, N=N, t=t, dt=dt, neuron_type=neuron_type, fS=fS, stimA=stimA, stimB=stimB, stage=2) ePreA = data['ePreA'] plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'], "integrate", "preInptToEns") np.savez(file, dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB) else: ePreA = np.zeros((NPre, N, 1)) if 3 in load: dEns = np.load(file)['dEns'] tauRiseEns = np.load(file)['tauRiseEns'] tauFallEns = np.load(file)['tauFallEns'] fEns = DoubleExp(tauRiseEns, tauFallEns) else: print('readout decoders for ens') spikes = np.zeros((nTrain, int(t / dt), N)) targets = np.zeros((nTrain, int(t / dt), 1)) for n in range(nTrain): stimA, stimB = makeSignal(t, fPre, dt=dt, seed=n) data = go(dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB, NPre=NPre, N=N, t=t, dt=dt, neuron_type=neuron_type, fPre=fPre, fS=fS, stimA=stimA, stimB=stimB, stage=3) spikes[n] = data['ens'] targets[n] = fPre.filt(data['inptB'], dt=dt) dEns, fEns, tauRiseEns, tauFallEns, X2, Y2, error2 = decode( spikes, targets, nTrain, dt=dt, reg=reg, tauRiseMax=tauRiseMax, tauFallMax=tauFallMax, name="integrate") np.savez(file, dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB, dEns=dEns, tauRiseEns=tauRiseEns, tauFallEns=tauFallEns) times = np.arange(0, t * nTrain, dt) plotState(times, X2, Y2, error2, "integrate", "%s_ens" % neuron_type, t * nTrain) if 4 in load: eBio = np.load(file)['eBio'] elif isinstance(neuron_type, Bio): print("encoders from ens2 to ens") eBio = np.zeros((N, N, 1)) for n in range(nEnc): stimA, stimB = makeSignal(t, fPre, dt=dt, seed=n) data = go(dPreA=dPreA, dPreB=dPreB, dEns=dEns, ePreA=ePreA, ePreB=ePreB, eBio=eBio, NPre=NPre, N=N, t=t, dt=dt, neuron_type=neuron_type, fPre=fPre, fEns=fEns, fS=fS, stimA=stimA, stimB=stimB, stage=4) eBio = data['eBio'] plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'], "integrate", "Ens2Ens") np.savez(file, dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB, dEns=dEns, tauRiseEns=tauRiseEns, tauFallEns=tauFallEns, eBio=eBio) else: eBio = np.zeros((N, N, 1)) print("testing") errors = np.zeros((nTest)) for test in range(nTest): stimA, stimB = makeSignal(t, fPre, dt=dt, seed=200 + test) data = go(dPreA=dPreA, dEns=dEns, ePreA=ePreA, eBio=eBio, NPre=NPre, N=N, t=t, dt=dt, neuron_type=neuron_type, fPre=fPre, fEns=fEns, fS=fS, stimA=stimA, stimB=stimB, stage=5) A = fEns.filt(data['ens'], dt=dt) X = np.dot(A, dEns) Y = fPre.filt(data['inptB'], dt=dt) U = fPre.filt(Tff * data['inptA'], dt=dt) error = rmse(X, Y) errorU = rmse(X, U) errors[test] = error fig, ax = plt.subplots() # ax.plot(data['times'], U, label="input") ax.plot(data['times'], X, label="estimate") ax.plot(data['times'], Y, label="target") ax.set(xlabel='time', ylabel='state', title='rmse=%.3f' % error, xlim=((0, t)), ylim=((-1, 1))) ax.legend(loc='upper left') sns.despine() fig.savefig("plots/integrate_%s_test%s.pdf" % (neuron_type, test)) plt.close('all') # print('errors:', errors) # np.savez(file, errors=errors, dPreA=dPreA, dPreB=dPreB, ePreA=ePreA, ePreB=ePreB, dEns=dEns, tauRiseEns=tauRiseEns, tauFallEns=tauFallEns, eBio=eBio) return data['times'], X, Y
def run(N=3000, neuron_type=LIF(), tTrain=200, tTest=100, tTransTrain=20, tTransTest=20, nTrain=1, nTest=10, dt=0.001, dtSampleTrain=0.003, dtSampleTest=0.01, seed=0, f2=10, reg=1e-3, reg2=1e-3, evals=100, r=30, load=False, file=None, tauRiseMin=3e-2, tauRiseMax=6e-2, tauFallMin=2e-1, tauFallMax=3e-1): print('\nNeuron Type: %s'%neuron_type) rng = np.random.RandomState(seed=seed) timeStepsTrain = int((tTrain-tTransTrain)/dtSampleTrain) tStart = int(tTransTrain/dtSampleTrain) tFlat = int((tTrain-tTransTrain)/dtSampleTrain)*nTrain if load: d = np.load(file)['d'] d2 = np.load(file)['d2'] tauRise = np.load(file)['tauRise'] tauFall = np.load(file)['tauFall'] f = DoubleExp(tauRise, tauFall) else: print('decoders for ens') spikes = np.zeros((nTrain, timeStepsTrain, N)) spikes2 = np.zeros((nTrain, timeStepsTrain, N)) targets = np.zeros((nTrain, timeStepsTrain, 3)) targets2 = np.zeros((nTrain, timeStepsTrain, 3)) for n in range(nTrain): # IC = np.array([rng.uniform(-15, 15), rng.uniform(-20, 20), rng.uniform(10, 35)]) IC = np.array([rng.uniform(-5, 5), rng.uniform(-5, 5), rng.uniform(20, 25)]) data = go(N=N, neuron_type=neuron_type, l=True, t=tTrain, r=r, dt=dt, dtSample=dtSampleTrain, seed=seed, IC=IC) spikes[n] = data['ens'][-timeStepsTrain:] spikes2[n] = gaussian_filter1d(data['ens'], sigma=f2, axis=0)[-timeStepsTrain:] targets[n] = data['tar'][-timeStepsTrain:] targets2[n] = gaussian_filter1d(data['tar'], sigma=f2, axis=0)[-timeStepsTrain:] d, f, tauRise, tauFall, X, Y, error = decode( spikes, targets, nTrain, dt=dtSampleTrain, dtSample=dtSampleTrain, name="lorenzNew", evals=evals, reg=reg, tauRiseMin=tauRiseMin, tauRiseMax=tauRiseMax, tauFallMin=tauFallMin, tauFallMax=tauFallMax) spikes2 = spikes2.reshape((tFlat, N)) targets2 = targets2.reshape((tFlat, 3)) A2 = gaussian_filter1d(spikes2, sigma=f2, axis=0)[-timeStepsTrain:] Y2 = gaussian_filter1d(targets2, sigma=f2, axis=0)[-timeStepsTrain:] d2, _ = nengo.solvers.LstsqL2(reg=reg2)(spikes2, targets2) np.savez("data/lorenzNew_%s.npz"%neuron_type, d=d, d2=d2, tauRise=tauRise, tauFall=tauFall, f2=f2) X2 = np.dot(A2, d2)[-timeStepsTrain:] error = plotLorenz(X, X2, Y2, neuron_type, "train") print("testing") tStart = int(tTransTest/dtSampleTest) errors = np.zeros((nTest)) rng = np.random.RandomState(seed=100+seed) for test in range(nTest): # IC = np.array([rng.uniform(-15, 15), rng.uniform(-20, 20), rng.uniform(10, 35)]) IC = np.array([rng.uniform(-5, 5), rng.uniform(-5, 5), rng.uniform(20, 25)]) data = go(d=d, f=f, N=N, neuron_type=neuron_type, t=tTest, r=r, dt=dt, dtSample=dtSampleTest, seed=seed, IC=IC) A = f.filt(data['ens'], dt=dtSampleTest) A2 = gaussian_filter1d(data['ens'], sigma=f2, axis=0)[tStart:] X = np.dot(A, d)[tStart:] X2 = np.dot(A2, d2)[tStart:] Y = data['tar'][tStart:] Y2 = gaussian_filter1d(data['tar'], sigma=f2, axis=0)[tStart:] error = plotLorenz(X, X2, Y2, neuron_type, test) errors[test] = error _ = plotLorenz(Y, Y2, Y2, 'target', '') print('errors: ', errors) np.savez("data/lorenzNew_%s.npz"%neuron_type, d=d, d2=d2, tauRise=tauRise, tauFall=tauFall, f2=f2, errors=errors) return errors
def run(NPre=100, N=100, t=10, nTrain=10, nTest=30, nEnc=10, neuron_type=LIF(), dt=0.001, f=DoubleExp(1e-3, 1e-1), fS=DoubleExp(1e-3, 1e-1), Tff=0.1, Tfb=1.0, reg=1e-3, tauRiseMax=5e-2, tauFallMax=3e-1, load=False, file=None): print('\nNeuron Type: %s' % neuron_type) if load: d1a = np.load(file)['d1a'] d1b = np.load(file)['d1b'] tauRise1a = np.load(file)['tauRise1a'] tauRise1b = np.load(file)['tauRise1b'] tauFall1a = np.load(file)['tauFall1a'] tauFall1b = np.load(file)['tauFall1b'] f1a = DoubleExp(tauRise1a, tauFall1a) f1b = DoubleExp(tauRise1b, tauFall1b) else: print('readout decoders for preInpt and preIntg') spikesInpt = np.zeros((nTrain, int(t / 0.001), NPre)) spikesIntg = np.zeros((nTrain, int(t / 0.001), NPre)) targetsInpt = np.zeros((nTrain, int(t / 0.001), 1)) targetsIntg = np.zeros((nTrain, int(t / 0.001), 1)) for n in range(nTrain): stim = makeSignal(t, f, dt=0.001, seed=n) data = go(NPre=NPre, N=N, t=t, dt=0.001, f=f, fS=fS, neuron_type=LIF(), stim=stim) spikesInpt[n] = data['preInpt'] spikesIntg[n] = data['preIntg'] targetsInpt[n] = f.filt(Tff * data['inpt'], dt=0.001) targetsIntg[n] = f.filt(data['intg'], dt=0.001) d1a, f1a, tauRise1a, tauFall1a, X1a, Y1a, error1a = decode( spikesInpt, targetsInpt, nTrain, dt=0.001, reg=reg, name="integrateNew", tauRiseMax=tauRiseMax, tauFallMax=tauFallMax) d1b, f1b, tauRise1b, tauFall1b, X1b, Y1b, error1b = decode( spikesIntg, targetsIntg, nTrain, dt=0.001, reg=reg, name="integrateNew", tauRiseMax=tauRiseMax, tauFallMax=tauFallMax) np.savez("data/integrateNew_%s.npz" % neuron_type, d1a=d1a, d1b=d1b, tauRise1a=tauRise1a, tauRise1b=tauRise1b, tauFall1a=tauFall1a, tauFall1b=tauFall1b) times = np.arange(0, t * nTrain, 0.001) plotState(times, X1a, Y1a, error1a, "integrateNew", "%s_preInpt" % neuron_type, t * nTrain) plotState(times, X1b, Y1b, error1b, "integrateNew", "%s_preIntg" % neuron_type, t * nTrain) if load: e1a = np.load(file)['e1a'] e1b = np.load(file)['e1b'] elif isinstance(neuron_type, Bio): e1a = np.zeros((NPre, N, 1)) e1b = np.zeros((NPre, N, 1)) print("encoders for preIntg-to-ens") for n in range(nEnc): stim = makeSignal(t, f, dt=dt, seed=n) data = go(d1a=d1a, d1b=d1b, e1a=e1a, e1b=e1b, f1a=f1a, f1b=f1b, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim, l1b=True) e1b = data['e1b'] plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'], "integrateNew", "preIntgToEns") np.savez("data/integrateNew_%s.npz" % neuron_type, d1a=d1a, d1b=d1b, tauRise1a=tauRise1a, tauRise1b=tauRise1b, tauFall1a=tauFall1a, tauFall1b=tauFall1b, e1a=e1a, e1b=e1b) print("encoders for preInpt-to-ens") for n in range(nEnc): stim = makeSignal(t, f, dt=dt, seed=n) # stim2 = makeSignal(t, f, dt=dt, seed=n, value=0.5) # stim2 = makeSignal(t, f, dt=dt, norm='u', freq=0.25, value=0.8, seed=100+n) stim2 = makeSin(t, f, dt=dt, seed=n) data = go(d1a=d1a, d1b=d1b, e1a=e1a, e1b=e1b, f1a=f1a, f1b=f1b, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim, l1a=True, stim2=stim2) e1a = data['e1a'] plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns'], "integrateNew", "preInptToEns") np.savez("data/integrateNew_%s.npz" % neuron_type, d1a=d1a, d1b=d1b, tauRise1a=tauRise1a, tauRise1b=tauRise1b, tauFall1a=tauFall1a, tauFall1b=tauFall1b, e1a=e1a, e1b=e1b) else: e1a = np.zeros((NPre, N, 1)) e1b = np.zeros((NPre, N, 1)) if load: d2 = np.load(file)['d2'] tauRise2 = np.load(file)['tauRise2'] tauFall2 = np.load(file)['tauFall2'] f2 = DoubleExp(tauRise2, tauFall2) else: print('readout decoders for ens') spikes = np.zeros((nTrain, int(t / dt), N)) targets = np.zeros((nTrain, int(t / dt), 1)) for n in range(nTrain): stim = makeSignal(t, f, dt=dt, seed=n) data = go(d1a=d1a, d1b=d1b, e1a=e1a, e1b=e1b, f1a=f1a, f1b=f1b, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim, l2=True) spikes[n] = data['ens'] targets[n] = f.filt(Tfb * data['intg'], dt=dt) d2, f2, tauRise2, tauFall2, X, Y, error = decode(spikes, targets, nTrain, dt=dt, reg=reg, tauRiseMax=tauRiseMax, tauFallMax=tauFallMax, name="integrateNew") np.savez("data/integrateNew_%s.npz" % neuron_type, d1a=d1a, d1b=d1b, tauRise1a=tauRise1a, tauRise1b=tauRise1b, tauFall1a=tauFall1a, tauFall1b=tauFall1b, e1a=e1a, e1b=e1b, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2) times = np.arange(0, t * nTrain, dt) plotState(times, X, Y, error, "integrateNew", "%s_ens" % neuron_type, t * nTrain) if load: e2 = np.load(file)['e2'] elif isinstance(neuron_type, Bio): print("encoders from ens2 to ens") e2 = np.zeros((N, N, 1)) for n in range(nEnc): stim = makeSignal(t, f, dt=dt, seed=n) #stim = makeSin(t, f, dt=dt, seed=n) data = go(d1a=d1a, d1b=d1b, d2=d2, e1a=e1a, e1b=e1b, e2=e2, f1a=f1a, f1b=f1b, f2=f2, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim, l3=True) e2 = data['e2'] plotActivity(t, dt, fS, data['times'], data['ens'], data['tarEns2'], "integrateNew", "Ens2Ens") np.savez("data/integrateNew_%s.npz" % neuron_type, d1a=d1a, d1b=d1b, tauRise1a=tauRise1a, tauRise1b=tauRise1b, tauFall1a=tauFall1a, tauFall1b=tauFall1b, e1a=e1a, e1b=e1b, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2) else: e2 = np.zeros((N, N, 1)) print("testing") errors = np.zeros((nTest)) for test in range(nTest): stim = makeSignal(t, f, dt=dt, seed=200 + test) data = go(d1a=d1a, d2=d2, e1a=e1a, e2=e2, f1a=f1a, f2=f2, NPre=NPre, N=N, t=t, dt=dt, f=f, fS=fS, neuron_type=neuron_type, stim=stim, test=True) A = f2.filt(data['ens'], dt=dt) X = np.dot(A, d2) Y = f.filt(data['intg'], dt=dt) U = f.filt(f.filt(Tff * data['inpt'], dt=dt)) error = rmse(X, Y) errorU = rmse(X, U) errors[test] = error plotState(data['times'], X, Y, error, "integrateNew", "%s_test%s" % (neuron_type, test), t) #plotState(data['times'], X, U, errorU, "integrateNew", "%s_inpt%s"%(neuron_type, test), t) print('%s errors:' % neuron_type, errors) np.savez("data/integrateNew_%s.npz" % neuron_type, d1a=d1a, d1b=d1b, tauRise1a=tauRise1a, tauRise1b=tauRise1b, tauFall1a=tauFall1a, tauFall1b=tauFall1b, e1a=e1a, e1b=e1b, d2=d2, tauRise2=tauRise2, tauFall2=tauFall2, e2=e2, errors=errors) return errors
def run(n_neurons=30, t=30, t_test=10, n_encodes=20, dt=0.001, n_tests=10, neuron_type=LIF(), f=DoubleExp(1e-3, 3e-2), f_out=DoubleExp(1e-3, 1e-1), reg=0, penalty=1.0, load_w=None, load_df=None): d_ens = np.zeros((n_neurons, 1)) f_ens = f f_smooth = DoubleExp(1e-2, 2e-1) w_ens = None e_ens = None w_ens2 = None e_ens2 = None print('\nNeuron Type: %s' % neuron_type) if isinstance(neuron_type, DurstewitzNeuron): if load_w: w_ens = np.load(load_w)['w_ens'] else: print('Optimizing ens1 encoders') for nenc in range(n_encodes): print("encoding trial %s" % nenc) stim_func = make_normed_flipped(value=1.2, t=t, dt=dt, f=f, seed=nenc) data = go(d_ens, f_ens, n_neurons=n_neurons, t=t, f=f, dt=0.001, stim_func=stim_func, neuron_type=neuron_type, w_ens=w_ens, e_ens=e_ens, L=True) e_ens = data['e_ens'] w_ens = data['w_ens'] np.savez('data/identity_w.npz', e_ens=e_ens, w_ens=w_ens) fig, ax = plt.subplots() sns.distplot(np.ravel(w_ens), ax=ax, kde=False) ax.set(xlabel='weights', ylabel='frequency') plt.savefig("plots/tuning/identity_%s_w_ens_nenc_%s.pdf" % (neuron_type, nenc)) a_ens = f_smooth.filt(data['ens'], dt=dt) a_supv = f_smooth.filt(data['supv'], dt=dt) for n in range(n_neurons): fig, ax = plt.subplots(1, 1) ax.plot(data['times'], a_supv[:, n], alpha=0.5, label='supv') ax.plot(data['times'], a_ens[:, n], alpha=0.5, label='ens') ax.set(ylim=((0, 40))) plt.legend() plt.savefig( 'plots/tuning/identity_ens_nenc_%s_activity_%s.pdf' % (nenc, n)) plt.close('all') if load_df: load = np.load(load_df) d_ens = load['d_ens'] d_out1 = load['d_out1'] taus_ens = load['taus_ens'] taus_out1 = load['taus_out1'] f_ens = DoubleExp(taus_ens[0], taus_ens[1]) f_out1 = DoubleExp(taus_out1[0], taus_out1[1]) else: print('Optimizing ens1 filters and decoders') stim_func = make_normed_flipped(value=1.0, t=t, dt=dt, f=f) data = go(d_ens, f_ens, n_neurons=n_neurons, t=t, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, w_ens=w_ens) d_ens, f_ens, taus_ens = df_opt(data['x'], data['ens'], f, dt=dt, reg=reg, penalty=penalty, name='identity_%s' % neuron_type) d_out1, f_out1, taus_out1 = df_opt(data['x'], data['ens'], f_out, dt=dt, reg=0, penalty=0, name='identity_%s' % neuron_type) np.savez('data/identity_%s_df.npz' % neuron_type, d_ens=d_ens, taus_ens=taus_ens, d_out1=d_out1, taus_out1=taus_out1) times = np.arange(0, 1, 0.0001) fig, ax = plt.subplots() ax.plot(times, f.impulse(len(times), dt=0.0001), label=r"$f^x, \tau_1=%.3f, \tau_2=%.3f$" % (-1. / f.poles[0], -1. / f.poles[1])) ax.plot(times, f_ens.impulse(len(times), dt=0.0001), label=r"$f^{ens}, \tau_1=%.3f, \tau_2=%.3f, d: %s/%s$" % (-1. / f_ens.poles[0], -1. / f_ens.poles[1], np.count_nonzero(d_ens), n_neurons)) ax.set(xlabel='time (seconds)', ylabel='impulse response', ylim=((0, 10))) ax.legend(loc='upper right') plt.tight_layout() plt.savefig("plots/identity_%s_filters_ens.pdf" % neuron_type) times = np.arange(0, 1, 0.0001) fig, ax = plt.subplots() ax.plot(times, f_out.impulse(len(times), dt=0.0001), label=r"$f^{out}, \tau=%.3f, \tau_2=%.3f$" % (-1. / f_out.poles[0], -1. / f_out.poles[1])) ax.plot(times, f_out1.impulse(len(times), dt=0.0001), label=r"$f^{out1}, \tau_1=%.3f, \tau_2=%.3f, d: %s/%s$" % (-1. / f_out1.poles[0], -1. / f_out1.poles[1], np.count_nonzero(d_out1), n_neurons)) ax.set(xlabel='time (seconds)', ylabel='impulse response', ylim=((0, 10))) ax.legend(loc='upper right') plt.tight_layout() plt.savefig("plots/identity_%s_filters_out1.pdf" % neuron_type) a_ens = f_out1.filt(data['ens'], dt=dt) x = f_out.filt(data['x'], dt=dt) xhat_ens = np.dot(a_ens, d_out1) rmse_ens = rmse(xhat_ens, x) fig, ax = plt.subplots() ax.plot(data['times'], x, linestyle="--", label='x') ax.plot(data['times'], xhat_ens, label='ens, rmse=%.3f' % rmse_ens) ax.set(xlabel='time (s)', ylabel=r'$\mathbf{x}$', title="train ens1") plt.legend(loc='upper right') plt.savefig("plots/identity_%s_ens1_train.pdf" % neuron_type) if isinstance(neuron_type, DurstewitzNeuron): if load_w: w_ens2 = np.load(load_w)['w_ens2'] else: print('Optimizing ens2 encoders') for nenc in range(n_encodes): print("encoding trial %s" % nenc) stim_func = make_normed_flipped(value=1.2, t=t, dt=dt, f=f, seed=nenc) data = go(d_ens, f_ens, n_neurons=n_neurons, t=t, f=f, f_smooth=f_smooth, neuron_type=neuron_type, stim_func=stim_func, w_ens=w_ens, w_ens2=w_ens2, e_ens2=e_ens2, L2=True) w_ens2 = data['w_ens2'] e_ens2 = data['e_ens2'] fig, ax = plt.subplots() sns.distplot(np.ravel(w_ens2), ax=ax) ax.set(xlabel='weights', ylabel='frequency') plt.savefig("plots/tuning/identity_%s_w_ens2_nenc_%s.pdf" % (neuron_type, nenc)) np.savez('data/identity_w.npz', w_ens=w_ens, w_ens2=w_ens2, e_ens=e_ens, e_ens2=e_ens2) a_ens = f_smooth.filt(data['ens2'], dt=dt) a_supv = f_smooth.filt(data['supv2'], dt=dt) for n in range(n_neurons): fig, ax = plt.subplots(1, 1) ax.plot(data['times'], a_supv[:, n], alpha=0.5, label='supv2') ax.plot(data['times'], a_ens[:, n], alpha=0.5, label='ens2') ax.set(ylim=((0, 40))) plt.legend() plt.savefig( 'plots/tuning/identity_ens2_nenc_%s_activity_%s.pdf' % (nenc, n)) plt.close('all') if load_df: load = np.load(load_df) d_out2 = load['d_out2'] taus_out2 = load['taus_out2'] f_out2 = DoubleExp(taus_out2[0], taus_out2[1]) else: print('Optimizing ens2 filters and decoders') stim_func = make_normed_flipped(value=1.0, t=t, dt=dt, f=f) data = go(d_ens, f_ens, n_neurons=n_neurons, t=t, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, w_ens=w_ens, w_ens2=w_ens2) d_out2, f_out2, taus_out2 = df_opt(data['x2'], data['ens2'], f_out, dt=dt, reg=0, penalty=0, name='identity_%s' % neuron_type) np.savez('data/identity_%s_df.npz' % neuron_type, d_ens=d_ens, taus_ens=taus_ens, d_out1=d_out1, taus_out1=taus_out1, d_out2=d_out2, taus_out2=taus_out2) times = np.arange(0, 1, 0.0001) fig, ax = plt.subplots() ax.plot(times, f_out.impulse(len(times), dt=0.0001), label=r"$f^{out}, \tau=%.3f, \tau_2=%.3f$" % (-1. / f_out.poles[0], -1. / f_out.poles[1])) ax.plot(times, f_out2.impulse(len(times), dt=0.0001), label=r"$f^{out2}, \tau_1=%.3f, \tau_2=%.3f, d: %s/%s$" % (-1. / f_out2.poles[0], -1. / f_out2.poles[1], np.count_nonzero(d_out2), n_neurons)) ax.set(xlabel='time (seconds)', ylabel='impulse response', ylim=((0, 10))) ax.legend(loc='upper right') plt.tight_layout() plt.savefig("plots/identity_%s_filters_out2.pdf" % neuron_type) fig, ax = plt.subplots() sns.distplot(np.ravel(d_out2)) ax.set(xlabel='decoders', ylabel='frequency') plt.savefig("plots/identity_%s_d_out2.pdf" % neuron_type) a_ens2 = f_out2.filt(data['ens2'], dt=dt) x2 = f_out.filt(data['x2'], dt=dt) xhat_ens2 = np.dot(a_ens2, d_out2) rmse_ens2 = rmse(xhat_ens2, x2) fig, ax = plt.subplots() ax.plot(data['times'], x2, linestyle="--", label='x') ax.plot(data['times'], xhat_ens2, label='ens2, rmse=%.3f' % rmse_ens2) ax.set(xlabel='time (s)', ylabel=r'$\mathbf{x}$', title="train ens2") plt.legend(loc='upper right') plt.savefig("plots/identity_%s_ens2_train.pdf" % neuron_type) rmses_ens = np.zeros((n_tests)) rmses_ens2 = np.zeros((n_tests)) for test in range(n_tests): print('test %s' % test) stim_func = make_normed_flipped(value=1.0, t=t_test, dt=dt, f=f, seed=100 + test) data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_test, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, w_ens=w_ens, w_ens2=w_ens2) a_ens = f_out1.filt(data['ens'], dt=dt) x = f_out.filt(data['x'], dt=dt) xhat_ens = np.dot(a_ens, d_ens) rmse_ens = rmse(xhat_ens, x) a_ens2 = f_out2.filt(data['ens2'], dt=dt) x2 = f_out.filt(data['x2'], dt=dt) xhat_ens2 = np.dot(a_ens2, d_out2) rmse_ens2 = rmse(xhat_ens2, x2) rmses_ens[test] = rmse_ens rmses_ens2[test] = rmse_ens2 fig, ax = plt.subplots() ax.plot(data['times'], x, linestyle="--", label='x') ax.plot(data['times'], xhat_ens, label='ens, rmse=%.3f' % rmse_ens) ax.set(xlabel='time (s)', ylabel=r'$\mathbf{x}$', title="test ens1") plt.legend(loc='upper right') plt.savefig("plots/identity_%s_ens1_test_%s.pdf" % (neuron_type, test)) fig, ax = plt.subplots() ax.plot(data['times'], x2, linestyle="--", label='x') ax.plot(data['times'], xhat_ens2, label='ens2, rmse=%.3f' % rmse_ens2) ax.set(xlabel='time (s)', ylabel=r'$\mathbf{x}$', title="test ens2") plt.legend(loc='upper right') plt.savefig("plots/identity_%s_ens2_test_%s.pdf" % (neuron_type, test)) plt.close('all') mean_ens = np.mean(rmses_ens) mean_ens2 = np.mean(rmses_ens2) CI_ens = sns.utils.ci(rmses_ens) CI_ens2 = sns.utils.ci(rmses_ens2) fig, ax = plt.subplots() sns.barplot(data=rmses_ens2) ax.set(ylabel='RMSE', title="mean=%.3f, CI=%.3f-%.3f" % (mean_ens2, CI_ens2[0], CI_ens2[1])) plt.xticks() plt.savefig("plots/identity_%s_rmse.pdf" % neuron_type) print('rmses: ', rmses_ens, rmses_ens2) print('means: ', mean_ens, mean_ens2) print('confidence intervals: ', CI_ens, CI_ens2) np.savez('data/identity_%s_results.npz' % neuron_type, rmses_ens=rmses_ens, rmses_ens2=rmses_ens2) return rmses_ens2
def run(n_neurons=100, t=10, t_flat=3, t_test=10, n_train=20, n_test=10, reg=1e-1, penalty=0.0, df_evals=100, T=0.2, dt=0.001, neuron_type=LIF(), f=DoubleExp(1e-2, 2e-1), f_smooth=DoubleExp(1e-2, 2e-1), w_file="data/memory_w.npz", fd_file="data/memory_DurstewitzNeuron()_fd.npz", load_w_x=False, load_w_u=False, load_w_fb=False, load_fd=False, supervised=False): d_ens = np.zeros((n_neurons, 1)) f_ens = f w_u = None w_x = None w_fb = None DA = None if isinstance(neuron_type, DurstewitzNeuron): DA = neuron_type.DA if load_w_x: w_x = np.load(w_file)['w_x'] e_x = np.load(w_file)['e_x'] else: print('optimizing encoders from pre_x into ens (white noise)') e_x = None w_x = None for nenc in range(n_train): print("encoding trial %s" % nenc) u = make_signal(t=t, t_flat=t_flat, f=f, normed='x', seed=nenc) t_sim = len(u) * dt - dt stim_func = lambda t: u[int(t / dt)] data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_sim, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, T=T, e_x=e_x, w_x=w_x, L_x=True) w_x = data['w_x'] e_x = data['e_x'] np.savez('data/memory_DA%s_w.npz' % DA, w_x=w_x, e_x=e_x) a_ens = f_smooth.filt(data['ens'], dt=dt) a_supv = f_smooth.filt(data['supv'], dt=dt) for n in range(n_neurons): fig, ax = plt.subplots(1, 1) ax.plot(data['times'], a_supv[:, n], alpha=0.5, label='supv') ax.plot(data['times'], a_ens[:, n], alpha=0.5, label='ens') ax.set(ylabel='firing rate', ylim=((0, 40))) plt.legend() plt.savefig( 'plots/tuning/memory_x_DA%s_nenc_%s_activity_%s.pdf' % (DA, nenc, n)) plt.close('all') if load_w_u: w_u = np.load(w_file)['w_u'] e_u = np.load(w_file)['e_u'] else: print('optimizing encoders from pre_u into ens (white noise)') e_u = None w_u = None bases = np.linspace(-0.5, 0.5, n_train) for nenc in range(n_train): print("encoding trial %s" % nenc) u = make_signal(t=t, t_flat=t_flat, f=f, normed='x', seed=nenc) t_sim = len(u) * dt - dt stim_func = lambda t: u[int(t / dt)] stim_func_base = lambda t: bases[nenc] data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_sim, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, T=T, w_x=w_x, e_u=e_u, w_u=w_u, stim_func_base=stim_func_base, L_u=True) w_u = data['w_u'] e_u = data['e_u'] np.savez('data/memory_DA%s_w.npz' % DA, w_x=w_x, e_x=e_x, w_u=w_u, e_u=e_u) a_ens = f_smooth.filt(data['ens'], dt=dt) a_supv = f_smooth.filt(data['supv'], dt=dt) for n in range(n_neurons): fig, ax = plt.subplots(1, 1) ax.plot(data['times'], a_supv[:, n], alpha=0.5, label='supv') ax.plot(data['times'], a_ens[:, n], alpha=0.5, label='ens') ax.set(ylabel='firing rate', ylim=((0, 40))) plt.legend() plt.savefig( 'plots/tuning/memory_u_DA%s_nenc_%s_activity_%s.pdf' % (DA, nenc, n)) plt.close('all') if load_fd: load = np.load(fd_file) d_ens = load['d_ens'] taus_ens = load['taus_ens'] f_ens = DoubleExp(taus_ens[0], taus_ens[1]) else: print( 'gathering filter/decoder training data for ens (white-flat-white)' ) targets = np.zeros((1, 1)) spikes = np.zeros((1, n_neurons)) for ntrn in range(n_train): print('filter/decoder iteration %s' % ntrn) u = make_signal(t=t, t_flat=t_flat, f=f, normed='x', seed=ntrn, dt=dt) t_sim = len(u) * dt - dt stim_func = lambda t: u[int(t / dt)] data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_sim, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, T=T, w_x=w_x, w_u=w_u, L_fd=True) targets = np.append(targets, data['x'], axis=0) spikes = np.append(spikes, data['ens'], axis=0) print('optimizing filters and decoders') d_ens, f_ens, taus_ens = df_opt(targets, spikes, f, dt=dt, penalty=penalty, reg=reg, df_evals=df_evals, name='flat_%s' % neuron_type) if DA: np.savez('data/memory_%s_DA%s_fd.npz' % (neuron_type, DA), d_ens=d_ens, taus_ens=taus_ens) else: np.savez('data/memory_%s_fd.npz' % neuron_type, d_ens=d_ens, taus_ens=taus_ens) times = np.arange(0, 1, 0.0001) fig, ax = plt.subplots() ax.plot(times, f.impulse(len(times), dt=0.0001), label=r"$f^x, \tau_1=%.3f, \tau_2=%.3f$" % (-1. / f.poles[0], -1. / f.poles[1])) ax.plot(times, f_ens.impulse(len(times), dt=0.0001), label=r"$f^{ens}, \tau_1=%.3f, \tau_2=%.3f, d: %s/%s$" % (-1. / f_ens.poles[0], -1. / f_ens.poles[1], np.count_nonzero(d_ens), n_neurons)) ax.set(xlabel='time (seconds)', ylabel='impulse response', ylim=((0, 10))) ax.legend(loc='upper right') plt.tight_layout() if DA: plt.savefig("plots/memory_%s_DA%s_filters.pdf" % (neuron_type, DA)) else: plt.savefig("plots/memory_%s_filters.pdf" % neuron_type) a_ens = f_ens.filt(spikes, dt=dt) xhat_ens = np.dot(a_ens, d_ens) x = f.filt(targets, dt=dt) rmse_ens = rmse(xhat_ens, x) fig, ax = plt.subplots() ax.plot(x, linestyle="--", label='x') ax.plot(xhat_ens, label='ens, rmse=%.3f' % rmse_ens) ax.set(xlabel='time (s)', ylabel=r'$\mathbf{x}$', title="train_fb") plt.legend(loc='upper right') if DA: plt.savefig("plots/memory_%s_DA%s_train_fb.pdf" % (neuron_type, DA)) else: plt.savefig("plots/memory_%s_train_fb.pdf" % neuron_type) if isinstance(neuron_type, DurstewitzNeuron): if load_w_fb: w_fb = np.load(w_file)['w_fb'] e_fb = np.load(w_file)['e_fb'] else: print('optimizing encoders from ens2 into ens (white noise)') e_fb = None w_fb = None for nenc in range(n_train): print("encoding trial %s" % nenc) u = make_signal(t=t, t_flat=t_flat, f=f, normed='x', seed=nenc) t_sim = len(u) * dt - dt stim_func = lambda t: u[int(t / dt)] data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_sim, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, T=T, w_x=w_x, w_u=w_u, e_fb=e_fb, w_fb=w_fb, L_fb=True) w_fb = data['w_fb'] e_fb = data['e_fb'] np.savez('data/memory_DA%s_w.npz' % DA, w_x=w_x, e_x=e_x, w_u=w_u, e_u=e_u, w_fb=w_fb, e_fb=e_fb) a_ens = f_smooth.filt(data['ens'], dt=dt) a_ens2 = f_smooth.filt(data['ens2'], dt=dt) a_supv = f_smooth.filt(data['supv'], dt=dt) # a_supv2 = f_smooth.filt(data['supv2'], dt=dt) for n in range(n_neurons): fig, ax = plt.subplots(1, 1) ax.plot(data['times'], a_supv[:, n], alpha=0.5, label='supv') # ax.plot(data['times'], a_supv2[:,n], alpha=0.5, label='supv2') ax.plot(data['times'], a_ens[:, n], alpha=0.5, label='ens') ax.plot(data['times'], a_ens2[:, n], alpha=0.5, label='ens2') ax.set(ylabel='firing rate', ylim=((0, 40))) plt.legend() plt.savefig( 'plots/tuning/memory_fb_DA%s_nenc_%s_activity_%s.pdf' % (DA, nenc, n)) plt.close('all') errors_flat = np.zeros((n_test)) errors_final = np.zeros((n_test)) errors_abs = np.zeros((n_test, int(t_test / dt))) for test in range(n_test): print('test %s' % test) u = make_signal(t=6, t_flat=t_test, f=f, normed='x', seed=test, dt=dt, test=True) t_sim = len(u) * dt - dt stim_func = lambda t: u[int(t / dt)] if supervised: data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_sim, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, T=T, w_x=w_x, w_u=w_u, w_fb=w_fb, supervised=True) a_ens = f_ens.filt(data['ens'], dt=dt) a_ens2 = f_ens.filt(data['ens2'], dt=dt) x = f.filt(f.filt(data['x'], dt=dt), dt=dt) xhat_ens = np.dot(a_ens, d_ens) xhat_ens2 = np.dot(a_ens2, d_ens) xhat_supv = data['supv_state'] xhat_supv2 = data['supv2_state'] error_ens = rmse(xhat_ens, x) error_ens2 = rmse(xhat_ens2, x) error_supv = rmse(xhat_supv, x) error_supv2 = rmse(xhat_supv2, x) fig, ax = plt.subplots() ax.plot(data['times'], x, linestyle="--", label='x') ax.plot(data['times'], xhat_ens, label='ens, error=%.3f' % error_ens) ax.plot(data['times'], xhat_ens2, label='ens2, error=%.3f' % error_ens2) ax.plot(data['times'], xhat_supv, label='supv, error=%.3f' % error_supv) ax.plot(data['times'], xhat_supv2, label='supv2, error=%.3f' % error_supv2) ax.set(xlabel='time (s)', ylabel=r'$\mathbf{x}$', title="supervised test") plt.legend(loc='upper right') if DA: plt.savefig("plots/memory_%s_DA%s_supervised_test_%s.pdf" % (neuron_type, DA, test)) else: plt.savefig("plots/memory_%s_supervised_test_%s.pdf" % (neuron_type, test)) plt.close('all') else: data = go(d_ens, f_ens, n_neurons=n_neurons, t=t_sim, f=f, dt=dt, neuron_type=neuron_type, stim_func=stim_func, T=T, w_u=w_u, w_x=None, w_fb=w_fb) a_ens = f_ens.filt(data['ens'], dt=dt) u = f.filt(f.filt(T * data['u'], dt=dt), dt=dt) x = f.filt(data['x'], dt=dt) xhat_ens = np.dot(a_ens, d_ens) error_flat = rmse(xhat_ens[-int(t_test / dt):], x[-int(t_test / dt):]) error_final = rmse(xhat_ens[-1], x[-1]) error_abs = np.abs(xhat_ens[-int(t_test / dt):, 0] - x[-int(t_test / dt):, 0]) errors_flat[test] = error_flat errors_final[test] = error_final errors_abs[test] = error_abs if test > 10: continue fig, ax = plt.subplots() ax.plot(data['times'], u, linestyle="--", label='u') ax.plot(data['times'], x, linestyle="--", label='x') ax.plot(data['times'], xhat_ens, label='error_flat=%.3f, error_final=%.3f' % (error_flat, error_final)) ax.set(xlabel='time (s)', ylabel=r'$\mathbf{x}$', title="test") plt.legend(loc='upper right') if DA: plt.savefig("plots/memory_%s_DA%s_test_%s.pdf" % (DA, neuron_type, test)) else: plt.savefig("plots/memory_%s_test_%s.pdf" % (neuron_type, test)) plt.close('all') fig, ax = plt.subplots() ax.plot(data['times'][-int(t_test / dt):], error_abs, linestyle="--", label='error') ax.set(xlabel='time (s)', ylabel=r'$|\mathbf{x} - \mathbf{\hat{x}}|$', title="test") # plt.legend(loc='upper right') if DA: plt.savefig("plots/memory_abs_%s_DA%a_test_%s.pdf" % (DA, neuron_type, test)) else: plt.savefig("plots/memory_abs_%s_test_%s.pdf" % (neuron_type, test)) plt.close('all') mean_flat = np.mean(errors_flat) mean_final = np.mean(errors_final) CI_flat = sns.utils.ci(errors_flat) CI_final = sns.utils.ci(errors_final) if DA: np.savez("data/%s_DA%s_errors_abs.npz" % (neuron_type, DA), errors_abs=errors_abs) else: np.savez("data/%s_errors_abs.npz" % neuron_type, errors_abs=errors_abs) # errors = np.vstack((errors_flat, errors_final)) # names = ['flat', 'final'] # fig, ax = plt.subplots() # sns.barplot(data=errors.T) # ax.set(ylabel='RMSE') # plt.xticks(np.arange(len(names)), tuple(names), rotation=0) # plt.savefig("plots/memory_%s_errors.pdf"%neuron_type) dfs = [] columns = ("nAvg", "time", "error") for a in range(errors_abs.shape[0]): for t in range(errors_abs.shape[1]): dfs.append( pd.DataFrame([[a, dt * t, errors_abs[a, t]]], columns=columns)) df = pd.concat([df for df in dfs], ignore_index=True) fig, ax = plt.subplots() sns.lineplot(data=df, x="time", y="error", ax=ax) ax.set(xlabel='time (s)', ylabel=r'$|\mathbf{x} - \mathbf{\hat{x}}|$') fig.tight_layout() if DA: fig.savefig("plots/memory_abs_%s_DA%s_all.pdf" % (neuron_type, DA)) else: fig.savefig("plots/memory_abs_%s_all.pdf" % (neuron_type)) plt.close('all') print("errors: mean flat=%.3f, mean final=%.3f" % (np.mean(errors_flat), np.mean(errors_final))) return errors_flat, errors_final