Ejemplo n.º 1
0
        public static List <float> PricesSequenceToInputVector(List <float> pricesSequence)
        {
            List <float> inputVector = new List <float>();

            for (int key_from = pricesSequence.Count - 1; key_from >= period; key_from--)
            {
                for (int key_to = key_from - 1; key_to >= key_from - period; key_to--)
                {
                    inputVector.Add(pricesSequence[key_from] - pricesSequence[key_to]);
                }
            }

            inputVector = MatrixTools.NormalizeV(inputVector);

            //do only after normalization
            if (inputVector.Count > 0)
            {
                inputVector.Add(pricesSequence[pricesSequence.Count - 1]);//add current price to last position of input vector
            }
            //Console.WriteLine("pricesSequence[pricesSequence.Count - 1] = {0}", pricesSequence[pricesSequence.Count - 1]);
            return(inputVector);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        private void MakeMatricesAndVectors()
        {
            if (values != null)
                return;

            int[] queue = new int[DNA.Length];       //порядок обработки нейронов, номер нода -> номер очереди
            byte[] map_outputs = new byte[DNA.Length];      //здесь отмечаем нейроны которые соединяются с последним слоем
            List<int>[] links_from = new List<int>[DNA.Length]; //здесь ДНК наоборот - номер нейрона -> список с номерами "входящих" связей

            int node_link_to, current_node_level, max_level = 0;
            bool is_output;
            for (int current_node = 0; current_node < DNA.Length; current_node++)
            {
                current_node_level = queue[current_node];

                is_output = true;

                for (int i = 0; i < DNA[current_node].Length; i++)
                {
                    node_link_to = current_node + DNA[current_node][i];

                    if (node_link_to >= DNA.Length)
                        continue;

                    if (links_from[node_link_to] == null)
                        links_from[node_link_to] = new List<int>();

                    links_from[node_link_to].Add(current_node);

                    queue[node_link_to] = Math.Max(queue[node_link_to], current_node_level + 1);

                    max_level = Math.Max(queue[node_link_to], max_level);

                    is_output = false;
                }

                map_outputs[current_node] = (byte)(is_output ? 1 : 0);
            }

            layers = new List<int>[max_level + 1];

            for (int i = 0; i < queue.Length; i++)
            {
                if (layers[queue[i]] == null)
                    layers[queue[i]] = new List<int>();

                layers[queue[i]].Add(i);
            }

            //строим векторы и матрицы
            List<int> vecOutKeys = new List<int>();

            matrices = new float[layers.Length + 1][,];
            incoming_vectors_keys = new int[layers.Length + 1][];

            matrices[0] = MatrixTools.FillRandomMatrix(layers[0].Count, input_layer_size);//матрица между Vi и Vh0

            for (int layer = 1; layer < layers.Length; layer++)
            {
                incoming_vectors_keys[layer] = GetLayerIncomeKeys(layers[layer], links_from);

                matrices[layer] = MatrixTools.FillRandomMatrix(layers[layer], incoming_vectors_keys[layer], links_from);
            }

            incoming_vectors_keys[incoming_vectors_keys.Length - 1] = GetOutputKeys(map_outputs);
            matrices[matrices.Length - 1] = MatrixTools.FillRandomMatrix(output_layer_size, incoming_vectors_keys[incoming_vectors_keys.Length - 1].Length);

            input = new float[input_layer_size];
            values = new float[DNA.Length];
            output = new float[output_layer_size];
        }