コード例 #1
0
    private float MSR(float[][] data, MFNN network)
    {
        int input_size  = network.GetInputSize();
        int output_size = network.GetOutputSize();

        //Error Checking
        Debug.Assert(data.Length > 0);
        Debug.Assert(data[0].Length == input_size + output_size);

        float msr = 0;

        for (int i = 0; i < data.Length; i++)
        {
            float[] x_values = new float[input_size];
            float[] t_values = new float[output_size];
            Array.Copy(data[i], 0, x_values, 0, input_size);
            Array.Copy(data[i], input_size, t_values, 0, output_size);

            float[] y_values = network.Compute(x_values);

            float sum = 0;
            for (int j = 0; j < output_size; j++)
            {
                sum += (t_values[j] - y_values[j]) * (t_values[j] - y_values[j]);
            }
            msr += sum;
        }

        return(msr);
    }
コード例 #2
0
    public void StartTraining()
    {
        last_q_values  = new float[actions.Length];
        abort_learning = false;
        current_step   = 0;
        current_reward = 0;
        particle_step  = 0;

        //Setup car components
        car_camera     = GetComponent <CarCamera>();
        car_controller = GetComponent <CarController>();
        car_body       = transform.GetChild(0).GetComponent <Rigidbody>();

        //Setup PSO
        working_particle = 0;
        particles        = new MFNN[max_particles];
        for (int i = 0; i < particles.Length; i++)
        {
            particles[i] = CreateMFNN();
        }
        particle_swarm = new PSO(particles);
        network_target = CreateMFNN();

        StartCoroutine(TakeStep());
    }
コード例 #3
0
    public void StartTraining()
    {
        //Setup
        action_index  = 0;
        current_step  = 0;
        learn_step    = 0;
        is_activated  = true;
        load_weights  = false;
        log           = new List <string>();
        last_q_values = new float[actions.Length];

        //Setup memory
        memory_index   = 0;
        network_memory = new MemoryStructure[memory_size];

        //Setup networks
        network_eval = new MFNN(
            new int[] { 5, 20, actions.Length },
            new ActivationType[] {
            ActivationType.NONE,
            ActivationType.ReLU,
            ActivationType.ReLU
        });
        network_target = new MFNN(
            new int[] { 5, 20, actions.Length },
            new ActivationType[] {
            ActivationType.NONE,
            ActivationType.ReLU,
            ActivationType.ReLU
        });

        //Setup epsilon
        max_epsilon = 0.9f;
        epsilon     = 0.0f;

        //Setup car components
        car_camera     = GetComponent <CarCamera>();
        car_controller = GetComponent <CarController>();
        car_body       = gameObject.transform.GetChild(0).gameObject.GetComponent <Rigidbody>();

        //Reset Car
        car_body.transform.position = car_spawner.transform.position;
        car_body.transform.rotation = car_spawner.transform.rotation;
        car_body.velocity           = Vector3.zero;
        car_body.angularVelocity    = Vector3.zero;
        action_index   = 0;
        reset_step     = current_step;
        current_reward = 0;

        //Start loop
        StartCoroutine(DQNStep());
        log.Add("Training Started");
    }
コード例 #4
0
    private void Start()
    {
        MFNN network = new MFNN(new int[] { 4, 7, 3 }, new ActivationType[] {
            ActivationType.NONE,
            ActivationType.LOGISTIC_SIGMOID,
            ActivationType.LOGISTIC_SIGMOID
        });

        int[] shuffle = ShuffleArray(IrisData.dataset.Length);
        Debug.Log("Initial Error: " + MSR(IrisData.dataset, network));

        int input_size  = network.GetInputSize();
        int output_size = network.GetOutputSize();

        int r = 0;

        while (r < 100)
        {
            for (int i = 0; i < IrisData.dataset.Length - 20; i++)
            {
                float[] x_values = new float[input_size];
                float[] t_values = new float[output_size];
                Array.Copy(IrisData.dataset[shuffle[i]], 0, x_values, 0, input_size);
                Array.Copy(IrisData.dataset[shuffle[i]], input_size, t_values, 0, output_size);

                float[] y_values = network.Compute(x_values);
                float[] errors   = new float[output_size];
                for (int j = 0; j < output_size; j++)
                {
                    errors[j] = t_values[j] - y_values[j];
                }
                network.UpdateWeights(errors, 0.01f, 0.0001f, 0.5f);
            }
            Debug.Log("Itr. " + r + " MSR: " + MSR(IrisData.dataset, network));
            r++;
        }

        Debug.Log("Testing");
        for (int i = IrisData.dataset.Length - 21; i < IrisData.dataset.Length; i++)
        {
            float[] x_values = new float[input_size];
            float[] t_values = new float[output_size];
            Array.Copy(IrisData.dataset[shuffle[i]], 0, x_values, 0, input_size);
            Array.Copy(IrisData.dataset[shuffle[i]], input_size, t_values, 0, output_size);
            float[] y_values = network.Compute(x_values);
            int     max1     = 0;
            int     max2     = 0;
            for (int j = 0; j < t_values.Length; j++)
            {
                if (t_values[max1] < t_values[j])
                {
                    max1 = j;
                }
                if (y_values[max2] < y_values[j])
                {
                    max2 = j;
                }
            }
            if (max1 == max2)
            {
                Debug.Log("GOOD");
            }
            else
            {
                Debug.Log("BAD");
            }
        }
    }