Exemplo n.º 1
0
        public Layer(LayerCreationInfo layerCreationInfo, bool IsNetworkUsingBias, LayerType type, double minWeight, double maxWeight)
        {
            LayerNo = layerCreationInfo.LayerNo;
            PreviousLayerNeuronsCount = layerCreationInfo.PreviousLayerNeuronsCount;
            LayerActivationFunction   = layerCreationInfo.LayerActivationFunction;
            this.Type      = type;
            this.MinWeight = minWeight;
            this.MaxWeight = maxWeight;

            Neurons = new List <Neuron>();

            for (int i = 0; i < layerCreationInfo.HowManyNeuronsPerLayer; i++)
            {
                Neurons.Add(new Neuron(
                                LayerNo == 0 ?  1 : PreviousLayerNeuronsCount,
                                layerCreationInfo.LayerNo,
                                false,
                                layerCreationInfo.LayerActivationFunction,
                                MinWeight,
                                MaxWeight));
            }

            if (IsNetworkUsingBias && type != LayerType.OUTPUT)
            {
                Neurons.Last().IsBias = true;
            }
        }
Exemplo n.º 2
0
        public static NeuralNetwork LoadNetworkFromFile(String filename)
        {
            NeuralNetwork neuralNetwork = null;

            try
            {
                StreamReader sr  = new StreamReader(filename);
                String       str = sr.ReadLine(); // headers
                String[]     strTab;
                str    = sr.ReadLine();
                strTab = str.Split(';');
                bool isNetworkUsingBias = bool.Parse(strTab[1]);

                str    = sr.ReadLine();
                strTab = str.Split(';');
                double minWeight = double.Parse(strTab[1]);

                str    = sr.ReadLine();
                strTab = str.Split(';');
                double maxWeight = double.Parse(strTab[1]);

                str    = sr.ReadLine();
                strTab = str.Split(';');
                LearningMethod methodOfLearning = strTab[1] == "0" ? LearningMethod.LINEAR : LearningMethod.NOT_LINEAR;

                str    = sr.ReadLine();
                strTab = str.Split(';');
                String name = strTab[1];

                String[] layersNeuronsStr                   = sr.ReadLine().Split(';'); // neurons in layers
                String[] layersActivationFunctionStr        = sr.ReadLine().Split(';'); // neurons in layers
                List <LayerCreationInfo> layerCreationInfos = new List <LayerCreationInfo>();

                for (int i = 1; i < layersNeuronsStr.Length; i++)
                {
                    int layerIdx          = i - 1;
                    LayerCreationInfo lci = new LayerCreationInfo();
                    lci.HowManyNeuronsPerLayer = int.Parse(layersNeuronsStr[i]);
                    lci.LayerNo = layerIdx;
                    lci.PreviousLayerNeuronsCount = layerIdx == 0 ? 0 : layerCreationInfos[layerIdx - 1].HowManyNeuronsPerLayer;

                    int LayerActivationFunctionInt = int.Parse(layersActivationFunctionStr[i]);
                    lci.LayerActivationFunction = GetActivationFunctionById(LayerActivationFunctionInt);

                    layerCreationInfos.Add(lci);
                }

                Topology topology = new Topology(layerCreationInfos, isNetworkUsingBias, minWeight, maxWeight);

                neuralNetwork = new NeuralNetwork(topology, minWeight, maxWeight, methodOfLearning, name);

                // provide saved neurons weights:
                for (int layerNo = 0; layerNo < neuralNetwork.Topology.Layers.Count; layerNo++)
                {
                    Layer layer = neuralNetwork.Topology.Layers[layerNo];

                    for (int neuronNo = 0; neuronNo < layer.Neurons.Count; neuronNo++)
                    {
                        String[] inputsString = sr.ReadLine().Split(';');


                        for (int inputNo = 0; inputNo < layer.Neurons[neuronNo].Inputs.Count; inputNo++)
                        {
                            layer.Neurons[neuronNo].Inputs[inputNo].Weight = double.Parse(inputsString[inputNo + 1]);
                        }
                    }
                }

                neuralNetwork.PropagateValuesForward(); // maybe not needed now, but for order it's good to get network in proper state.
                return(neuralNetwork);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while loading network: " + ex.Message);

                return(null);
            }
        }