Exemplo n.º 1
0
    private INeuralModel Init2InputsNeuralModel()
    {
        var model = new NeuralModelBase();

        model.defaultWeightInitializer = () => GARandomManager.NextFloat(-1, 1);;

        model.WeightConstraints = new Tuple <float, float>(-5f, 5f);

        var bias   = model.AddBiasNeuron();
        var layers = new List <Neuron[]>()
        {
            model.AddInputNeurons(CartPoleAgent.nbOfInputs).ToArray(),

            model.AddOutputNeurons(
                1,
                ActivationFunctions.TanH
                ).ToArray(),
        };

        model.ConnectLayers(layers);

        var outputNeuron = layers.Last().Last();
        //var memNeurons = model.AddNeurons(
        //          sampleNeuron: new MemoryNeuron(-1, outputNeuron.InnovationNb),
        //          count: 1
        //).ToArray();

        // RNN

        //var innerMemNeurons = model.AddNeurons(
        //	sampleNeuron: new Neuron(-1, ActivationFunctions.TanH),
        //	count: 1).ToArray();

        //model.ConnectLayers(new[] { memNeurons, innerMemNeurons });
        //model.ConnectLayers(new[] { layers[0], innerMemNeurons, new[] { outputNeuron } });

        // LSTM
        Neuron lstmIn, lstmOut;

        model.AddLSTM(out lstmIn, out lstmOut, biasNeuron: bias);
        //model.ConnectNeurons(memNeurons, new[] { lstmIn }).ToArray();
        model.ConnectNeurons(layers[0], new[] { lstmIn }).ToArray();
        model.ConnectNeurons(new[] { lstmOut }, layers.Last()).ToArray();

        return(model);
    }
    protected override INeuralModel InitNeuralModel()
    {
        var model = new NeuralModelBase();

        model.defaultWeightInitializer = () => GARandomManager.NextFloat(-1, 1);

        model.WeightConstraints = new Tuple <float, float>(
            weightConstraints.x,
            weightConstraints.y
            );

        var bias = model.AddBiasNeuron();

        var layers = new List <Neuron[]>()
        {
            // Inputs
            model.AddInputNeurons(
                agentPrefab.GetComponent <AgentProxy>().nbOfInputs
                ).ToArray(),

            //model.AddNeurons(
            //    new Neuron(-1, ActivationFunctions.TanH),
            //    count: 4
            //).ToArray(),

            // Outputs
            model.AddOutputNeurons(
                agentPrefab.GetComponent <AgentProxy>().nbOfOutputs,
                ActivationFunctions.Sigmoid
                ).ToArray(),
        };

        model.ConnectLayers(layers);
        model.ConnectBias(bias, layers.Skip(1));

        //var memoryNeurons = new List<Neuron>();

        //foreach (var neuron in layers.Last())
        //{
        //	var mem = model.AddNeurons(
        //            sampleNeuron: new MemoryNeuron(-1, neuron.InnovationNb),
        //            count: 1
        //          );
        //	memoryNeurons.Add(mem[0]);
        //}

        //var memoryProcLayer1 = model.AddNeurons(
        //	new Neuron(-1, ActivationFunctions.TanH),
        //	count: 4
        //).ToArray();

        //var memoryProcLayer2 = model.AddNeurons(
        //          new Neuron(-1, ActivationFunctions.TanH),
        //          count: 4
        //      ).ToArray();

        //model.ConnectLayers(
        //	new Neuron[][]
        //          {
        //              memoryNeurons.ToArray(),
        //              memoryProcLayer1,
        //	    memoryProcLayer2,
        //              layers.Last()
        //          }
        //);

        Neuron lstmIn, lstmOut;

        model.AddLSTM(out lstmIn, out lstmOut, biasNeuron: bias);
        model.ConnectNeurons(layers[0], new[] { lstmIn }).ToArray();
        model.ConnectNeurons(new[] { lstmOut }, layers.Last()).ToArray();

        return(model);
    }