Пример #1
0
        public void Train(float[] expected_output)
        {
            errors = new float[DNA.Length];
            float[] output_error;
            float[] _error, tmp, prev_output, next_output;
            float[,] d;

            for (int i = matrices.Length - 1; i >= 0; i--)
            {
                if (i == matrices.Length - 1)
                {
                    next_output = output;
                    output_error = MatrixTools.SubtractVV(expected_output, output);
                    /*Console.WriteLine("=====================================================================");
                    Console.WriteLine("expected_output: " + MatrixTools.Vector2Str(expected_output));
                    Console.WriteLine("output: " + MatrixTools.Vector2Str(output));
                    Console.WriteLine("output_error: " + MatrixTools.Vector2Str(output_error));*/
                }
                else
                {
                    next_output = GetVector(layers[i].ToArray());
                    output_error = GetErrors(layers[i]);
                }

                if (i == 0)
                {
                    prev_output = input;
                }
                else
                {
                    prev_output = GetVector(incoming_vectors_keys[i]);
                }

                _error = MatrixTools.MultiplyVI(output_error, GetLearningRate());

                tmp = MatrixTools.SubtractFV(1, next_output);

                tmp = MatrixTools.MultiplyVV(_error, next_output, tmp);
                d = MatrixTools.MultiplyVVAsMatrix(tmp, prev_output);

                output_error = MatrixTools.MultiplyMV(MatrixTools.TransposeM(matrices[i]), _error);

                if (i > 0)
                    PutErrors(incoming_vectors_keys[i], output_error);

                matrices[i] = MatrixTools.AddMM(matrices[i], d);
            }
        }
Пример #2
0
        public float[] Query(float[] _input)
        {
            this.input = _input;

            float[] Input = MatrixTools.MultiplyMV(matrices[0], _input);//умножаем матрицу 0 на вектор входящего сигнала, получаем инпут слоя 1
            float[] Output = ActivationFunction(Input);//применяем функцию активации к инпуту слоя 1, получаем аутпут слоя 1

            SetVector(layers[0], Output);//записываем значения первого слоя

            for (int l = 1; l < layers.Length; l++)
            {
                //Console.WriteLine(MatrixTools.Matrix2String(matrices[l]));
                Input = MatrixTools.MultiplyMV(matrices[l], GetVector(incoming_vectors_keys[l]));
                Output = ActivationFunction(Input);

                SetVector(layers[l], Output);
            }

            Input = MatrixTools.MultiplyMV(matrices[matrices.Length - 1], GetVector(incoming_vectors_keys[incoming_vectors_keys.Length - 1]));

            return output = ActivationFunction(Input);
        }