Ejemplo n.º 1
0
        public void ErrorTranclatedCorrect()
        {
            var uselessLayer = new NeuronsLayer(2, 1);

            double[,] TEST_WEIGHTS = { { 1, 0.5 } };
            double[] TEST_ERROR    = { 1, -0.5 };
            double[] CORRECT_ERROR = { 0.75 };

            uselessLayer.Weights = TEST_WEIGHTS;
            uselessLayer.Error   = TEST_ERROR;

            uselessLayer.TranslateError(_layer);

            CollectionAssert.AreEqual(CORRECT_ERROR, _layer.Error);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads information about a network from the network file.
        /// </summary>
        /// <param name="file">The .unf file.</param>
        /// <returns>A input size - layers pair.</returns>
        public (int, List <INeuronsLayer>) ReadNetworkFile(string file)
        {
            _file = file;

            var localStorage = new Dictionary <string, string>();

            string[] info;
            using (var reader = new StreamReader(_file))
            {
                info = reader.ReadLine().Split(',', StringSplitOptions.RemoveEmptyEntries);
                ConvertStringsToPairs(info);

                _inputSize = int.Parse(localStorage["is"]);
                _layers    = new List <INeuronsLayer>();

                var layersCount = int.Parse(localStorage["lc"]);
                for (int i = 0; i < layersCount; i++)
                {
                    localStorage.Clear();
                    reader.ReadLine();

                    info = reader.ReadLine().Split(',', StringSplitOptions.RemoveEmptyEntries);
                    ConvertStringsToPairs(info);

                    var inputSize          = int.Parse(localStorage["is"]);
                    var size               = int.Parse(localStorage["ls"]);
                    var activationFunction = GetFunctionType(localStorage["af"]);

                    var newLayer = new NeuronsLayer(size, inputSize);
                    newLayer.ActivationFunction = activationFunction;

                    for (int j = 0; j < size; j++)
                    {
                        var weights = reader.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);

                        newLayer.Bias[j] = double.Parse(weights[0]);
                        for (int k = 0; k < inputSize; k++)
                        {
                            newLayer.Weights[k, j] = double.Parse(weights[k + 1]);
                        }
                    }

                    _layers.Add(newLayer);
                }
            }

            return(_inputSize, _layers);

            void ConvertStringsToPairs(string[] strings)
            {
                foreach (var param in strings)
                {
                    var pair = param.Split('=', StringSplitOptions.RemoveEmptyEntries);
                    localStorage.Add(pair[0], pair[1]);
                }
            }

            ActivationFunctionType GetFunctionType(string name)
            {
                return(name switch
                {
                    "LI" => ActivationFunctionType.Linear,
                    "S" => ActivationFunctionType.Sigmoid,
                    "T" => ActivationFunctionType.TanH,
                    "R" => ActivationFunctionType.ReLU,
                    "LR" => ActivationFunctionType.LReLU
                });
            }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads a information about network from config and weights files.
        /// </summary>
        /// <param name="file">A .ncfg file.</param>
        /// <returns>A input size - layers pair.</returns>
        public (int, List <INeuronsLayer>) ReadNetworkFile(string file)
        {
            (int, List <INeuronsLayer>)result;

            var localStorage = new Dictionary <string, string>();

            using (var reader = new StreamReader(file))
            {
                for (int i = 0; i < 3; i++)
                {
                    var current = reader.ReadLine()
                                  .Replace(" ", "")
                                  .Split('=', StringSplitOptions.RemoveEmptyEntries);

                    localStorage.Add(current[0], current[1]);
                }

                result.Item1 = int.Parse(localStorage["input_size"]);
                result.Item2 = new List <INeuronsLayer>();

                var layersCount = int.Parse(localStorage["layers_count"]);

                var basePath = Path.Join(file, @"../", localStorage["path_to_weights"]);
                for (int i = 0; i < layersCount; i++)
                {
                    localStorage.Clear();

                    reader.ReadLine();
                    for (int j = 0; j < 4; j++)
                    {
                        var current = reader.ReadLine()
                                      .Replace(" ", "")
                                      .Split('=', StringSplitOptions.RemoveEmptyEntries);

                        localStorage.Add(current[0], current[1]);
                    }
                    var layerSize         = int.Parse(localStorage["layer_size"]);
                    var inputSize         = int.Parse(localStorage["input_size"]);
                    var functionType      = GetFunctionType(localStorage["activation_function"]);
                    var weightsFileStream = new FileStream(Path.Combine(basePath, localStorage["weights_file"]), FileMode.Open);

                    var newLayer = new NeuronsLayer(layerSize, inputSize);
                    newLayer.ActivationFunction = functionType;
                    ReadLayerWeights(newLayer, weightsFileStream);
                    result.Item2.Add(newLayer);

                    _filesTable.Add(newLayer, weightsFileStream);
                }
            }

            return(result);

            ActivationFunctionType GetFunctionType(string @string)
            {
                return(@string switch
                {
                    "Linear" => ActivationFunctionType.Linear,
                    "Sigmoid" => ActivationFunctionType.Sigmoid,
                    "LReLU" => ActivationFunctionType.LReLU,
                    "ReLU" => ActivationFunctionType.ReLU,
                    "TanH" => ActivationFunctionType.TanH,
                    _ => throw new ArgumentException()
                });
            }