Ejemplo n.º 1
0
    public unsafe void TrainAndTest()
    {
        Debug.Log("Training and Testing");

        var    Y = new double[trainSpheresTransforms.Length];
        IntPtr model;

        var linear_inputs = new double[trainSpheresTransforms.Length * 2];

        for (int i = 0; i < trainSpheresTransforms.Length; i++)
        {
            var position = new Vector3();
            if (transfo != -1)
            {
                position = AppliTransfo(trainSpheresTransforms[i].position);
            }
            else
            {
                position = trainSpheresTransforms[i].position;
            }
            Y[i] = position.y;
            linear_inputs[i * 2]       = position.x;
            linear_inputs[(i * 2) + 1] = position.z;
        }

        // Create Model
        model = VisualStudioLibWrapper.linear_model_create(2);
        // Train Model
        VisualStudioLibWrapper.linear_model_train_regression(model, linear_inputs, trainSpheresTransforms.Length, 2, Y, Y.Length, 1000000, 0.01f);

        // For each testSphere : Predict
        foreach (var testSpheres in testSpheresTransforms)
        {
            var position = new Vector3();
            if (transfo != -1)
            {
                position = AppliTransfo(testSpheres.position);
            }
            else
            {
                position = testSpheres.position;
            }
            double[] input = { position.x, position.z };
            float    y     = (float)VisualStudioLibWrapper.linear_model_predict_regression(model, input, 2);
            testSpheres.position = new Vector3(
                position.x,
                y,
                position.z
                );
        }
    }
    void RegressionTrain(IntPtr model)
    {
        Debug.Log("Train Model");
        List <double> inputs    = new List <double>();
        List <double> expecteds = new List <double>();

        foreach (var trainSpheresTransform in trainSpheresTransforms)
        {
            inputs.Add(trainSpheresTransform.position.x);
            inputs.Add(trainSpheresTransform.position.z);

            expecteds.Add(trainSpheresTransform.position.y);
        }

        VisualStudioLibWrapper.linear_model_train_regression(model, inputs.ToArray(), inputs.Count() / 2, 2, expecteds.ToArray());
    }