コード例 #1
0
def plotSingleTuningCurve(dataPath, gaussianDistance, ax1=None):
    b.rcParams['font.size'] = 20
    averagingWindowSize = 1
    nE = 1600
    ymax = 1
    
    if ax1==None:
        b.figure(figsize=(8,6.5))
        fig_axis = b.subplot(1,1,1)
    else:
        fig_axis = ax1
        b.sca(ax1)
        
    path = dataPath + '/dist'+str(gaussianDistance)+'/' +'activity/'
    spikeCount = np.load(path + 'spikeCountPerExample.npy')
    spikeCountSingleNeuron = (spikeCount[:,nE/2-2,0])
    numMeasurements = len(spikeCount[:,0,0])
    
    spikeCount = movingaverage(spikeCountSingleNeuron,averagingWindowSize)
    spikeCount /= np.max(spikeCountSingleNeuron)
    b.plot(spikeCount, color='deepskyblue', marker='o', alpha=0.6, linewidth=0, label='Output')#alpha=0.5+(0.5*float(i)/float(len(lower_peaks))), 
    fig_axis.set_xticks([0., numMeasurements/2, numMeasurements])
    fig_axis.set_xticklabels(['0', '0.5', '1'])
    fig_axis.set_yticks([0., ymax/2, ymax])
    fig_axis.spines['top'].set_visible(False)
    fig_axis.spines['right'].set_visible(False)
    fig_axis.get_xaxis().tick_bottom()
    fig_axis.get_yaxis().tick_left()
    b.ylabel('Normalized response')
    b.ylim(0,ymax+0.05)

#         b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount)))
    if ax1==None:
        b.legend(loc='upper left', fancybox=True, framealpha=0.0)
        b.savefig(dataPath + '/tuningCurveSingleNeuron.png', dpi=300, bbox_inches='tight')
コード例 #2
0
def plotActivity(dataPath, inputStrengths, ax1=None):
    averagingWindowSize = 10
    nE = 1600
    ymax = 40
    linewidth = 5
#     b.rcParams['lines.color'] = 'w'
#     b.rcParams['text.color'] = 'w'
#     b.rcParams['xtick.color'] = 'w'
#     b.rcParams['ytick.color'] = 'w'
#     b.rcParams['axes.labelcolor'] = 'w'
#     b.rcParams['axes.edgecolor'] = 'w'
    b.rcParams['font.size'] = 20
    if ax1==None:
        b.figure(figsize=(8,6.5))
        fig_axis = b.subplot(1,1,1)
    else:
        fig_axis = ax1
        b.sca(ax1)


    for i,inputStrength in enumerate(inputStrengths[:]):
        path = dataPath + '/in_'+str(inputStrength)+'/' +'activity/'
        spikeCount = np.loadtxt(path + 'spikeCountAe.txt')
        inputSpikeCount = np.loadtxt(path + 'spikeCountXe.txt')
        
        spikeCount = movingaverage(spikeCount,averagingWindowSize)
        inputSpikeCount = movingaverage(inputSpikeCount,averagingWindowSize)
        if i==len(inputStrengths)-1:
            b.plot([0], 'w', label=' ')#
            b.plot([0], 'w', label='Avg. input 20 Hz:')#
            b.plot(inputSpikeCount, 'coral', alpha=0.6, linewidth=linewidth, label='Input firing rate')
            b.plot(spikeCount, 'deepskyblue', alpha=0.6, linewidth=linewidth, label='Output firing rate')
        elif i==1:
            b.plot([0], 'w', label=' ')#
            b.plot([0], 'w', label='Avg. input 6 Hz:')#
            b.plot(inputSpikeCount, 'red', alpha=1., linewidth=linewidth,  label='Input firing rate')
            b.plot(spikeCount, 'blue', alpha=0.6, linewidth=linewidth, label='Output firing rate')
        elif i==0:
            b.plot([0], 'w', label='Avg. input 2 Hz:')#
            b.plot(inputSpikeCount, 'darkred', alpha=1., linewidth=linewidth, label='Input firing rate')
            b.plot(spikeCount, 'darkblue', alpha=1.0, linewidth=linewidth, label='Output firing rate')
        else: 
            b.plot(spikeCount, 'k', alpha=0.2+(0.4*float(i)/float(len(inputStrengths))), linewidth=0.6)
    b.legend(loc='upper right', fancybox=True, framealpha=0.0, fontsize = 17)




    handles, labels = fig_axis.get_legend_handles_labels()
    # fig_axis.legend(handles[::-1], labels[::-1], loc='upper left', fancybox=True, framealpha=0.0)
    fig_axis.set_xticks([0., nE/2, nE])
    fig_axis.set_yticks([0., ymax/2, ymax])
    fig_axis.spines['top'].set_visible(False)
    fig_axis.spines['right'].set_visible(False)
    fig_axis.get_xaxis().tick_bottom()
    fig_axis.get_yaxis().tick_left()
    b.xlabel('Neuron number (resorted)')
    b.ylabel('Firing rate [Hz]')
    b.ylim(0,ymax)
コード例 #3
0
def plotActivity(dataPath, inputStrengths, ax1=None):
    averagingWindowSize = 1
    nE = 1600
    ymax = 30
#     b.rcParams['lines.color'] = 'w'
#     b.rcParams['text.color'] = 'w'
#     b.rcParams['xtick.color'] = 'w'
#     b.rcParams['ytick.color'] = 'w'
#     b.rcParams['axes.labelcolor'] = 'w'
#     b.rcParams['axes.edgecolor'] = 'w'
    b.rcParams['font.size'] = 20
    if ax1==None:
        b.figure(figsize=(8,6.5))
        fig_axis = b.subplot(1,1,1)
    else:
        fig_axis = ax1
        b.sca(ax1)
    for i,inputStrength in enumerate(inputStrengths[:]):
        path = dataPath + '/peak_'+str(inputStrength)+'/' +'activity/'
        spikeCount = np.loadtxt(path + 'spikeCountAe.txt')
        inputSpikeCount = np.loadtxt(path + 'spikeCountXe.txt')
        
        spikeCount = movingaverage(spikeCount,averagingWindowSize)
        inputSpikeCount = movingaverage(inputSpikeCount,averagingWindowSize)
        if i==len(inputStrengths)-1:
            b.plot(inputSpikeCount, 'r', alpha=1., linewidth=3, label='Input')
            b.plot(spikeCount, 'deepskyblue', alpha=0.6, linewidth=3, label='Output')#alpha=0.5+(0.5*float(i)/float(len(numPeaks))), 
#         elif i==0:
#             b.plot(spikeCount, 'k--', alpha=1., linewidth=3)#
#             b.plot(inputSpikeCount, 'r--', alpha=1., linewidth=3)  
        else: 
            b.plot(spikeCount, 'k', alpha=0.2+(0.4*float(i)/float(len(inputStrength))), linewidth=0.6)
            b.plot(inputSpikeCount, 'r', alpha=0.2+(0.4*float(i)/float(len(inputStrength))), linewidth=0.6)
    fig_axis.set_xticks([0., nE/2, nE])
    fig_axis.set_xticklabels(['0', '0.5', '1'])
    fig_axis.set_yticks([0., ymax/2, ymax])
    fig_axis.spines['top'].set_visible(False)
    fig_axis.spines['right'].set_visible(False)
    fig_axis.get_xaxis().tick_bottom()
    fig_axis.get_yaxis().tick_left()
    b.ylabel('Firing Rate [Hz]')

    b.ylim(0,ymax)
#         b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount)))

    if ax1==None:
        b.xlabel('Neuron number (resorted)')
        b.legend(fancybox=True, framealpha=0.0, loc='upper left')
        b.savefig(dataPath + 'CueIntegration_single.png', dpi=900, transparent=True)
コード例 #4
0
def plotPopulationTuningCurve(dataPath, gaussianDistance, ax1=None):
    b.rcParams['font.size'] = 20
    averagingWindowSize = 1
    nE = 1600
    ymax = 1
    
    if ax1==None:
        b.figure(figsize=(8,6.5))
        fig_axis = b.subplot(1,1,1)
    else:
        fig_axis = ax1
        b.sca(ax1)
        
    
    path = dataPath + '/dist'+str(gaussianDistance)+'/' +'activity/'
    spikeCount = np.load(path + 'spikeCountPerExample.npy')
    numMeasurements = len(spikeCount[:,0,0])
    measurementSpace = np.arange(numMeasurements)
    populationSpikeCount = np.zeros((numMeasurements))
    for i in xrange(nE):
        populationSpikeCount += np.roll(spikeCount[:,i,0], int(-1.*i/nE*numMeasurements+ numMeasurements/2))
    populationSpikeCount = movingaverage(populationSpikeCount,averagingWindowSize)
    populationSpikeCount /= np.max(populationSpikeCount)
    if gaussianDistance==0.:
        mean = sum(measurementSpace*populationSpikeCount)/numMeasurements                   #note this correction
        sigma = sum(populationSpikeCount*(measurementSpace-mean)**2)/numMeasurements        #note this correction
        popt, _ = curve_fit(gaus,measurementSpace,populationSpikeCount,p0=[1,mean,sigma])
        print 'Gaussian amplitude: ', popt[0], ', mean: ', popt[1], ', std.: ', popt[2]**2
        
        b.plot(measurementSpace,gaus(measurementSpace,*popt),'k')
    
    b.plot(populationSpikeCount, color='deepskyblue', marker='o', alpha=0.6, linewidth=0, label='Output')#alpha=0.5+(0.5*float(i)/float(len(lower_peaks))), 
    fig_axis.set_xticks([0., numMeasurements/2, numMeasurements])
    fig_axis.set_xticklabels(['0', '0.5', '1'])
    fig_axis.set_yticks([0., ymax/2, ymax])
    fig_axis.spines['top'].set_visible(False)
    fig_axis.spines['right'].set_visible(False)
    fig_axis.get_xaxis().tick_bottom()
    fig_axis.get_yaxis().tick_left()
#     b.ylabel('Normalized response')
    b.ylim(0,ymax+0.05)

#         b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount)))
    if ax1==None:
        b.legend(loc='upper left', fancybox=True, framealpha=0.0)
        b.savefig(dataPath + '/tuningCurvePopulation' + str(gaussianDistance) + '.png', dpi=300, bbox_inches='tight')
コード例 #5
0
def plotActivity(dataPath, ax1=None):
    averagingWindowSize = 1
    nE = 1600
    ymax = 40
#     b.rcParams['lines.color'] = 'w'
#     b.rcParams['text.color'] = 'w'
#     b.rcParams['xtick.color'] = 'w'
#     b.rcParams['ytick.color'] = 'w'
#     b.rcParams['axes.labelcolor'] = 'w'
#     b.rcParams['axes.edgecolor'] = 'w'
    b.rcParams['font.size'] = 20
    if ax1==None:
        fig = b.figure(figsize=(8,6.5))
        fig_axis=b.subplot(1,1,1)
    else:
        fig_axis = ax1
        b.sca(ax1)
    path = dataPath + 'activity/'
    spikeCount = np.loadtxt(path + 'spikeCountCe.txt')
    popVecs = np.loadtxt(path + 'popVecs1.txt')
    desiredResult = (popVecs[0] + popVecs[1])%1.*1600
    resultMonitor = np.loadtxt(path + 'resultPopVecs1.txt')
    actualResult = resultMonitor[2]*1600
    ax1.axvline(desiredResult, color='r', linewidth=3, ymax=ymax, label='Desired result')
    ax1.axvline(actualResult, color='blue', linewidth=3, ymax=ymax, label='Population vector')
    
    print 'desiredResult', desiredResult, ', actual result', actualResult
#     spikeCount = np.roll(spikeCount, 800+int(-1*desiredResult))
    spikeCount = movingaverage(spikeCount,averagingWindowSize)
    b.plot(spikeCount, 'deepskyblue', alpha=0.6, linewidth=3, label='Population activity')
    fig_axis.set_xticks([0., nE/2, nE])
    fig_axis.set_xticklabels(['0', '0.5', '1'])
    fig_axis.set_yticks([0., ymax/2, ymax])
    fig_axis.spines['top'].set_visible(False)
    fig_axis.spines['right'].set_visible(False)
    fig_axis.get_xaxis().tick_bottom()
    fig_axis.get_yaxis().tick_left()
    b.ylabel('Firing Rate [Hz]')

    b.ylim(0,ymax)
    b.legend(fancybox=True, framealpha=0.0, loc='upper left')
#         b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount)))
    if ax1==None:
        b.xlabel('Neuron number (resorted)')
        b.savefig(dataPath + 'SignalRestoration.png', dpi=900, transparent=True)
コード例 #6
0
def plotSingleActivity(dataPath, gaussianDistance, ax1=None):
    b.rcParams['font.size'] = 20
    averagingWindowSize = 30
    nE = 1600
    ymax = 1
    
    if ax1==None:
        b.figure(figsize=(8,6.5))
        fig_axis = b.subplot(1,1,1)
    else:
        fig_axis = ax1
        b.sca(ax1)
        
    linewidth = 3
    path = dataPath + '/dist'+str(gaussianDistance)+'/' +'activity/'
    spikeCountTemp = np.load(path + 'spikeCountPerExample.npy')
    spikeCount = spikeCountTemp[25,:,0]#np.loadtxt(path + 'spikeCountAe.txt')
#     spikeCount = np.roll(spikeCount, 400)
    inputSpikeCount = np.roll(np.loadtxt(path + 'spikeCountXe.txt'), 400)
    
    spikeCount = movingaverage(spikeCount,averagingWindowSize)
    spikeCount /= np.max(spikeCount)
    inputSpikeCount = movingaverage(inputSpikeCount,averagingWindowSize)
    inputSpikeCount /= np.max(inputSpikeCount)
    b.plot(spikeCount, 'deepskyblue', alpha=0.6, linewidth=linewidth, label='Output')#alpha=0.5+(0.5*float(i)/float(len(lower_peaks))), 
    b.plot(inputSpikeCount, 'r', alpha=1., linewidth=linewidth, label='Input')
    fig_axis.set_xticks([0., nE/2, nE])
    fig_axis.set_xticklabels(['0', '0.5', '1'])
    fig_axis.set_yticks([0., ymax/2, ymax])
    fig_axis.spines['top'].set_visible(False)
    fig_axis.spines['right'].set_visible(False)
    fig_axis.get_xaxis().tick_bottom()
    fig_axis.get_yaxis().tick_left()
    b.ylabel('Normalized response')
    b.ylim(0,ymax)

#         b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount)))
    if ax1==None:
        b.legend(loc='upper left', fancybox=True, framealpha=0.0)
        b.savefig(dataPath + '/multipleAnglesSingleActivity.png', dpi=300)
コード例 #7
0
def save_and_plot_results():
	global fig_num
	
	print '...saving results'

	if not test_mode:
		save_theta()
	if not test_mode:
		save_connections()
	else:
		np.save(top_level_path + 'activity/conv_patch_connectivity_activity/results_' + str(num_examples) + '_' + ending, result_monitor)
		np.save(top_level_path + 'activity/conv_patch_connectivity_activity/input_numbers_' + str(num_examples) + '_' + ending, input_numbers)

	if do_plot:
		if rate_monitors:
			b.figure(fig_num)
			fig_num += 1
			for i, name in enumerate(rate_monitors):
				b.subplot(len(rate_monitors), 1, i + 1)
				b.plot(rate_monitors[name].times / b.second, rate_monitors[name].rate, '.')
				b.title('Rates of population ' + name)

		if spike_monitors:
			b.figure(fig_num)
			fig_num += 1
			for i, name in enumerate(spike_monitors):
				b.subplot(len(spike_monitors), 1, i + 1)
				b.raster_plot(spike_monitors[name])
				b.title('Spikes of population ' + name)

		if spike_counters:
			b.figure(fig_num)
			fig_num += 1
			for i, name in enumerate(spike_counters):
				b.subplot(len(spike_counters), 1, i + 1)
				b.plot(np.asarray(spike_counters['Ae'].count[:]))
				b.title('Spike count of population ' + name)

		plot_2d_input_weights()
		plot_patch_weights()

		b.ioff()
		b.show()
コード例 #8
0
class ConvolutionalSpikingNN(object):
	'''
	Object which represents a convolutional spiking neural network.
	'''

	def __init__(mode, connectivity, weight_dependence, post_pre, conv_size, conv_stride, conv_features, weight_sharing, lattice_structure, random_inhibition_prob, top_percent):
		'''
		Network initialization.
		'''

		# setting input parameters
		this.mode = mode
		this.connectivity = connectivity
		this.weight_dependence = weight_dependence
		this.post_pre = post_pre
		this.conv_size = conv_size
		this.conv_features = conv_features
		this.weight_sharing = weight_sharing
		this.lattice_structure = lattice_structure
		this.random_inhibition_prob = random_inhibition_prob

		# load training or testing data
		if mode == 'train':
		    start = time.time()
		    this.data = get_labeled_data(MNIST_data_path + 'training')
		    end = time.time()
		    print 'time needed to load training set:', end - start
		else:
		    start = time.time()
		    this.data = get_labeled_data(MNIST_data_path + 'testing', bTrain = False)
		    end = time.time()
		    print 'time needed to load test set:', end - start

		# set parameters for simulation based on train / test mode
		if test_mode:
			weight_path = top_level_path + 'weights/conv_patch_connectivity_weights/'
			this.num_examples = 10000 * 1
			this.do_plot_performance = False
			ee_STDP_on = False
		else:
			weight_path = top_level_path + 'random/conv_patch_connectivity_random/'
			this.num_examples = 60000 * 1
			this.do_plot_performance = True
			ee_STDP_on = True

		# plotting or not
		do_plot = True

		# number of inputs to the network
		this.n_input = 784
		this.n_input_sqrt = int(math.sqrt(n_input))

		# number of neurons parameters
		this.n_e = ((n_input_sqrt - conv_size) / conv_stride + 1) ** 2
		this.n_e_total = n_e * conv_features
		this.n_e_sqrt = int(math.sqrt(n_e))
		this.n_i = n_e
		this.conv_features_sqrt = int(math.sqrt(conv_features))

		# time (in seconds) per data example presentation and rest period in between, used to calculate total runtime
		this.single_example_time = 0.35 * b.second
		this.resting_time = 0.15 * b.second
		runtime = num_examples * (single_example_time + resting_time)

		# set the update interval
		if test_mode:
			this.update_interval = num_examples
		else:
			this.update_interval = 100

		# rest potential parameters, reset potential parameters, threshold potential parameters, and refractory periods
		v_rest_e, v_rest_i = -65. * b.mV, -60. * b.mV
		v_reset_e, v_reset_i = -65. * b.mV, -45. * b.mV
		v_thresh_e, v_thresh_i = -52. * b.mV, -40. * b.mV
		refrac_e, refrac_i = 5. * b.ms, 2. * b.ms

		# dictionaries for weights and delays
		weight, delay = {}, {}

		# populations, connections, saved connections, etc.
		input_population_names = [ 'X' ]
		population_names = [ 'A' ]
		input_connection_names = [ 'XA' ]
		save_conns = [ 'XeAe', 'AeAe' ]

		# weird and bad names for variables, I think
		input_conn_names = [ 'ee_input' ]
		recurrent_conn_names = [ 'ei', 'ie', 'ee' ]
		
		# setting weight, delay, and intensity parameters
		weight['ee_input'] = (conv_size ** 2) * 0.175
		delay['ee_input'] = (0 * b.ms, 10 * b.ms)
		delay['ei_input'] = (0 * b.ms, 5 * b.ms)
		input_intensity = start_input_intensity = 2.0

		# time constants, learning rates, max weights, weight dependence, etc.
		tc_pre_ee, tc_post_ee = 20 * b.ms, 20 * b.ms
		nu_ee_pre, nu_ee_post = 0.0001, 0.01
		wmax_ee = 1.0
		exp_ee_post = exp_ee_pre = 0.2
		w_mu_pre, w_mu_post = 0.2, 0.2

		# setting up differential equations (depending on train / test mode)
		if test_mode:
			scr_e = 'v = v_reset_e; timer = 0*ms'
		else:
			tc_theta = 1e7 * b.ms
			theta_plus_e = 0.05 * b.mV
			scr_e = 'v = v_reset_e; theta += theta_plus_e; timer = 0*ms'

		offset = 20.0 * b.mV
		v_thresh_e = '(v>(theta - offset + ' + str(v_thresh_e) + ')) * (timer>refrac_e)'

		# equations for neurons
		neuron_eqs_e = '''
				dv/dt = ((v_rest_e - v) + (I_synE + I_synI) / nS) / (100 * ms)  : volt
				I_synE = ge * nS *         -v                           : amp
				I_synI = gi * nS * (-100.*mV-v)                          : amp
				dge/dt = -ge/(1.0*ms)                                   : 1
				dgi/dt = -gi/(2.0*ms)                                  : 1
				'''
		if test_mode:
			neuron_eqs_e += '\n  theta      :volt'
		else:
			neuron_eqs_e += '\n  dtheta/dt = -theta / (tc_theta)  : volt'

		neuron_eqs_e += '\n  dtimer/dt = 100.0 : ms'

		neuron_eqs_i = '''
				dv/dt = ((v_rest_i - v) + (I_synE + I_synI) / nS) / (10*ms)  : volt
				I_synE = ge * nS *         -v                           : amp
				I_synI = gi * nS * (-85.*mV-v)                          : amp
				dge/dt = -ge/(1.0*ms)                                   : 1
				dgi/dt = -gi/(2.0*ms)                                  : 1
				'''

		# creating dictionaries for various objects
		this.neuron_groups = {}
		this.input_groups = {}
		this.connections = {}
		this.input_connections = {}
		this.stdp_methods = {}
		this.rate_monitors = {}
		this.spike_monitors = {}
		this.spike_counters = {}

		# creating excitatory, inhibitory populations
		this.neuron_groups['e'] = b.NeuronGroup(n_e_total, neuron_eqs_e, threshold=v_thresh_e, refractory=refrac_e, reset=scr_e, compile=True, freeze=True)
		this.neuron_groups['i'] = b.NeuronGroup(n_e_total, neuron_eqs_i, threshold=v_thresh_i, refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True)

		# creating subpopulations of excitatory, inhibitory neurons
		for name in population_names:
			print '...creating neuron group:', name

			# get a subgroup of size 'n_e' from all exc
			neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features * n_e)
			# get a subgroup of size 'n_i' from the inhibitory layer
			neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features * n_e)

			# start the membrane potentials of these groups 40mV below their resting potentials
			neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV
			neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV

		print '...creating recurrent connections'

		for name in population_names:
			# if we're in test mode / using some stored weights
			if mode == 'test' or weight_path[-8:] == 'weights/conv_patch_connectivity_weights/':
				# load up adaptive threshold parameters
				neuron_groups['e'].theta = np.load(weight_path + 'theta_A' + '_' + ending +'.npy')
			else:
				# otherwise, set the adaptive additive threshold parameter at 20mV
				neuron_groups['e'].theta = np.ones((n_e_total)) * 20.0 * b.mV

			for conn_type in recurrent_conn_names:
				if conn_type == 'ei':
					# create connection name (composed of population and connection types)
					conn_name = name + conn_type[0] + name + conn_type[1]
					# create a connection from the first group in conn_name with the second group
					connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0])
					# instantiate the created connection
					for feature in xrange(conv_features):
						for n in xrange(n_e):
							connections[conn_name][feature * n_e + n, feature * n_e + n] = 10.4

				elif conn_type == 'ie':
					# create connection name (composed of population and connection types)
					conn_name = name + conn_type[0] + name + conn_type[1]
					# create a connection from the first group in conn_name with the second group
					connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0])
					# instantiate the created connection
					for feature in xrange(conv_features):
						for other_feature in xrange(conv_features):
							if feature != other_feature:
								for n in xrange(n_e):
									connections[conn_name][feature * n_e + n, other_feature * n_e + n] = 17.4

					if random_inhibition_prob != 0.0:
						for feature in xrange(conv_features):
							for other_feature in xrange(conv_features):
								for n_this in xrange(n_e):
									for n_other in xrange(n_e):
										if n_this != n_other:
											if b.random() < random_inhibition_prob:
												connections[conn_name][feature * n_e + n_this, other_feature * n_e + n_other] = 17.4

				elif conn_type == 'ee':
					# create connection name (composed of population and connection types)
					conn_name = name + conn_type[0] + name + conn_type[1]
					# get weights from file if we are in test mode
					if mode == 'test':
						weight_matrix = get_matrix_from_file(weight_path + conn_name + '_' + ending + '.npy', conv_features * n_e, conv_features * n_e)
					# create a connection from the first group in conn_name with the second group
					connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0])
					# instantiate the created connection
					if connectivity == 'all':
						for feature in xrange(conv_features):
							for other_feature in xrange(conv_features):
								if feature != other_feature:
									for this_n in xrange(n_e):
										for other_n in xrange(n_e):
											if is_lattice_connection(n_e_sqrt, this_n, other_n):
												if mode == 'test':
													connections[conn_name][feature * n_e + this_n, other_feature * n_e + other_n] = weight_matrix[feature * n_e + this_n, other_feature * n_e + other_n]
												else:
													connections[conn_name][feature * n_e + this_n, other_feature * n_e + other_n] = (b.random() + 0.01) * 0.3

					elif connectivity == 'pairs':
						for feature in xrange(conv_features):
							if feature % 2 == 0:
								for this_n in xrange(n_e):
									for other_n in xrange(n_e):
										if is_lattice_connection(n_e_sqrt, this_n, other_n):
											if mode == 'test':
												connections[conn_name][feature * n_e + this_n, (feature + 1) * n_e + other_n] = weight_matrix[feature * n_e + this_n, (feature + 1) * n_e + other_n]
											else:
												connections[conn_name][feature * n_e + this_n, (feature + 1) * n_e + other_n] = (b.random() + 0.01) * 0.3
							elif feature % 2 == 1:
								for this_n in xrange(n_e):
									for other_n in xrange(n_e):
										if is_lattimode == 'test'ce_connection(n_e_sqrt, this_n, other_n):
											if mode == 'test':
												connections[conn_name][feature * n_e + this_n, (feature - 1) * n_e + other_n] = weight_matrix[feature * n_e + this_n, (feature - 1) * n_e + other_n]
											else:
												connections[conn_name][feature * n_e + this_n, (feature - 1) * n_e + other_n] = (b.random() + 0.01) * 0.3

					elif connectivity == 'none':
						pass

			# if STDP from excitatory -> excitatory is on and this connection is excitatory -> excitatory
			if ee_STDP_on and 'ee' in recurrent_conn_names:
				stdp_methods[name + 'e' + name + 'e'] = b.STDP(connections[name + 'e' + name + 'e'], eqs=eqs_stdp_ee, pre=eqs_stdp_pre_ee, post=eqs_stdp_post_ee, wmin=0., wmax=wmax_ee)

			print '...creating monitors for:', name

			# spike rate monitors for excitatory and inhibitory neuron populations
			rate_monitors[name + 'e'] = b.PopulationRateMonitor(neuron_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second)
			rate_monitors[name + 'i'] = b.PopulationRateMonitor(neuron_groups[name + 'i'], bin=(single_example_time + resting_time) / b.second)
			spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e'])

			# record neuron population spikes if specified
			spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e'])
			spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i'])

		if do_plot:
			b.figure(fig_num)
			fig_num += 1
			b.ion()
			b.subplot(211)
			b.raster_plot(spike_monitors['Ae'], refresh=1000 * b.ms, showlast=1000 * b.ms)
			b.subplot(212)
			b.raster_plot(spike_monitors['Ai'], refresh=1000 * b.ms, showlast=1000 * b.ms)

		# creating lattice locations for each patch
		if connectivity == 'all':
			lattice_locations = {}
			for this_n in xrange(conv_features * n_e):
				lattice_locations[this_n] = [ other_n for other_n in xrange(conv_features * n_e) if is_lattice_connection(n_e_sqrt, this_n % n_e, other_n % n_e) ]
		elif connectivity == 'pairs':
			lattice_locations = {}
			for this_n in xrange(conv_features * n_e):
				lattice_locations[this_n] = []
				for other_n in xrange(conv_features * n_e):
					if this_n // n_e % 2 == 0:
						if is_lattice_connection(n_e_sqrt, this_n % n_e, other_n % n_e) and other_n // n_e == this_n // n_e + 1:
							lattice_locations[this_n].append(other_n)
					elif this_n // n_e % 2 == 1:
						if is_lattice_connection(n_e_sqrt, this_n % n_e, other_n % n_e) and other_n // n_e == this_n // n_e - 1:
							lattice_locations[this_n].append(other_n)
		elif connectivity == 'none':
			lattice_locations = {}

		# setting up parameters for weight normalization between patches
		num_lattice_connections = sum([ len(value) for value in lattice_locations.values() ])
		weight['ee_recurr'] = (num_lattice_connections / conv_features) * 0.15

		# creating Poission spike train from input image (784 vector, 28x28 image)
		for name in input_population_names:
			input_groups[name + 'e'] = b.PoissonGroup(n_input, 0)
			rate_monitors[name + 'e'] = b.PopulationRateMonitor(input_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second)

		# creating connections from input Poisson spike train to convolution patch populations
		for name in input_connection_names:
			print '\n...creating connections between', name[0], 'and', name[1]
			
			# for each of the input connection types (in this case, excitatory -> excitatory)
			for conn_type in input_conn_names:
				# saved connection name
				conn_name = name[0] + conn_type[0] + name[1] + conn_type[1]

				# get weight matrix depending on training or test phase
				if mode == 'test':
					weight_matrix = get_matrix_from_file(weight_path + conn_name + '_' + ending + '.npy', n_input, conv_features * n_e)

				# create connections from the windows of the input group to the neuron population
				input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + conn_type[1]], structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1])
				
				if mode == 'test':
					for feature in xrange(conv_features):
						for n in xrange(n_e):
							for idx in xrange(conv_size ** 2):
								input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = weight_matrix[convolution_locations[n][idx], feature * n_e + n]
				else:
					for feature in xrange(conv_features):
						for n in xrange(n_e):
							for idx in xrange(conv_size ** 2):
								input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = (b.random() + 0.01) * 0.3

			# if excitatory -> excitatory STDP is specified, add it here (input to excitatory populations)
			if ee_STDP_on:
				print '...creating STDP for connection', name
				
				# STDP connection name
				conn_name = name[0] + conn_type[0] + name[1] + conn_type[1]
				# create the STDP object
				stdp_methods[conn_name] = b.STDP(input_connections[conn_name], eqs=eqs_stdp_ee, pre=eqs_stdp_pre_ee, post=eqs_stdp_post_ee, wmin=0., wmax=wmax_ee)

		print '\n'
コード例 #9
0
                                                           post = eqs_stdp_post_ee, wmin=0., wmax= wmax_ee)

    print 'create monitors for', name
    rate_monitors[name+'e'] = b.PopulationRateMonitor(neuron_groups[name+'e'], bin = (single_example_time+resting_time)/b.second)
    rate_monitors[name+'i'] = b.PopulationRateMonitor(neuron_groups[name+'i'], bin = (single_example_time+resting_time)/b.second)
    spike_counters[name+'e'] = b.SpikeCounter(neuron_groups[name+'e'])
    
    if record_spikes:
        spike_monitors[name+'e'] = b.SpikeMonitor(neuron_groups[name+'e'])
        spike_monitors[name+'i'] = b.SpikeMonitor(neuron_groups[name+'i'])

if record_spikes:
    b.figure(fig_num)
    fig_num += 1
    b.ion()
    b.subplot(211)
    b.raster_plot(spike_monitors['Ae'], refresh=1000*b.ms, showlast=1000*b.ms)
    b.subplot(212)
    b.raster_plot(spike_monitors['Ai'], refresh=1000*b.ms, showlast=1000*b.ms)


#------------------------------------------------------------------------------ 
# create input population and connections from input populations 
#------------------------------------------------------------------------------ 
pop_values = [0,0,0]
for i,name in enumerate(input_population_names):
    input_groups[name+'e'] = b.PoissonGroup(n_input, 0)
    rate_monitors[name+'e'] = b.PopulationRateMonitor(input_groups[name+'e'], bin = (single_example_time+resting_time)/b.second)

for name in input_connection_names:
    print 'create connections between', name[0], 'and', name[1]
コード例 #10
0
ファイル: csnn_pc_mnist.py プロジェクト: zhouzhou773/lm-snn
def build_network():
    global fig_num, assignments

    neuron_groups['e'] = b.NeuronGroup(n_e_total,
                                       neuron_eqs_e,
                                       threshold=v_thresh_e,
                                       refractory=refrac_e,
                                       reset=scr_e,
                                       compile=True,
                                       freeze=True)
    neuron_groups['i'] = b.NeuronGroup(n_e_total,
                                       neuron_eqs_i,
                                       threshold=v_thresh_i,
                                       refractory=refrac_i,
                                       reset=v_reset_i,
                                       compile=True,
                                       freeze=True)

    for name in population_names:
        print '...Creating neuron group:', name

        # get a subgroup of size 'n_e' from all exc
        neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features *
                                                                n_e)
        # get a subgroup of size 'n_i' from the inhibitory layer
        neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features *
                                                                n_e)

        # start the membrane potentials of these groups 40mV below their resting potentials
        neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV
        neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV

    print '...Creating recurrent connections'

    for name in population_names:
        # if we're in test mode / using some stored weights
        if test_mode:
            # load up adaptive threshold parameters
            if save_best_model:
                neuron_groups['e'].theta = np.load(
                    os.path.join(best_weights_dir,
                                 '_'.join(['theta_A', ending + '_best.npy'])))
            else:
                neuron_groups['e'].theta = np.load(
                    os.path.join(end_weights_dir,
                                 '_'.join(['theta_A', ending + '_end.npy'])))
        else:
            # otherwise, set the adaptive additive threshold parameter at 20mV
            neuron_groups['e'].theta = np.ones((n_e_total)) * 20.0 * b.mV

        for conn_type in recurrent_conn_names:
            if conn_type == 'ei':
                # create connection name (composed of population and connection types)
                conn_name = name + conn_type[0] + name + conn_type[1]
                # create a connection from the first group in conn_name with the second group
                connections[conn_name] = b.Connection(
                    neuron_groups[conn_name[0:2]],
                    neuron_groups[conn_name[2:4]],
                    structure='sparse',
                    state='g' + conn_type[0])

                # instantiate the created connection
                for feature in xrange(conv_features):
                    for n in xrange(n_e):
                        connections[conn_name][feature * n_e + n,
                                               feature * n_e + n] = 10.4

            elif conn_type == 'ie':
                # create connection name (composed of population and connections types)
                conn_name = name + conn_type[0] + name + conn_type[
                    1] + '_' + ending
                # create a connection from the first group in conn_name with the second group
                connections[conn_name] = b.Connection(
                    neuron_groups[conn_name[0:2]],
                    neuron_groups[conn_name[2:4]],
                    structure='sparse',
                    state='g' + conn_type[0])
                # instantiate the created connection with the 'weightMatrix' loaded from file
                for feature in xrange(conv_features):
                    for other_feature in xrange(conv_features):
                        if feature != other_feature:
                            for n in xrange(n_e):
                                connections[conn_name][feature * n_e + n,
                                                       other_feature * n_e +
                                                       n] = 17.4

        print '...Creating monitors for:', name

        # spike rate monitors for excitatory and inhibitory neuron populations
        rate_monitors[name + 'e'] = b.PopulationRateMonitor(
            neuron_groups[name + 'e'],
            bin=(single_example_time + resting_time) / b.second)
        rate_monitors[name + 'i'] = b.PopulationRateMonitor(
            neuron_groups[name + 'i'],
            bin=(single_example_time + resting_time) / b.second)
        spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e'])

        # record neuron population spikes if specified
        if record_spikes or plot:
            spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name +
                                                                      'e'])
            spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name +
                                                                      'i'])

    if record_spikes and plot:
        b.figure(fig_num, figsize=(8, 6))

        fig_num += 1

        b.ion()
        b.subplot(211)
        b.raster_plot(spike_monitors['Ae'],
                      refresh=1000 * b.ms,
                      showlast=1000 * b.ms,
                      title='Excitatory spikes per neuron')
        b.subplot(212)
        b.raster_plot(spike_monitors['Ai'],
                      refresh=1000 * b.ms,
                      showlast=1000 * b.ms,
                      title='Inhibitory spikes per neuron')
        b.tight_layout()

    # creating Poission spike train from input image (784 vector, 28x28 image)
    for name in input_population_names:
        input_groups[name + 'e'] = b.PoissonGroup(n_input, 0)
        rate_monitors[name + 'e'] = b.PopulationRateMonitor(
            input_groups[name + 'e'],
            bin=(single_example_time + resting_time) / b.second)

    # creating connections from input Poisson spike train to excitatory neuron population(s)
    for name in input_connection_names:
        print '\n...Creating connections between', name[0], 'and', name[1]

        # for each of the input connection types (in this case, excitatory -> excitatory)
        for conn_type in input_conn_names:
            # saved connection name
            conn_name = name[0] + conn_type[0] + name[1] + conn_type[1]

            # get weight matrix depending on training or test phase
            if test_mode:
                if save_best_model:
                    weight_matrix = np.load(
                        os.path.join(
                            best_weights_dir,
                            '_'.join([conn_name, ending + '_best.npy'])))
                else:
                    weight_matrix = np.load(
                        os.path.join(
                            end_weights_dir,
                            '_'.join([conn_name, ending + '_end.npy'])))

            # create connections from the windows of the input group to the neuron population
            input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + conn_type[1]], \
                structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1])

            if test_mode:
                for feature in xrange(conv_features):
                    for n in xrange(n_e):
                        for idx in xrange(conv_size**2):
                            input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = \
                                    weight_matrix[convolution_locations[n][idx], feature * n_e + n]
            else:
                for feature in xrange(conv_features):
                    for n in xrange(n_e):
                        for idx in xrange(conv_size**2):
                            input_connections[conn_name][
                                convolution_locations[n][idx],
                                feature * n_e + n] = (b.random() + 0.01) * 0.3

            if test_mode:
                if plot:
                    plot_weights_and_assignments(assignments)
                    fig_num += 1

        # if excitatory -> excitatory STDP is specified, add it here (input to excitatory populations)
        if not test_mode:
            print '...Creating STDP for connection', name

            # STDP connection name
            conn_name = name[0] + conn_type[0] + name[1] + conn_type[1]
            # create the STDP object
            stdp_methods[conn_name] = b.STDP(input_connections[conn_name], eqs=eqs_stdp_ee, \
                pre=eqs_stdp_pre_ee, post=eqs_stdp_post_ee, wmin=0., wmax=wmax_ee)

    print '\n'
コード例 #11
0
    def __init__(self,
                 n_input=784,
                 conv_size=16,
                 conv_stride=4,
                 conv_features=50,
                 connectivity='all',
                 weight_dependence=False,
                 post_pre=True,
                 weight_sharing=False,
                 lattice_structure='4',
                 random_lattice_prob=0.0,
                 random_inhibition_prob=0.0):
        '''
		Constructor for the spiking convolutional neural network model.

		n_input: (flattened) dimensionality of the input data
		conv_size: side length of convolution windows used
		conv_stride: stride (horizontal and vertical) of convolution windows used
		conv_features: number of convolution features (or patches) used
		connectivity: connection style between patches; one of 'none', 'pairs', all'; more to be added
		weight_dependence: whether to use weight STDP with weight dependence
		post_pre: whether to use STDP with both post- and pre-synpatic traces
		weight_sharing: whether to impose that all neurons within a convolution patch share a common set of weights
		lattice_structure: lattice connectivity pattern between patches; one of 'none', '4', '8', and 'all'
		random_lattice_prob: probability of adding random additional lattice connections between patches
		random_inhibition_prob: probability of adding random additional inhibition edges from the inhibitory to excitatory population
		'''
        self.n_input, self.conv_size, self.conv_stride, self.conv_features, self.connectivity, self.weight_dependence, \
         self.post_pre, self.weight_sharing, self.lattice_structure, self.random_lattice_prob, self.random_inhibition_prob = \
         n_input, conv_size, conv_stride, conv_features, connectivity, weight_dependence, post_pre, weight_sharing, lattice_structure, \
         random_lattice_prob, random_inhibition_prob

        # number of inputs to the network
        self.n_input_sqrt = int(math.sqrt(self.n_input))
        self.n_excitatory_patch = (
            (self.n_input_sqrt - self.conv_size) / self.conv_stride + 1)**2
        self.n_excitatory = self.n_excitatory_patch * self.conv_features
        self.n_excitatory_patch_sqrt = int(math.sqrt(self.n_excitatory_patch))
        self.n_inhibitory_patch = self.n_excitatory_patch
        self.n_inhibitory = self.n_excitatory
        self.conv_features_sqrt = int(math.ceil(math.sqrt(self.conv_features)))

        # time (in seconds) per data example presentation and rest period in between
        self.single_example_time = 0.35 * b.second
        self.resting_time = 0.15 * b.second

        # set update intervals
        self.update_interval = 100
        self.weight_update_interval = 10
        self.print_progress_interval = 10

        # rest potential parameters, reset potential parameters, threshold potential parameters, and refractory periods
        v_rest_e, v_rest_i = -65. * b.mV, -60. * b.mV
        v_reset_e, v_reset_i = -65. * b.mV, -45. * b.mV
        v_thresh_e, v_thresh_i = -52. * b.mV, -40. * b.mV
        refrac_e, refrac_i = 5. * b.ms, 2. * b.ms

        # time constants, learning rates, max weights, weight dependence, etc.
        tc_pre_ee, tc_post_ee = 20 * b.ms, 20 * b.ms
        nu_ee_pre, nu_ee_post = 0.0001, 0.01
        exp_ee_post = exp_ee_pre = 0.2
        w_mu_pre, w_mu_post = 0.2, 0.2

        # parameters for neuron equations
        tc_theta = 1e7 * b.ms
        theta_plus = 0.05 * b.mV
        scr_e = 'v = v_reset_e; theta += theta_plus; timer = 0*ms'
        offset = 20.0 * b.mV
        v_thresh_e = '(v>(theta - offset + ' + str(
            v_thresh_e) + ')) * (timer>refrac_e)'

        # equations for neurons
        neuron_eqs_e = '''
				dv / dt = ((v_rest_e - v) + (I_synE + I_synI) / nS) / (100 * ms)  : volt
				I_synE = ge * nS * - v  : amp
				I_synI = gi * nS * (-100. * mV - v)  : amp
				dge / dt = -ge / (1.0*ms)  : 1
				dgi / dt = -gi / (2.0*ms)  : 1
				dtheta / dt = -theta / (tc_theta)  : volt
				dtimer / dt = 100.0  : ms
			'''

        neuron_eqs_i = '''
				dv/dt = ((v_rest_i - v) + (I_synE + I_synI) / nS) / (10*ms)  : volt
				I_synE = ge * nS *         -v                           : amp
				I_synI = gi * nS * (-85.*mV-v)                          : amp
				dge/dt = -ge/(1.0*ms)                                   : 1
				dgi/dt = -gi/(2.0*ms)                                  : 1
			'''

        # STDP synaptic traces
        eqs_stdp_ee = '''
				dpre / dt = -pre / tc_pre_ee : 1.0
				dpost / dt = -post / tc_post_ee : 1.0
			'''

        # dictionaries for weights and delays
        self.weight, self.delay = {}, {}

        # setting weight, delay, and intensity parameters
        self.weight['ee_input'] = (conv_size**2) * 0.175
        self.delay['ee_input'] = (0 * b.ms, 10 * b.ms)
        self.delay['ei_input'] = (0 * b.ms, 5 * b.ms)
        self.input_intensity = self.start_input_intensity = 2.0
        self.wmax_ee = 1.0

        # populations, connections, saved connections, etc.
        self.input_population_names = ['X']
        self.population_names = ['A']
        self.input_connection_names = ['XA']
        self.save_connections = ['XeAe', 'AeAe']
        self.input_connection_names = ['ee_input']
        self.recurrent_connection_names = ['ei', 'ie', 'ee']

        # setting STDP update rule
        if weight_dependence:
            if post_pre:
                eqs_stdp_pre_ee = 'pre = 1.; w -= nu_ee_pre * post * w ** exp_ee_pre'
                eqs_stdp_post_ee = 'w += nu_ee_post * pre * (wmax_ee - w) ** exp_ee_post; post = 1.'

            else:
                eqs_stdp_pre_ee = 'pre = 1.'
                eqs_stdp_post_ee = 'w += nu_ee_post * pre * (wmax_ee - w) ** exp_ee_post; post = 1.'

        else:
            if post_pre:
                eqs_stdp_pre_ee = 'pre = 1.; w -= nu_ee_pre * post'
                eqs_stdp_post_ee = 'w += nu_ee_post * pre; post = 1.'

            else:
                eqs_stdp_pre_ee = 'pre = 1.'
                eqs_stdp_post_ee = 'w += nu_ee_post * pre; post = 1.'

        print '\n'

        # for filesaving purposes
        stdp_input = ''
        if self.weight_dependence:
            stdp_input += 'weight_dependence_'
        else:
            stdp_input += 'no_weight_dependence_'
        if self.post_pre:
            stdp_input += 'post_pre'
        else:
            stdp_input += 'no_post_pre'
        if self.weight_sharing:
            use_weight_sharing = 'weight_sharing'
        else:
            use_weight_sharing = 'no_weight_sharing'

        # set ending of filename saves
        self.ending = self.connectivity + '_' + str(self.conv_size) + '_' + str(self.conv_stride) + '_' + str(self.conv_features) + \
             '_' + str(self.n_excitatory_patch) + '_' + stdp_input + '_' + \
             use_weight_sharing + '_' + str(self.lattice_structure) + '_' + str(self.random_lattice_prob) + \
             '_' + str(self.random_inhibition_prob)

        self.fig_num = 1

        # creating dictionaries for various objects
        self.neuron_groups, self.input_groups, self.connections, self.input_connections, self.stdp_methods, self.rate_monitors, \
         self.spike_monitors, self.spike_counters, self.output_numbers = {}, {}, {}, {}, {}, {}, {}, {}, {}

        # creating convolution locations inside the input image
        self.convolution_locations = {}
        for n in xrange(self.n_excitatory_patch):
            self.convolution_locations[n] = [ ((n % self.n_excitatory_patch_sqrt) * self.conv_stride + (n // self.n_excitatory_patch_sqrt) \
                      * self.n_input_sqrt * self.conv_stride) + (x * self.n_input_sqrt) + y \
                      for y in xrange(self.conv_size) for x in xrange(self.conv_size) ]

        # instantiating neuron spike / votes monitor
        self.result_monitor = np.zeros(
            (self.update_interval, self.conv_features,
             self.n_excitatory_patch))

        # creating overarching neuron populations
        self.neuron_groups['e'] = b.NeuronGroup(self.n_excitatory, neuron_eqs_e, threshold=v_thresh_e, \
                     refractory=refrac_e, reset=scr_e, compile=True, freeze=True)
        self.neuron_groups['i'] = b.NeuronGroup(self.n_inhibitory, neuron_eqs_i, threshold=v_thresh_i, \
                     refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True)

        # create neuron subpopulations
        for name in self.population_names:
            print '...creating neuron group:', name

            # get a subgroup of size 'n_e' from all exc
            self.neuron_groups[name + 'e'] = self.neuron_groups['e'].subgroup(
                self.conv_features * self.n_excitatory_patch)
            # get a subgroup of size 'n_i' from the inhibitory layer
            self.neuron_groups[name + 'i'] = self.neuron_groups['i'].subgroup(
                self.conv_features * self.n_excitatory_patch)

            # start the membrane potentials of these groups 40mV below their resting potentials
            self.neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV
            self.neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV

        print '...creating recurrent connections'

        for name in self.population_names:
            # set the adaptive additive threshold parameter at 20mV
            self.neuron_groups['e'].theta = np.ones(
                (self.n_excitatory)) * 20.0 * b.mV

            for connection_type in self.recurrent_connection_names:
                if connection_type == 'ei':
                    # create connection name (composed of population and connection types)
                    connection_name = name + connection_type[
                        0] + name + connection_type[1]
                    # create a connection from the first group in conn_name with the second group
                    self.connections[connection_name] = b.Connection(self.neuron_groups[connection_name[0:2]], \
                            self.neuron_groups[connection_name[2:4]], structure='sparse', state='g' + conn_type[0])
                    # instantiate the created connection
                    for feature in xrange(self.conv_features):
                        for n in xrange(self.n_excitatory_patch):
                            self.connections[conn_name][feature * self.n_excitatory_patch + n, \
                                    feature * self.n_excitatory_patch + n] = 10.4

                elif connection_type == 'ie':
                    # create connection name (composed of population and connection types)
                    connection_name = name + connection_type[
                        0] + name + connection_type[1]
                    # create a connection from the first group in conn_name with the second group
                    self.connections[connection_name] = b.Connection(self.neuron_groups[connection_name[0:2]], \
                            self.neuron_groups[connection_name[2:4]], structure='sparse', state='g' + conn_type[0])
                    # instantiate the created connection
                    for feature in xrange(self.conv_features):
                        for other_feature in xrange(self.conv_features):
                            if feature != other_feature:
                                for n in xrange(self.n_excitatory_patch):
                                    self.connections[connection_name][feature * self.n_excitatory_patch + n, \
                                          other_feature * self.n_excitatory_patch + n] = 17.4

                    # adding random inhibitory connections as specified
                    if self.random_inhibition_prob != 0.0:
                        for feature in xrange(self.conv_features):
                            for other_feature in xrange(self.conv_features):
                                for n_this in xrange(self.n_excitatory_patch):
                                    for n_other in xrange(
                                            self.n_excitatory_patch):
                                        if n_this != n_other:
                                            if b.random(
                                            ) < self.random_inhibition_prob:
                                                self.connections[connection_name][feature * self.n_excitatory_patch + n_this, \
                                                  other_feature * self.n_excitatory_patch + n_other] = 17.4

                elif connection_type == 'ee':
                    # create connection name (composed of population and connection types)
                    connection_name = name + connection_type[
                        0] + name + connection_type[1]
                    # create a connection from the first group in conn_name with the second group
                    self.connections[connection_name] = b.Connection(self.neuron_groups[connection_name[0:2]], \
                       self.neuron_groups[connection_name[2:4]], structure='sparse', state='g' + connection_type[0])
                    # instantiate the created connection
                    if self.connectivity == 'all':
                        for feature in xrange(self.conv_features):
                            for other_feature in xrange(self.conv_features):
                                if feature != other_feature:
                                    for this_n in xrange(
                                            self.n_excitatory_patch):
                                        for other_n in xrange(
                                                self.n_excitatory_patch):
                                            if is_lattice_connection(
                                                    self.
                                                    n_excitatory_patch_sqrt,
                                                    this_n, other_n):
                                                self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \
                                                  other_feature * self.n_excitatory_patch + other_n] = \
                                                    (b.random() + 0.01) * 0.3

                    elif self.connectivity == 'pairs':
                        for feature in xrange(self.conv_features):
                            if feature % 2 == 0:
                                for this_n in xrange(self.n_excitatory_patch):
                                    for other_n in xrange(
                                            self.n_excitatory_patch):
                                        if is_lattice_connection(
                                                self.n_excitatory_patch_sqrt,
                                                this_n, other_n):
                                            self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \
                                              (feature + 1) * self.n_excitatory_patch + other_n] = (b.random() + 0.01) * 0.3
                            elif feature % 2 == 1:
                                for this_n in xrange(self.n_excitatory_patch):
                                    for other_n in xrange(
                                            self.n_excitatory_patch):
                                        if is_lattice_connection(
                                                self.n_excitatory_patch_patch,
                                                this_n, other_n):
                                            self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \
                                              (feature - 1) * self.n_excitatory_patch + other_n] = (b.random() + 0.01) * 0.3

                    elif connectivity == 'linear':
                        for feature in xrange(self.conv_features):
                            if feature != self.conv_features - 1:
                                for this_n in xrange(self.n_excitatory_patch):
                                    for other_n in xrange(
                                            self.n_excitatory_patch):
                                        if is_lattice_connection(
                                                self.n_excitatory_patch_sqrt,
                                                this_n, other_n):
                                            self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \
                                              (feature + 1) * self.n_excitatory_patch + other_n] = \
                                                 (b.random() + 0.01) * 0.3
                            if feature != 0:
                                for this_n in xrange(self.n_excitatory_patch):
                                    for other_n in xrange(
                                            self.n_excitatory_patch):
                                        if is_lattice_connection(
                                                self.n_excitatory_patch_sqrt,
                                                this_n, other_n):
                                            self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \
                                              (feature - 1) * self.n_excitatory_patch + other_n] = \
                                                 (b.random() + 0.01) * 0.3

                    elif self.connectivity == 'none':
                        pass

            # if STDP from excitatory -> excitatory is on and this connection is excitatory -> excitatory
            if 'ee' in self.recurrent_conn_names:
                self.stdp_methods[name + 'e' + name + 'e'] = b.STDP(self.connections[name + 'e' + name + 'e'], \
                            eqs=eqs_stdp_ee, pre=eqs_stdp_pre_ee, \
                            post=eqs_stdp_post_ee, wmin=0., wmax=self.wmax_ee)

            print '...creating monitors for:', name

            # spike rate monitors for excitatory and inhibitory neuron populations
            self.rate_monitors[name + 'e'] = b.PopulationRateMonitor(self.neuron_groups[name + 'e'], \
                      bin=(self.single_example_time + self.resting_time) / b.second)
            self.rate_monitors[name + 'i'] = b.PopulationRateMonitor(self.neuron_groups[name + 'i'], \
                      bin=(self.single_example_time + self.resting_time) / b.second)
            self.spike_counters[name + 'e'] = b.SpikeCounter(
                self.neuron_groups[name + 'e'])

            # record neuron population spikes
            self.spike_monitors[name + 'e'] = b.SpikeMonitor(
                self.neuron_groups[name + 'e'])
            self.spike_monitors[name + 'i'] = b.SpikeMonitor(
                self.neuron_groups[name + 'i'])

        if do_plot:
            b.figure(self.fig_num)
            fig_num += 1
            b.ion()
            b.subplot(211)
            b.raster_plot(self.spike_monitors['Ae'],
                          refresh=1000 * b.ms,
                          showlast=1000 * b.ms)
            b.subplot(212)
            b.raster_plot(self.spike_monitors['Ai'],
                          refresh=1000 * b.ms,
                          showlast=1000 * b.ms)

        # specifying locations of lattice connections
        self.lattice_locations = {}
        if self.connectivity == 'all':
            for this_n in xrange(self.conv_features * self.n_excitatory_patch):
                self.lattice_locations[this_n] = [ other_n for other_n in xrange(self.conv_features * self.n_excitatory_patch) \
                        if is_lattice_connection(self.n_excitatory_patch_sqrt, \
                        this_n % self.n_excitatory_patch, other_n % self.n_excitatory_patch) ]
        elif self.connectivity == 'pairs':
            for this_n in xrange(self.conv_features * self.n_excitatory_patch):
                self.lattice_locations[this_n] = []
                for other_n in xrange(self.conv_features *
                                      self.n_excitatory_patch):
                    if this_n // self.n_excitatory_patch % 2 == 0:
                        if is_lattice_connection(self.n_excitatory_patch_sqrt, this_n % self.n_excitatory_patch, \
                               other_n % self.n_excitatory_patch) and \
                               other_n // self.n_excitatory_patch == this_n // self.n_excitatory_patch + 1:
                            self.lattice_locations[this_n].append(other_n)
                    elif this_n // self.n_excitatory_patch % 2 == 1:
                        if is_lattice_connection(self.n_excitatory_patch_sqrt, this_n % self.n_excitatory_patch, \
                               other_n % self.n_excitatory_patch) and \
                               other_n // self.n_excitatory_patch == this_n // self.n_excitatory_patch - 1:
                            self.lattice_locations[this_n].append(other_n)
        elif self.connectivity == 'linear':
            for this_n in xrange(self.conv_features * self.n_excitatory_patch):
                self.lattice_locations[this_n] = []
                for other_n in xrange(conv_features * self.n_excitatory_patch):
                    if this_n // self.n_excitatory_patch != self.conv_features - 1:
                        if is_lattice_connection(self.n_excitatory_patch_sqrt, this_n % self.n_excitatory_patch, \
                               other_n % self.n_excitatory_patch) and \
                               other_n // self.n_excitatory_patch == this_n // self.n_excitatory_patch + 1:
                            self.lattice_locations[this_n].append(other_n)
                    elif this_n // self.n_excitatory_patch != 0:
                        if is_lattice_connection(self.n_excitatory_patch_sqrt, this_n % self.n_excitatory_patch, \
                               other_n % self.n_excitatory_patch) and \
                               other_n // self.n_excitatory_patch == this_n // self.n_excitatory_patch - 1:
                            self.lattice_locations[this_n].append(other_n)

        # setting up parameters for weight normalization between patches
        num_lattice_connections = sum(
            [len(value) for value in lattice_locations.values()])
        self.weight['ee_recurr'] = (num_lattice_connections /
                                    self.conv_features) * 0.15

        # creating Poission spike train from input image (784 vector, 28x28 image)
        for name in self.input_population_names:
            self.input_groups[name + 'e'] = b.PoissonGroup(self.n_input, 0)
            self.rate_monitors[name + 'e'] = b.PopulationRateMonitor(self.input_groups[name + 'e'], \
                       bin=(self.single_example_time + self.resting_time) / b.second)

        # creating connections from input Poisson spike train to convolution patch populations
        for name in self.input_connection_names:
            print '\n...creating connections between', name[0], 'and', name[1]

            # for each of the input connection types (in this case, excitatory -> excitatory)
            for connection_type in self.input_conn_names:
                # saved connection name
                connection_name = name[0] + connection_type[0] + name[
                    1] + connection_type[1]

                # create connections from the windows of the input group to the neuron population
                self.input_connections[connection_name] = b.Connection(self.input_groups['Xe'], \
                    self.neuron_groups[name[1] + connection_type[1]], structure='sparse', \
                    state='g' + connection_type[0], delay=True, max_delay=self.delay[connection_type][1])

                for feature in xrange(self.conv_features):
                    for n in xrange(self.n_excitatory_patch):
                        for idx in xrange(self.conv_size**2):
                            self.input_connections[connection_name][self.convolution_locations[n][idx], \
                                 feature * self.n_excitatory_patch + n] = (b.random() + 0.01) * 0.3

            # if excitatory -> excitatory STDP is specified, add it here (input to excitatory populations)
            print '...creating STDP for connection', name

            # STDP connection name
            connection_name = name[0] + connection_type[0] + name[
                1] + connection_type[1]
            # create the STDP object
            self.stdp_methods[connection_name] = b.STDP(self.input_connections[connection_name], \
              eqs=eqs_stdp_ee, pre=eqs_stdp_pre_ee, post=eqs_stdp_post_ee, wmin=0., wmax=self.wmax_ee)

        print '\n'
コード例 #12
0
ファイル: snn_mnist.py プロジェクト: zhouzhou773/lm-snn
        bin=(single_example_time + resting_time) / b.second)
    rate_monitors[name + 'i'] = b.PopulationRateMonitor(
        neuron_groups[name + 'i'],
        bin=(single_example_time + resting_time) / b.second)
    spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e'])

    # record neuron population spikes if specified
    if record_spikes:
        spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e'])
        spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i'])

if record_spikes and plot:
    b.figure(fig_num)
    fig_num += 1
    b.ion()
    b.subplot(211)
    b.raster_plot(spike_monitors['Ae'],
                  refresh=1000 * b.ms,
                  showlast=1000 * b.ms)
    b.subplot(212)
    b.raster_plot(spike_monitors['Ai'],
                  refresh=1000 * b.ms,
                  showlast=1000 * b.ms)

#################################################################
# CREATE INPUT POPULATION AND CONNECTIONS FROM INPUT POPULATION #
#################################################################

# creating convolution locations inside the input image
convolution_locations = {}
for n in xrange(n_e):
コード例 #13
0
    def run(self):
        #------------------------------------------------------------------------------ 
        # run the simulation and set inputs
        #------------------------------------------------------------------------------ 
        previousSpikeCountB = np.zeros(self.nE)
        previousSpikeCountC = np.zeros(self.nE)
        self.resultMonitor = np.zeros((self.numExamples,len(self.populationNames)))
        start = time.time()
        
        if self.recordSpikes:
            b.figure()
            b.ion()
            b.subplot(211)
            b.raster_plot(self.spikeMonitors['He'], refresh=1000*b.ms, showlast=1000*b.ms)
            b.subplot(212)
            b.raster_plot(self.spikeMonitors['Hi'], refresh=1000*b.ms, showlast=1000*b.ms)
        
#         realTimeMonitor = None
#         realTimeMonitor = rltmMon.RealtimeConnectionMonitor(self.connections['HeAe'], cmap=cm.get_cmap('gist_rainbow'), 
#                                                             wmin=0, wmax=self.wmaxEE, clock=Clock(1000*b.ms))
        
        
        for j in xrange(int(self.numExamples)):            
            if self.restingTime or j==0:
                for i,name in enumerate(self.inputPopulationNames):
                    rates = np.ones(self.nE)  * 0
                    self.inputGroups[name+'e'].rate = rates
                self.net.run(self.restingTime)#, report='text')
                
            if j%self.normalization_interval == 0:
                self.normalizeWeights()
                
            print 'set new rates of the inputs'
            self.popValues = [0]*len(self.inputPopulationNames)
            for i,name in enumerate(self.inputPopulationNames):
                if name == 'X':
                    self.popValues[i] = np.random.rand();
                    rates = self.createTopoInput(self.nE, self.popValues[i])
                    self.resultMonitor[j,0] = self.popValues[i]
                else:
                    if self.testMode:
                        rates = np.ones(self.nE)  * 0
                    elif name == 'Y':
                        self.popValues[i] = (self.popValues[0]*2) % 1.
                        rates = self.createTopoInput(self.nE, self.popValues[i])
                    elif name == 'Z':
                        self.popValues[i] = (self.popValues[0]**2) % 1.
                        rates = self.createTopoInput(self.nE, self.popValues[i])
                if self.testMode:
                    rates += noise
                self.inputGroups[name+'e'].rate = rates 
                    
                    
            print 'run number:', j+1, 'of', int(self.numExamples)
            self.net.run(self.singleExampleTime)#, report='text')
            currentSpikeCountB = np.asarray(self.spikeCounters['Be'].count[:]) - previousSpikeCountB
            currentSpikeCountC = np.asarray(self.spikeCounters['Ce'].count[:]) - previousSpikeCountC
        #     print currentSpikeCount,  np.asarray(spikeCounters['Ce'].count[:]), previousSpikeCount
            previousSpikeCountB = np.copy(self.spikeCounters['Be'].count[:])
            previousSpikeCountC = np.copy(self.spikeCounters['Ce'].count[:])
            self.resultMonitor[j,1] = self.computePopVector(currentSpikeCountB)
            self.resultMonitor[j,2] = self.computePopVector(currentSpikeCountC)
            difference = np.abs((self.resultMonitor[j,0]**2)%1. - self.resultMonitor[j,2])
            if difference > 0.5:
                difference = 1-difference
            print 'Pop. activity: ', self.resultMonitor[j,2], ', Desired activity: ', (self.resultMonitor[j,0]**2)%1., ', Difference: ', difference
            
                
                    
            if not self.testMode:
                if self.numExamples <= 1000:
                    if j%100 == 0:
                        self.saveConnections(str(j))
                else:
                    if j%1000 == 0:
                        self.saveConnections(str(j))
            
        end = time.time()
        print 'time needed to simulate:', end - start
        
        
        #------------------------------------------------------------------------------ 
        # save results
        #------------------------------------------------------------------------------ 
        print 'save results'
        
        if self.testMode:
            np.savetxt(self.dataPath + 'activity/resultPopVecs' + str(self.numExamples) + '.txt', self.resultMonitor)
        else:
            self.saveConnections(str(j))
            self.normalizeWeights()
            self.saveConnections()
コード例 #14
0
    
    neuron_groups[i] = neuron_groups['e'].subgroup(num_neurons[i])

#     print 'create monitors for', i
    rate_monitors[i] = b.PopulationRateMonitor(neuron_groups[i], bin = (single_example_time+resting_time)/b.second)
    spike_counters[i] = b.SpikeCounter(neuron_groups[i])
    
    if record_spikes:
        spike_monitors[i] = b.SpikeMonitor(neuron_groups[i])

        state_monitors[i] = b.MultiStateMonitor(neuron_groups[i], ['v'], record=[0])

if record_spikes:
    b.figure()
    b.ion()
    b.subplot(211)
    b.raster_plot(spike_monitors[1], refresh=1000*b.ms, showlast=1000*b.ms)
    b.subplot(212)
    b.raster_plot(spike_monitors[1], refresh=1000*b.ms, showlast=1000*b.ms)



#------------------------------------------------------------------------------ 
# create input population
#------------------------------------------------------------------------------ 
pop_values = [0,0,0]
for i,name in enumerate(input_population_names):
    if poisson_inputs:
        input_groups[name] = b.PoissonGroup(n_input*2, 0)
    else:
        input_groups[name] = b.SpikeGeneratorGroup(n_input*2, [])
コード例 #15
0
if not test_mode:
    save_connections()
else:
    np.save(data_path + 'activity/resultPopVecs' + str(num_examples),
            result_monitor)
    np.save(data_path + 'activity/inputNumbers' + str(num_examples),
            input_numbers)

#------------------------------------------------------------------------------
# plot results
#------------------------------------------------------------------------------
if rate_monitors:
    b.figure(fig_num)
    fig_num += 1
    for i, name in enumerate(rate_monitors):
        b.subplot(len(rate_monitors), 1, i)
        b.plot(rate_monitors[name].times / b.second, rate_monitors[name].rate,
               '.')
        b.title('Rates of population ' + name)

if spike_monitors:
    b.figure(fig_num)
    fig_num += 1
    for i, name in enumerate(spike_monitors):
        b.subplot(len(spike_monitors), 1, i)
        b.raster_plot(spike_monitors[name])
        b.title('Spikes of population ' + name)

if spike_counters:
    b.figure(fig_num)
    fig_num += 1
コード例 #16
0
    def run(self):
        #------------------------------------------------------------------------------ 
        # run the simulation and set inputs
        #------------------------------------------------------------------------------ 
        start = time.time()
        previousSpikeCount = np.zeros((self.nE,len(self.populationNames)))
        currentSpikeCount = np.zeros((self.nE,len(self.populationNames)))
        
        if self.recordSpikes:
            b.figure()
            b.ion()
            b.subplot(411)
            b.raster_plot(self.spikeMonitors['Ae'], refresh=1000*b.ms, showlast=1000*b.ms)
            b.subplot(412)
            b.raster_plot(self.spikeMonitors['Be'], refresh=1000*b.ms, showlast=1000*b.ms)
            b.subplot(413)
            b.raster_plot(self.spikeMonitors['Ce'], refresh=1000*b.ms, showlast=1000*b.ms)
            b.subplot(414)
            b.raster_plot(self.spikeMonitors['He'], refresh=1000*b.ms, showlast=1000*b.ms)
        
#         realTimeMonitor = None
#         realTimeMonitor = rltmMon.RealtimeConnectionMonitor(self.connections['HeAe'], cmap=cm.get_cmap('gist_rainbow'), 
#                                                             wmin=0, wmax=self.wmaxEE, clock=Clock(1000*b.ms))
        
        for j in xrange(int(self.numExamples)):            
            if self.restingTime or j==0:
                for i,name in enumerate(self.inputPopulationNames):
                    rates = np.ones(self.nE)  * 0
                    self.inputGroups[name+'e'].rate = rates
                self.net.run(self.restingTime)#, report='text')
                
            if j%self.normalization_interval == 0:
                self.normalizeWeights()
                
            print 'set new rates of the inputs'
            self.setNewInput(self,j)
                    
                    
            print 'run number:', j+1, 'of', int(self.numExamples)
            self.net.run(self.singleExampleTime)#, report='text')
            for i,name in enumerate(self.populationNames):
                name += 'e'
                currentSpikeCount[:,i] = np.asarray(self.spikeCounters[name].count[:]) - previousSpikeCount[:,i]
            #     print currentSpikeCount,  np.asarray(spikeCounters['Ce'].count[:]), previousSpikeCount
                previousSpikeCount[:,i] = np.copy(self.spikeCounters[name].count[:])
                self.resultMonitor[j,i] = self.computePopVector(currentSpikeCount[:,i])
                print name, 'pop. activity: ', self.resultMonitor[j,i], ', spikecount:', sum(currentSpikeCount[:,i])
            
            for i,name in enumerate(self.inputPopulationNames):
                print name, 'pop. activity: ', (self.popValues[j,i])
                
                    
            if not self.testMode:
                if self.numExamples <= 1000:
                    if j%100==0 and not j==0:
                        self.saveConnections(str(j))
                else:
                    if j%10000==0 and not j==0:
                        self.saveConnections(str(j))
            
        end = time.time()
        print 'time needed to simulate:', end - start
        
        
        #------------------------------------------------------------------------------ 
        # save results
        #------------------------------------------------------------------------------ 
        print 'save results'
        
        if self.testMode:
            np.savetxt(self.dataPath + 'activity/resultPopVecs' + str(self.numExamples) + '.txt', self.resultMonitor)
            np.savetxt(self.dataPath + 'activity/spikeCount' + str(self.gaussian_peak_low) + '.txt', 
                       self.spikeCounters['Ae'].count[:]/(self.singleExampleTime*int(self.numExamples)))
            np.savetxt(self.dataPath + 'activity/inputSpikeCount' + str(self.gaussian_peak_low) + '.txt', 
                       self.spikeCounters['Xe'].count[:]/(self.singleExampleTime*int(self.numExamples)))
            np.savetxt(self.dataPath + 'activity/popVecs' + str(self.numExamples) + '.txt', self.popValues)
        else:
            self.saveConnections(str(j))
            self.normalizeWeights()
            self.saveConnections()
コード例 #17
0
    def plotResults(self):
        #------------------------------------------------------------------------------ 
        # plot results
        #------------------------------------------------------------------------------ 
        if self.rateMonitors:
            b.figure()
            for i, name in enumerate(self.rateMonitors):
                b.subplot(len(self.rateMonitors), 1, i)
                b.plot(self.rateMonitors[name].times/b.second, self.rateMonitors[name].rate, '.')
                b.title('rates of population ' + name)
            
        if self.spikeMonitors:
            b.figure()
            for i, name in enumerate(self.spikeMonitors):
                b.subplot(len(self.spikeMonitors), 1, i)
                b.raster_plot(self.spikeMonitors[name])
                b.title('spikes of population ' + name)
                if name=='Ce':
                    timePoints = np.linspace(0+(self.singleExampleTime+self.restingTime)/(2*b.second)*1000, 
                                             self.runtime/b.second*1000-(self.singleExampleTime+self.restingTime)/(2*b.second)*1000, 
                                             self.numExamples)
                    b.plot(timePoints, self.resultMonitor[:,0]*self.nE, 'g')
                    b.plot(timePoints, self.resultMonitor[:,1]*self.nE, 'r')
        
        if self.stateMonitors:
            b.figure()
            for i, name in enumerate(self.stateMonitors):
                b.plot(self.stateMonitors[name].times/b.second, self.stateMonitors[name]['v'][0], label = name + ' v 0')
                b.legend()
                b.title('membrane voltages of population ' + name)
            
        
            b.figure()
            for i, name in enumerate(self.stateMonitors):
                b.plot(self.stateMonitors[name].times/b.second, self.stateMonitors[name]['ge'][0], label = name + ' v 0')
                b.legend()
                b.title('conductances of population ' + name)
        
        plotWeights = [
        #                 'XeAe', 
        #                 'XeAi', 
        #                 'AeAe', 
        #                 'AeAi', 
        #                 'AiAe', 
        #                 'AiAi', 
        #                'BeBe', 
        #                'BeBi', 
        #                'BiBe', 
        #                'BiBi', 
        #                'CeCe', 
        #                'CeCi', 
                        'CiCe', 
        #                'CiCi', 
        #                'HeHe', 
        #                'HeHi', 
        #                'HiHe', 
        #                'HiHi', 
                        'AeHe',
        #                 'BeHe',
        #                 'CeHe',
                        'HeAe',
        #                 'HeBe',
        #                 'HeCe',
                       ]
        
        for name in plotWeights:
            b.figure()
#             my_cmap = matplotlib.colors.LinearSegmentedColormap.from_list('own2',['#f4f4f4', '#000000'])
#             my_cmap2 = matplotlib.colors.LinearSegmentedColormap.from_list('own2',['#000000', '#f4f4f4'])
            if name[1]=='e':
                nSrc = self.nE
            else:
                nSrc = self.nI
            if name[3]=='e':
                nTgt = self.nE
            else:
                nTgt = self.nI
                
            w_post = np.zeros((nSrc, nTgt))
            connMatrix = self.connections[name][:]
            for i in xrange(nSrc):
                w_post[i, connMatrix.rowj[i]] = connMatrix.rowdata[i]
            im2 = b.imshow(w_post, interpolation="nearest", vmin = 0, cmap=cm.get_cmap('gist_ncar')) #my_cmap
            b.colorbar(im2)
            b.title('weights of connection' + name)
            
            
        if self.plotError:
            error = np.abs(self.resultMonitor[:,1] - self.resultMonitor[:,0])
            correctionIdxs = np.where(error > 0.5)[0]
            correctedError = [1 - error[i] if (i in correctionIdxs) else error[i] for i in xrange(len(error))]
            correctedErrorSum = np.average(correctedError)
                 
            b.figure()
            b.scatter(self.resultMonitor[:,1], self.resultMonitor[:,0], c=range(len(error)), cmap=cm.get_cmap('gray'))
            b.title('Error: ' + str(correctedErrorSum))
            b.xlabel('Desired activity')
            b.ylabel('Population activity')
             
            b.figure()
            error = np.abs(self.resultMonitor[:,1] - self.resultMonitor[:,0])
            correctionIdxs = np.where(error > 0.5)[0]
            correctedError = [1 - error[i] if (i in correctionIdxs) else error[i] for i in xrange(len(error))]
            correctedErrorSum = np.average(correctedError)
            b.scatter(self.resultMonitor[:,1], self.resultMonitor[:,0], c=self.resultMonitor[:,2], cmap=cm.get_cmap('gray'))
            b.title('Error: ' + str(correctedErrorSum))
            b.xlabel('Desired activity')
            b.ylabel('Population activity')
        
        b.ioff()
        b.show()
コード例 #18
0
        neuron_groups[name + 'e'],
        bin=(single_example_time + resting_time) / b.second)
    rate_monitors[name + 'i'] = b.PopulationRateMonitor(
        neuron_groups[name + 'i'],
        bin=(single_example_time + resting_time) / b.second)
    spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e'])

    if record_spikes:
        spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e'])
        spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i'])

if record_spikes:
    b.figure(fig_num)
    fig_num += 1
    b.ion()
    b.subplot(211)
    b.raster_plot(spike_monitors['Ae'],
                  refresh=1000 * b.ms,
                  showlast=1000 * b.ms)
    b.subplot(212)
    b.raster_plot(spike_monitors['Ai'],
                  refresh=1000 * b.ms,
                  showlast=1000 * b.ms)

#------------------------------------------------------------------------------
# create input population and connections from input populations
#------------------------------------------------------------------------------
pop_values = [0, 0, 0]
for i, name in enumerate(input_population_names):
    input_groups[name + 'e'] = b.PoissonGroup(n_input, 0)
    rate_monitors[name + 'e'] = b.PopulationRateMonitor(
コード例 #19
0
def plotTuningHeatmap(dataPath, gaussianDistances, ax1=None):
    b.rcParams['font.size'] = 20
#     averagingWindowSize = 1
    nE = 1600
    if ax1==None:
        b.figure(figsize=(8,15))
        fig_axis = b.subplot(1,1,1)
    else:
        b.sca(ax1)
        fig_axis=ax1
    path = dataPath + '/dist'+str(gaussianDistances[0])+'/' +'activity/'
    spikeCount = np.load(path + 'spikeCountPerExample.npy')
    numMeasurements = len(spikeCount[:,0,0])
    measurementSpace = np.arange(numMeasurements)
    populationSpikeCount = np.zeros((numMeasurements, len(gaussianDistances)))
        
    for i,gaussianDistance in enumerate(gaussianDistances):
#         distanceAngle = gaussianDistance * 2 * 360
        path = dataPath + '/dist'+str(gaussianDistance)+'/' +'activity/'
        spikeCount = np.load(path + 'spikeCountPerExample.npy')
        for j in xrange(nE):
            populationSpikeCount[:,i] += np.roll(spikeCount[:,j,0], int(-1.*j/nE*numMeasurements+ numMeasurements/2))
#         j = 0
#         populationSpikeCount[:,i] += np.roll(spikeCount[:,j,0], int(-1.*j/nE*numMeasurements+ numMeasurements/2))
#         populationSpikeCount[:,i] = movingaverage(populationSpikeCount[:,i],averagingWindowSize)
        populationSpikeCount[:,i] /= np.max(populationSpikeCount[:,i])
        
        if gaussianDistance==0.:
            mean = sum(measurementSpace*populationSpikeCount[:,i])/numMeasurements                   #note this correction
            sigma = sum(populationSpikeCount[:,i]*(measurementSpace-mean)**2)/numMeasurements        #note this correction
            popt, _ = curve_fit(gaus,measurementSpace,populationSpikeCount[:,i],p0=[1,mean,sigma])
#             tuningWidth = abs(popt[2])*2
            tuningWidth = np.sum(populationSpikeCount>=0.5)
            tuningWidthAngle = tuningWidth/50.*360.
            print 'Gaussian amplitude:', popt[0], 'mean:', popt[1], 'std.:', popt[2], \
                ', tuning width:', tuningWidth, ', tuning width angle:', tuningWidthAngle
        
        
        
    minX = max(0,round(numMeasurements/2-tuningWidth*2))
    maxX = min(numMeasurements, round(numMeasurements/2+tuningWidth*2))
    minY = round(0)
    maxY = round(tuningWidth/50.*len(gaussianDistances)*2*2)
    print minX, maxX, minY, maxY
    croppedCount = populationSpikeCount[minX:maxX,minY:maxY]
    fig_axis.imshow(croppedCount.transpose(), aspect='auto', 
                    extent=[minX, maxX, 2, 0])
#     b.colorbar()
    fig_axis.set_xlabel('Distance from preferred stimulus, \ndivided by tuning width')
    fig_axis.set_xticks([numMeasurements/2-tuningWidth*2, numMeasurements/2-tuningWidth, 
                         numMeasurements/2, numMeasurements/2+tuningWidth, numMeasurements/2+tuningWidth*2])
    fig_axis.set_xticklabels(['-2', '-1', '0', '1', '2'])
    fig_axis.set_yticks([0., 0.5, 1, 1.5, 2])
    fig_axis.spines['top'].set_visible(False)
    fig_axis.spines['right'].set_visible(False)
    fig_axis.spines['bottom'].set_visible(False)
    fig_axis.spines['left'].set_visible(False)
    fig_axis.get_xaxis().tick_bottom()
    fig_axis.get_yaxis().tick_left()
    
    if ax1==None:
        b.savefig(dataPath + '/tuningHeatmap.png', dpi=300, bbox_inches='tight')
    
    return tuningWidthAngle
コード例 #20
0
def build_network():
    global fig_num

    neuron_groups['e'] = b.NeuronGroup(n_e_total, neuron_eqs_e, threshold=v_thresh_e, \
          refractory=refrac_e, reset=scr_e, compile=True, freeze=True)
    neuron_groups['i'] = b.NeuronGroup(n_e_total, neuron_eqs_i, threshold=v_thresh_i, \
         refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True)

    for name in population_names:
        print '...Creating neuron group:', name

        # get a subgroup of size 'n_e' from all exc
        neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features *
                                                                n_e)
        # get a subgroup of size 'n_i' from the inhibitory layer
        neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features *
                                                                n_e)

        # start the membrane potentials of these groups 40mV below their resting potentials
        neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV
        neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV

    print '...Creating recurrent connections'

    for name in population_names:
        neuron_groups['e'].theta = np.load(
            os.path.join(best_weights_dir,
                         '_'.join(['theta_A', ending + '_best.npy'])))

        for conn_type in recurrent_conn_names:
            if conn_type == 'ei':
                # create connection name (composed of population and connection types)
                conn_name = name + conn_type[0] + name + conn_type[1]
                # create a connection from the first group in conn_name with the second group
                connections[conn_name] = b.Connection(
                    neuron_groups[conn_name[0:2]],
                    neuron_groups[conn_name[2:4]],
                    structure='sparse',
                    state='g' + conn_type[0])

                # instantiate the created connection
                for feature in xrange(conv_features):
                    for n in xrange(n_e):
                        connections[conn_name][feature * n_e + n,
                                               feature * n_e + n] = 10.4

            elif conn_type == 'ie':
                # create connection name (composed of population and connection types)
                conn_name = name + conn_type[0] + name + conn_type[1]

                # load weight matrix
                weight_matrix = np.load(
                    os.path.join(best_weights_dir,
                                 '_'.join([conn_name, ending, 'best.npy'])))

                # create a connection from the first group in conn_name with the second group
                connections[conn_name] = b.Connection(
                    neuron_groups[conn_name[0:2]],
                    neuron_groups[conn_name[2:4]],
                    structure='sparse',
                    state='g' + conn_type[0])

                # define the actual synaptic connections and strengths
                for feature in xrange(conv_features):
                    for other_feature in xrange(conv_features):
                        if feature != other_feature:
                            for n in xrange(n_e):
                                connections[conn_name][feature * n_e + n,
                                                       other_feature * n_e +
                                                       n] = inhibition_level

        print '...Creating monitors for:', name

        # spike rate monitors for excitatory and inhibitory neuron populations
        rate_monitors[name + 'e'] = b.PopulationRateMonitor(
            neuron_groups[name + 'e'],
            bin=(single_example_time + resting_time) / b.second)
        rate_monitors[name + 'i'] = b.PopulationRateMonitor(
            neuron_groups[name + 'i'],
            bin=(single_example_time + resting_time) / b.second)
        spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e'])

        # record neuron population spikes if specified
        if record_spikes and do_plot:
            spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name +
                                                                      'e'])
            spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name +
                                                                      'i'])

    if record_spikes and do_plot:
        b.figure(fig_num, figsize=(8, 6))
        b.ion()
        b.subplot(211)
        b.raster_plot(spike_monitors['Ae'],
                      refresh=1000 * b.ms,
                      showlast=1000 * b.ms,
                      title='Excitatory spikes per neuron')
        b.subplot(212)
        b.raster_plot(spike_monitors['Ai'],
                      refresh=1000 * b.ms,
                      showlast=1000 * b.ms,
                      title='Inhibitory spikes per neuron')
        b.tight_layout()

        fig_num += 1

    # creating Poission spike train from input image (784 vector, 28x28 image)
    for name in input_population_names:
        input_groups[name + 'e'] = b.PoissonGroup(n_input, 0)
        rate_monitors[name + 'e'] = b.PopulationRateMonitor(
            input_groups[name + 'e'],
            bin=(single_example_time + resting_time) / b.second)

    # creating connections from input Poisson spike train to excitatory neuron population(s)
    for name in input_connection_names:
        print '\n...Creating connections between', name[0], 'and', name[1]

        # for each of the input connection types (in this case, excitatory -> excitatory)
        for conn_type in input_conn_names:
            # saved connection name
            conn_name = name[0] + conn_type[0] + name[1] + conn_type[1]

            # get weight matrix depending on training or test phase
            weight_matrix = np.load(
                os.path.join(best_weights_dir,
                             '_'.join([conn_name, ending + '_best.npy'])))

            # create connections from the windows of the input group to the neuron population
            input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + conn_type[1]], \
                structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1])

            for feature in xrange(conv_features):
                for n in xrange(n_e):
                    for idx in xrange(conv_size**2):
                        input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = \
                                weight_matrix[convolution_locations[n][idx], feature * n_e + n]

            if do_plot:
                plot_2d_input_weights()
                fig_num += 1

    print '\n'
コード例 #21
0
def build_network():
    global fig_num

    neuron_groups['e'] = b.NeuronGroup(n_e_total,
                                       neuron_eqs_e,
                                       threshold=v_thresh_e,
                                       refractory=refrac_e,
                                       reset=scr_e,
                                       compile=True,
                                       freeze=True)
    neuron_groups['i'] = b.NeuronGroup(n_e_total,
                                       neuron_eqs_i,
                                       threshold=v_thresh_i,
                                       refractory=refrac_i,
                                       reset=v_reset_i,
                                       compile=True,
                                       freeze=True)

    for name in ['A']:
        print '...Creating neuron group:', name

        # get a subgroup of size 'n_e' from all exc
        neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features *
                                                                n_e)
        # get a subgroup of size 'n_i' from the inhibitory layer
        neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features *
                                                                n_e)

        # start the membrane potentials of these groups 40mV below their resting potentials
        neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV
        neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV

    print '...Creating recurrent connections'

    for name in ['A']:
        neuron_groups['e'].theta = np.load(
            os.path.join(best_weights_dir,
                         '_'.join(['theta_A', ending + '_best.npy'])))

        for conn_type in ['ei', 'ie']:
            if conn_type == 'ei':
                # create connection name (composed of population and connection types)
                conn_name = name + conn_type[0] + name + conn_type[1]
                # create a connection from the first group in conn_name with the second group
                connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], \
                    neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0])
                # instantiate the created connection
                for feature in xrange(conv_features):
                    for n in xrange(n_e):
                        connections[conn_name][feature * n_e + n,
                                               feature * n_e + n] = 10.4

            elif conn_type == 'ie' and not remove_inhibition:
                # create connection name (composed of population and connection types)
                conn_name = name + conn_type[0] + name + conn_type[1]
                # create a connection from the first group in conn_name with the second group
                connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], \
                    neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0])
                # define the actual synaptic connections and strengths
                for feature in xrange(conv_features):
                    if inhib_scheme in ['far', 'strengthen']:
                        for other_feature in set(range(conv_features)) - set(
                                neighbor_mapping[feature]):
                            if inhib_scheme == 'far':
                                for n in xrange(n_e):
                                    connections[conn_name][feature * n_e + n,
                                                           other_feature *
                                                           n_e + n] = 17.4

                            elif inhib_scheme == 'strengthen':
                                if n_e == 1:
                                    x, y = feature // np.sqrt(
                                        n_e_total), feature % np.sqrt(
                                            n_e_total)
                                    x_, y_ = other_feature // np.sqrt(
                                        n_e_total), other_feature % np.sqrt(
                                            n_e_total)
                                else:
                                    x, y = feature // np.sqrt(
                                        conv_features), feature % np.sqrt(
                                            conv_features)
                                    x_, y_ = other_feature // np.sqrt(
                                        conv_features
                                    ), other_feature % np.sqrt(conv_features)

                                for n in xrange(n_e):
                                    connections[conn_name][feature * n_e + n, other_feature * n_e + n] = \
                                        min(17.4, inhib_const * np.sqrt(euclidean([x, y], [x_, y_])))

                    elif inhib_scheme == 'increasing':
                        for other_feature in xrange(conv_features):
                            if n_e == 1:
                                x, y = feature // np.sqrt(
                                    n_e_total), feature % np.sqrt(n_e_total)
                                x_, y_ = other_feature // np.sqrt(
                                    n_e_total), other_feature % np.sqrt(
                                        n_e_total)
                            else:
                                x, y = feature // np.sqrt(
                                    conv_features), feature % np.sqrt(
                                        conv_features)
                                x_, y_ = other_feature // np.sqrt(
                                    conv_features), other_feature % np.sqrt(
                                        conv_features)

                            if feature != other_feature:
                                for n in xrange(n_e):
                                    connections[conn_name][feature * n_e + n, other_feature * n_e + n] = \
                                        min(17.4, inhib_const * np.sqrt(euclidean([x, y], [x_, y_])))

                    else:
                        raise Exception(
                            'Expecting one of "far", "increasing", or "strengthen" for argument "inhib_scheme".'
                        )

        # spike rate monitors for excitatory and inhibitory neuron populations
        rate_monitors[name + 'e'] = b.PopulationRateMonitor(
            neuron_groups[name + 'e'],
            bin=(single_example_time + resting_time) / b.second)
        rate_monitors[name + 'i'] = b.PopulationRateMonitor(
            neuron_groups[name + 'i'],
            bin=(single_example_time + resting_time) / b.second)
        spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e'])

        # record neuron population spikes if specified
        if record_spikes:
            spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name +
                                                                      'e'])
            spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name +
                                                                      'i'])

    if record_spikes and do_plot:
        if reset_state_vars:
            time_window = single_example_time * 1000
        else:
            time_window = (single_example_time + resting_time) * 1000

        b.figure(fig_num, figsize=(8, 6))
        b.ion()
        b.subplot(211)
        b.raster_plot(spike_monitors['Ae'],
                      refresh=time_window * b.ms,
                      showlast=time_window * b.ms,
                      title='Excitatory spikes per neuron')
        b.subplot(212)
        b.raster_plot(spike_monitors['Ai'],
                      refresh=time_window * b.ms,
                      showlast=time_window * b.ms,
                      title='Inhibitory spikes per neuron')
        b.tight_layout()

        fig_num += 1

    # creating Poission spike train from input image (784 vector, 28x28 image)
    for name in ['X']:
        input_groups[name + 'e'] = b.PoissonGroup(n_input, 0)
        rate_monitors[name + 'e'] = b.PopulationRateMonitor(
            input_groups[name + 'e'],
            bin=(single_example_time + resting_time) / b.second)

    # creating connections from input Poisson spike train to convolution patch populations
    for name in ['XA']:
        print '\n...Creating connections between', name[0], 'and', name[1]

        # for each of the input connection types (in this case, excitatory -> excitatory)
        for conn_type in ['ee_input']:
            # saved connection name
            conn_name = name[0] + conn_type[0] + name[1] + conn_type[1]

            # get weight matrix depending on training or test phase
            weight_matrix = np.load(
                os.path.join(best_weights_dir,
                             '_'.join([conn_name, ending + '_best.npy'])))

            # create connections from the windows of the input group to the neuron population
            input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + \
              conn_type[1]], structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1])

            for feature in xrange(conv_features):
                for n in xrange(n_e):
                    for idx in xrange(conv_size**2):
                        input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = \
                             weight_matrix[convolution_locations[n][idx], feature * n_e + n]

            if do_plot:
                plot_2d_input_weights()
                fig_num += 1