Ejemplo n.º 1
0
func TestNeuronMutateWeights(t *testing.T) {

	xnorCortex := ng.XnorCortex()
	neuron := xnorCortex.NeuronUUIDMap()["output-neuron"]
	assert.True(t, neuron != nil)
	neuronCopy := neuron.Copy()

	foundModifiedWeight := false
	for i := 0; i < 100; i++ {

		didMutateWeights, _ := NeuronMutateWeights(neuron)
		if didMutateWeights == true {

			foundModifiedWeight = verifyWeightsModified(neuron, neuronCopy)

		}

		if foundModifiedWeight == true {
			break
		}

	}

	assert.True(t, foundModifiedWeight == true)

}
Ejemplo n.º 2
0
func TestAddBias(t *testing.T) {
	xnorCortex := ng.XnorCortex()
	for _, neuron := range xnorCortex.Neurons {
		neuron.Bias = 0.0
	}
	beforeString := ng.JsonString(xnorCortex)
	AddBias(xnorCortex)
	afterString := ng.JsonString(xnorCortex)
	assert.True(t, beforeString != afterString)
}
Ejemplo n.º 3
0
func TestNeuronAddInlinkRecurrent(t *testing.T) {

	madeNonRecurrentInlink := false
	madeRecurrentInlink := false

	for i := 0; i < 100; i++ {
		xnorCortex := ng.XnorCortex()
		neuron := xnorCortex.NeuronUUIDMap()["output-neuron"]
		ok, mutateResult := NeuronAddInlinkRecurrent(neuron)
		if !ok {
			continue
		}
		inboundConnection := mutateResult.(*ng.InboundConnection)
		if neuron.IsInboundConnectionRecurrent(inboundConnection) {

			// the first time we make a nonRecurrentInlink,
			// test the network out
			if madeRecurrentInlink == false {
				// make sure the network actually works
				examples := ng.XnorTrainingSamples()
				fitness := xnorCortex.Fitness(examples)
				assert.True(t, fitness >= 0)

			}

			madeRecurrentInlink = true
		} else {

			// the first time we make a nonRecurrentInlink,
			// test the network out
			if madeNonRecurrentInlink == false {
				// make sure the network doesn't totally break
				examples := ng.XnorTrainingSamples()
				fitness := xnorCortex.Fitness(examples)
				assert.True(t, fitness >= 0)
			}

			madeNonRecurrentInlink = true

		}

	}

	assert.True(t, madeNonRecurrentInlink)
	assert.True(t, madeRecurrentInlink)

}
Ejemplo n.º 4
0
func TestPerturbParameters(t *testing.T) {

	cortex := ng.XnorCortex()

	nnJson, _ := json.Marshal(cortex)
	nnJsonString := fmt.Sprintf("%s", nnJson)

	saturationBounds := []float64{-100000, 10000}
	PerturbParameters(cortex, saturationBounds)

	nnJsonAfter, _ := json.Marshal(cortex)
	nnJsonStringAfter := fmt.Sprintf("%s", nnJsonAfter)

	// the json should be different after we perturb it
	assert.NotEquals(t, nnJsonString, nnJsonStringAfter)

}
Ejemplo n.º 5
0
func TestNeuronAddInlinkNonRecurrent(t *testing.T) {

	ng.SeedRandom()

	madeNonRecurrentInlink := false
	madeRecurrentInlink := false
	firstTime := true

	// since it's stochastic, repeat the operation many times and make
	// sure that it always produces expected behavior
	for i := 0; i < 100; i++ {

		xnorCortex := ng.XnorCortex()
		sensor := xnorCortex.Sensors[0]
		neuron := xnorCortex.NeuronUUIDMap()["output-neuron"]
		hiddenNeuron1 := xnorCortex.NeuronUUIDMap()["hidden-neuron1"]
		targetLayerIndex := hiddenNeuron1.NodeId.LayerIndex

		// add a new neuron at the same layer index as the hidden neurons
		hiddenNeuron3 := &ng.Neuron{
			ActivationFunction: ng.EncodableSigmoid(),
			NodeId:             ng.NewNeuronId("hidden-neuron3", targetLayerIndex),
			Bias:               -30,
		}

		hiddenNeuron3.Init()
		xnorCortex.Neurons = append(xnorCortex.Neurons, hiddenNeuron3)
		weights := randomWeights(sensor.VectorLength)
		sensor.ConnectOutbound(hiddenNeuron3)
		hiddenNeuron3.ConnectInboundWeighted(sensor, weights)

		ok, mutateResult := NeuronAddInlinkNonRecurrent(neuron)
		if !ok {
			continue
		}
		inboundConnection := mutateResult.(*ng.InboundConnection)

		if neuron.IsInboundConnectionRecurrent(inboundConnection) {
			madeRecurrentInlink = true
		} else {
			madeNonRecurrentInlink = true
		}

		if firstTime == true {

			// only two possibilities - the hiddenNeuron3 or the
			// sensor.  if it was the sensor, then the hiddenNeuron3
			// is "dangliing" and so lets connect it
			if inboundConnection.NodeId.UUID == "sensor" {
				weights2 := randomWeights(1)
				hiddenNeuron3.ConnectOutbound(neuron)
				neuron.ConnectInboundWeighted(hiddenNeuron3, weights2)
			}

			// run network make sure it runs
			examples := ng.XnorTrainingSamples()
			fitness := xnorCortex.Fitness(examples)
			assert.True(t, fitness >= 0)

			firstTime = false
		}

	}

	assert.True(t, madeNonRecurrentInlink)
	assert.False(t, madeRecurrentInlink)

}