示例#1
0
func TestMutatorsThatAlwaysMutate(t *testing.T) {

	testCortex := BasicCortex()
	cortexMutators := []CortexMutator{
		RemoveBias,
		MutateWeights,
		ResetWeights,
		MutateActivation,
		AddInlinkRecurrent,
		AddInlinkNonRecurrent,
		AddOutlinkRecurrent,
		AddOutlinkNonRecurrent,
		AddNeuronNonRecurrent,
		AddNeuronRecurrent,
		OutspliceRecurrent,
		OutspliceNonRecurrent,
	}
	for _, cortexMutator := range cortexMutators {
		beforeString := ng.JsonString(testCortex)
		ok, _ := cortexMutator(testCortex)
		assert.True(t, ok)
		afterString := ng.JsonString(testCortex)
		hasChanged := beforeString != afterString
		if !hasChanged {
			log.Printf("!hasChanged.  beforeString/afterString: %v", beforeString)
		}
		assert.True(t, hasChanged)
		if !hasChanged {
			break
		}

	}

}
示例#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)
}
func RunTopologyMutatingTrainer() bool {

	ng.SeedRandom()

	// training set
	examples := ng.XnorTrainingSamples()

	// create netwwork with topology capable of solving XNOR
	cortex := ng.BasicCortex()

	// verify it can not yet solve the training set (since training would be useless in that case)
	verified := cortex.Verify(examples)
	if verified {
		panic("neural net already trained, nothing to do")
	}

	shc := &nv.StochasticHillClimber{
		FitnessThreshold:           ng.FITNESS_THRESHOLD,
		MaxIterationsBeforeRestart: 20000,
		MaxAttempts:                10,
		WeightSaturationRange:      []float64{-10000, 10000},
	}

	tmt := &nv.TopologyMutatingTrainer{
		MaxAttempts:                100,
		MaxIterationsBeforeRestart: 5,
		StochasticHillClimber:      shc,
	}
	cortexTrained, succeeded := tmt.TrainExamples(cortex, examples)
	if succeeded {
		logg.LogTo("MAIN", "Successfully trained net: %v", ng.JsonString(cortexTrained))

		// verify it can now solve the training set
		verified = cortexTrained.Verify(examples)
		if !verified {
			logg.LogTo("MAIN", "Failed to verify neural net")
			succeeded = false
		}

	}

	if !succeeded {
		logg.LogTo("MAIN", "Failed to train neural net")
	}

	return succeeded

}