Пример #1
0
    public void Train()
    {
        //Re-import the file to update the reference in the editor
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(trainingAnswers));
        var allLines = File.ReadAllText(AssetDatabase.GetAssetPath(trainingAnswers), Encoding.Default).Split(" \r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Where(c => c.Length == 0);

        Init(new NeuralNetwork(new int[4] {
            camera.pixelWidth *camera.pixelHeight + 1, 64, 32, 6
        }));

        float[,] trainingOutputs = new float[trainingImages.Count, 6];
        trainingOutputs          = ReadAnswersFromFile();

        //load all of the training images into a list
        foreach (string file in System.IO.Directory.GetFiles(Application.dataPath + "/Racecar Training Images/"))
        {
            if (!file.Contains(".meta") && file.Contains(".bmp"))
            {
                trainingImages.Add(LoadTexture(file));
            }
        }

        for (int i = 0; i < trainingIterations; i++)
        {
            foreach (Texture2D trainingImage in trainingImages)
            {
                //inputs = new float[trainingImage.height * trainingImage.width + 1];
                int z = 0;
                foreach (Color pixel in trainingImage.GetPixels())
                {
                    //grayscale the pixels to change them from rgb values to a single float
                    inputs[z] = pixel.grayscale;
                    z++;
                }
                float[] outputs = new float[6];
                float[] errors  = null;

                float[] errorTimesOutputs = null;

                //Run through the network with the current weights
                outputs = net.FeedForward(Sigmoid(inputs));

                errors = new float[6];



                //get the margin of error for each output and adjust the weights accordingly
                for (int x = 0; x < outputs.Length; x++)
                {
                    try
                    {
                        errors[x] = outputs[x] - trainingOutputs[trainingImages.IndexOf(trainingImage), x];
                        Debug.Log(errors[x]);
                    }
                    catch (Exception e)
                    {
                        Debug.Log("Something went wrong: " + e);
                    }
                }

                errorTimesOutputs = new float[6];
                for (int y = 0; y < errorTimesOutputs.Length; y++)
                {
                    errorTimesOutputs[y] = errors[y] * SigmoidDerivative(outputs[y]);
                }


                try
                {
                    //Get dot product of the inputs and outputs
                    float adjustments = inputs.Zip(errorTimesOutputs, (d1, d2) => d1 * d2).Sum();

                    Debug.Log(adjustments);

                    //Tell the neural network to adjust all the weights
                    net.AdjustWeights(adjustments);
                }
                catch (Exception e)
                {
                    Debug.Log("Something went wrong down here: " + e);
                }
            }
        }
    }