func TestNeuronRemoveBias(t *testing.T) { neuron := &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("neuron", 0.25), Bias: 10, } neuron.Init() NeuronRemoveBias(neuron) assert.True(t, neuron.Bias == 0) }
func TestNeuronMutateActivation(t *testing.T) { ng.SeedRandom() neuron := &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("neuron", 0.25), Bias: 10, } NeuronMutateActivation(neuron) assert.True(t, neuron.ActivationFunction != nil) assert.True(t, neuron.ActivationFunction.Name != ng.EncodableSigmoid().Name) }
func TestNeuronAddBias(t *testing.T) { // basic case where there is no bias neuron := &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("neuron", 0.25), } neuron.Init() NeuronAddBias(neuron) assert.True(t, neuron.Bias != 0) // make sure it treats 0 bias as not having a bias neuron = &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("neuron", 0.25), Bias: 0, } neuron.Init() NeuronAddBias(neuron) assert.True(t, neuron.Bias != 0) // make sure it doesn't add a bias if there is an existing one neuron = &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("neuron", 0.25), Bias: 10, } neuron.Init() NeuronAddBias(neuron) assert.True(t, neuron.Bias == 10) }
func SingleNeuronCortex(uuid string) *ng.Cortex { sensor := &ng.Sensor{ NodeId: ng.NewSensorId("sensor", 0.0), VectorLength: 1, } sensor.Init() neuron := &ng.Neuron{ ActivationFunction: ng.EncodableIdentity(), NodeId: ng.NewNeuronId("neuron", 0.15), Bias: 1, } neuron.Init() actuator := &ng.Actuator{ NodeId: ng.NewActuatorId("actuator", 0.5), VectorLength: 1, } actuator.Init() sensor.ConnectOutbound(neuron) neuron.ConnectInboundWeighted(sensor, []float64{1}) neuron.ConnectOutbound(actuator) actuator.ConnectInbound(neuron) nodeId := ng.NewCortexId(uuid) cortex := &ng.Cortex{ NodeId: nodeId, } cortex.SetSensors([]*ng.Sensor{sensor}) cortex.SetNeurons([]*ng.Neuron{neuron}) cortex.SetActuators([]*ng.Actuator{actuator}) return cortex }
func BasicCortex() *ng.Cortex { sensor := &ng.Sensor{ NodeId: ng.NewSensorId("sensor", 0.0), VectorLength: 2, } sensor.Init() hiddenNeuron1 := &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("hidden-neuron1", 0.15), Bias: -30, } hiddenNeuron1.Init() hiddenNeuron2 := &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("hidden-neuron2", 0.25), Bias: 10, } hiddenNeuron2.Init() hiddenNeuron3 := &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("hidden-neuron3", 0.35), Bias: 10, } hiddenNeuron3.Init() outputNeuron := &ng.Neuron{ ActivationFunction: ng.EncodableSigmoid(), NodeId: ng.NewNeuronId("output-neuron", 0.45), Bias: -10, } outputNeuron.Init() actuator := &ng.Actuator{ NodeId: ng.NewActuatorId("actuator", 0.5), VectorLength: 1, } actuator.Init() sensor.ConnectOutbound(hiddenNeuron1) hiddenNeuron1.ConnectInboundWeighted(sensor, []float64{20, 20}) hiddenNeuron1.ConnectOutbound(hiddenNeuron2) hiddenNeuron2.ConnectInboundWeighted(hiddenNeuron1, []float64{1}) hiddenNeuron2.ConnectOutbound(hiddenNeuron3) hiddenNeuron3.ConnectInboundWeighted(hiddenNeuron2, []float64{1}) hiddenNeuron3.ConnectOutbound(outputNeuron) outputNeuron.ConnectInboundWeighted(hiddenNeuron3, []float64{1}) outputNeuron.ConnectOutbound(actuator) actuator.ConnectInbound(outputNeuron) nodeId := ng.NewCortexId("test-cortex") cortex := &ng.Cortex{ NodeId: nodeId, } cortex.SetSensors([]*ng.Sensor{sensor}) cortex.SetNeurons([]*ng.Neuron{hiddenNeuron1, hiddenNeuron2, hiddenNeuron3, outputNeuron}) cortex.SetActuators([]*ng.Actuator{actuator}) return cortex }
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) }