Пример #1
0
func TestMultipleConnectionSpikingNeuronNetwork(t *testing.T) {
	// Create a group of neurons with the default parameters.
	// Connect to a redis client.
	redisConnection, err := redis.Dial("tcp", ":6379")
	if err != nil {
		// Handle error if any...
		panic(err)
	}
	defer redisConnection.Close()

	network := make([]*sn.SpikingNeuron, numberOfSpikingNeurons)

	// Create a feed-forward network.
	for i := 0; i < numberOfSpikingNeurons; i++ {
		// Define the neuron.
		network[i] = sn.NewSpikingNeuron(defaultA, defaultB, defaultC, defaultD, int64(i), redisConnection)

		if i > 0 {
			// Create a connection from one neuron to the next.
			network[i-1].CreateConnection(network[i], defaultWeight, true, 0)
		}
	}

	// This is a hacky way to create an external connection...
	externalConnections := make([]*sn.SpikingNeuron, numberOfExternalConnections)
	for index, _ := range externalConnections {
		externalConnections[index] = sn.NewSpikingNeuron(0, 0, 0, 0, int64(-index), redisConnection)
		externalConnections[index].CreateConnection(network[0], 1.0, true, 0)
	}

	// Create a default simulation.
	simulation := sim.NewSimulation(defaultSteps, defaultTau, defaultStart, defaultStepRise)

	var testNetwork *net.Network

	for input := float64(0); input < maxInput+lazyIncrement; input = input + lazyIncrement {
		if input == 0 {
			input = 1
		}

		for _, externalSource := range externalConnections {
			for _, connection := range externalSource.GetConnections() {
				if connection.IsWriteable() {
					connection.SetOutput(input)
				}
			}
		}

		// Create a new network simulation of connections feeding from/to neurons.
		testNetwork = net.NewNetwork(network)
		testNetwork.Simulate(simulation)

		for plotIndex, neuron := range testNetwork.GetNeurons() {
			plotIndexString := alphabet[plotIndex]
			numberOfConnections := strconv.Itoa(len(neuron.GetConnections()))
			inputString := strconv.FormatFloat(input, 'f', 6, 64)
			title := "Phasic Spiking Neuron " + plotIndexString + " with connections " + numberOfConnections + " @ I = " + inputString
			xLabel := "Time Series"
			yLabel := "Membrane Potential"
			legendLabel := "Membrane Potential over Time"
			fileName := "plots/spiking-neuron-" + plotIndexString + "-" + inputString + "-with-" + numberOfConnections + "-connections.png"
			plots.GeneratePlot(simulation.GetTimeSeries(), neuron.GetOutputs(), title, xLabel, yLabel, legendLabel, fileName)

			neuron.SetSpikeRate(input, float64(neuron.GetSpikes())/defaultMeasureStart)
			neuron.ResetParameters(defaultA, defaultB, defaultC, defaultD)
		}

		if input == 1 {
			input = 0
		}
	}
}
Пример #2
0
func main() {
	// Create a new simulation with some parameters.
	var alphabet = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "O", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

	numberOfSteps := float64(500)
	interval := float64(0.25)
	start := float64(0)
	turnOn := float64(50)
	simulation := sim.NewSimulation(numberOfSteps, interval, start, turnOn)

	// Connect to the redis instance.
	redisConnection, err := redis.Dial("tcp", ":6379")
	if err != nil {
		panic(err)
	}
	defer redisConnection.Close()

	// Allocate the neurons.
	numberOfNeurons := 6
	a := float64(0.02)
	b := float64(0.25)
	c := float64(-65)
	d := float64(6)
	neurons := make([]*sn.SpikingNeuron, numberOfNeurons)
	for i := 0; i < numberOfNeurons; i++ {
		neurons[i] = sn.NewSpikingNeuron(a, b, c, d, int64(i), redisConnection)
	}

	// Create external sources to provide input to the neurons.
	numberOfExternalSources := 4
	externalSources := make([]*sn.SpikingNeuron, numberOfExternalSources)
	for i := 0; i < numberOfExternalSources; i++ {
		externalSources[i] = sn.NewSpikingNeuron(0, 0, 0, 0, int64(-i), redisConnection)
	}

	// Manually connect the external sources to the first layer neurons.
	// Create one way connections.
	externalSources[0].CreateConnection(neurons[0], 1.0, true, 0)
	externalSources[1].CreateConnection(neurons[0], 1.0, true, 0)
	externalSources[1].CreateConnection(neurons[1], 1.0, true, 0)
	externalSources[2].CreateConnection(neurons[1], 1.0, true, 0)
	externalSources[2].CreateConnection(neurons[2], 1.0, true, 0)
	externalSources[3].CreateConnection(neurons[2], 1.0, true, 0)

	// Manually connect the neurons to feed forward.
	// Layer 1...
	neurons[0].CreateConnection(neurons[3], 100.0, true, 0)
	neurons[1].CreateConnection(neurons[3], 100.0, true, 0)
	neurons[1].CreateConnection(neurons[4], 100.0, true, 0)
	neurons[2].CreateConnection(neurons[4], 100.0, true, 0)

	// Layer 2...
	neurons[3].CreateConnection(neurons[5], 100.0, true, 0)
	neurons[4].CreateConnection(neurons[5], 100.0, true, 0)

	// Create a some random data for the connections.
	externalInputs := [][]float64{
		{15.0, 15.0},
		{15.0, 15.0},
		{15.0, 15.0},
		{15.0, 15.0},
	}

	// Apply the inputs of each external source.
	for i, externalSource := range externalSources {
		for j, connection := range externalSource.GetConnections() {
			if connection.IsWriteable() {
				connection.SetOutput(externalInputs[i][j])
			}
		}
	}

	// Create the network simulation and run it.
	network := net.NewNetwork(neurons)
	network.Simulate(simulation)

	for plotIndex, neuron := range network.GetNeurons() {
		plotIndexString := alphabet[plotIndex]
		numberOfConnections := strconv.Itoa(len(neuron.GetConnections()))
		title := "Phasic Spiking Neuron " + plotIndexString + " with connections " + numberOfConnections
		xLabel := "Time Series"
		yLabel := "Membrane Potential"
		legendLabel := "Membrane Potential over Time"
		fileName := "plots/spiking-neuron-" + plotIndexString + "-with-" + numberOfConnections + "-connections.png"
		plots.GeneratePlot(simulation.GetTimeSeries(), neuron.GetOutputs(), title, xLabel, yLabel, legendLabel, fileName)
	}
}
Пример #3
0
// External Source -> Neuron A -> Neuron B -> Neuron C ... example.
func TestSingleConnectionSpikingNeuronNetwork(t *testing.T) {
	// Create a group of neurons with the default parameters.
	// Connect to a redis client.
	redisConnection, err := redis.Dial("tcp", ":6379")
	if err != nil {
		// Handle error if any...
		panic(err)
	}
	defer redisConnection.Close()

	network := make([]*sn.SpikingNeuron, numberOfSpikingNeurons)

	// Create a feed-forward network.
	for i := 0; i < numberOfSpikingNeurons; i++ {
		// Define the neuron.
		network[i] = sn.NewSpikingNeuron(defaultA, defaultB, defaultC, defaultD, int64(i), redisConnection)

		if i > 0 {
			// Create a connection from one neuron to the next.
			network[i-1].CreateConnection(network[i], defaultWeight, true, 0)
		}
	}

	// This is a hacky way to create an external connection...
	externalSource := sn.NewSpikingNeuron(0, 0, 0, 0, int64(-1), redisConnection)
	externalSource.CreateConnection(network[0], 1.0, true, 0)

	// Create a default simulation.
	simulation := sim.NewSimulation(defaultSteps, defaultTau, defaultStart, defaultStepRise)

	var testNetwork *net.Network

	for input := float64(0); input < maxInput+lazyIncrement; input = input + lazyIncrement {
		if input == 0 {
			input = 1
		}

		for _, connection := range externalSource.GetConnections() {
			if connection.IsWriteable() {
				connection.SetOutput(input)
			}
		}

		// Create a new network simulation of connections feeding from/to neurons.
		testNetwork = net.NewNetwork(network)
		testNetwork.Simulate(simulation)

		for plotIndex, neuron := range testNetwork.GetNeurons() {
			plotIndexString := alphabet[plotIndex]
			inputString := strconv.FormatFloat(input, 'f', 6, 64)
			title := "Phasic Spiking Neuron " + plotIndexString + " @ I = " + inputString
			xLabel := "Time Series"
			yLabel := "Membrane Potential"
			legendLabel := "Membrane Potential over Time"
			fileName := "plots/spiking-neuron-" + plotIndexString + "-" + inputString + ".png"
			plots.GeneratePlot(simulation.GetTimeSeries(), neuron.GetOutputs(), title, xLabel, yLabel, legendLabel, fileName)

			neuron.SetSpikeRate(input, float64(neuron.GetSpikes())/defaultMeasureStart)
			neuron.ResetParameters(defaultA, defaultB, defaultC, defaultD)
		}

		if input == 1 {
			input = 0
		}
	}

	// Measure and plot out mean spike rates.
	meansMap := make(map[int64][]float64)

	for _, neuron := range testNetwork.GetNeurons() {
		means := make([]float64, len(inputMeasurements))
		for i, selectedInput := range inputMeasurements {
			neuronSpikeRate := neuron.GetSpikeRate()
			means[i] = neuronSpikeRate[selectedInput]
		}
		meansMap[neuron.GetId()] = means
	}

	plotIndex := int(0)
	for _, value := range meansMap {
		plotIndexString := alphabet[plotIndex]
		title := "Mean Spike Rate of Phasic Spiking Neuron " + plotIndexString
		xLabel := "Input"
		yLabel := "Spike Rate"
		legendLabel := "Spike Rate as a Function of Input"
		fileName := "plots/spiking-neuron-network-mean-spike-rate-" + plotIndexString + ".png"
		plots.GeneratePlot(inputMeasurements, value, title, xLabel, yLabel, legendLabel, fileName)
		plotIndex++
	}
}