Exemplo n.º 1
0
    /// <summary>
    /// Connects two neurons.
    /// </summary>
    /// <param name="neuron1">First neuron, cannot be OutputNeuron.</param>
    /// <param name="neuron2">Second neuron, cannot be InputNeuron.</param>
    /// <returns>InputLink of seocnd neuron.</returns>
    private Neuron.InputLink Connect(Neuron neuron1, Neuron neuron2)
    {
        if (neuron1.Type == NeuronType.OutputNeuron)
        {
            throw new NeuralNetworkException("Output neuron cannot have output links");
        }
        if (neuron2.Type == NeuronType.InputNeuron)
        {
            throw new NeuralNetworkException("Input neuron cannot have input links");
        }

        Neuron.InputLink newInputLink = new Neuron.InputLink(neuron1, 0.0);
        neuron2.InputLinks.Add(newInputLink);
        neuron1.OutputLinks.Add(neuron2);

        // since network structure changed, neurons have to be sorted
        this.SortNeurons();

        return(newInputLink);
    }
Exemplo n.º 2
0
    /// <summary>
    /// Loads neural network from txt file.
    /// </summary>
    /// <param name="filename">Name of the file.</param>
    /// <returns>Neural network loaded from the file.</returns>
#pragma warning disable SA1204 // Static elements should appear before instance elements
    public static NeuralNetwork LoadFromFile(string filename)
#pragma warning restore SA1204 // Static elements should appear before instance elements
    {
        // object which will be returned
        NeuralNetwork loadedNetwork = new NeuralNetwork();

        using (StreamReader reader = new StreamReader(filename))
        {
            // loading network properties
            loadedNetwork.Id = int.Parse(reader.ReadLine().Split(' ')[1]);

            // loading extra properties
            while (true)
            {
                string nextLine = reader.ReadLine();

                // list of properties ends with empty line
                if (nextLine == null || nextLine == string.Empty)
                {
                    break;
                }

                loadedNetwork.ExtraProperties.Add(nextLine.Split(' ')[0], nextLine.Split(' ')[1]);
            }

            // first pass, loading list of neurons and creating them
            while (true)
            {
                string nextLine = reader.ReadLine();

                // checking if list has ended
                if (nextLine == null || nextLine == string.Empty)
                {
                    break;
                }

                // skipping lines with weights
                if (nextLine.StartsWith("    "))
                {
                    continue;
                }

                // creating new neuron
                Neuron   newNeuron    = null;
                string[] neuronHeader = nextLine.Split(' ');
                switch (neuronHeader[0])
                {
                case "Input": newNeuron = loadedNetwork.AddInputNeuron(string.Empty); break;

                case "Hidden": newNeuron = loadedNetwork.AddHiddenNeuron(string.Empty); break;

                case "Output": newNeuron = loadedNetwork.AddOutputNeuron(string.Empty); break;
                }
                newNeuron.Id   = int.Parse(neuronHeader[1]);
                newNeuron.Name = neuronHeader[2];
            }

            // reset position to the beginning of the file
            reader.DiscardBufferedData();
            reader.BaseStream.Seek(0, SeekOrigin.Begin);

            // skipping network parameters and empty line in the beginning
            while (true)
            {
                string nextLine = reader.ReadLine();
                if (nextLine == string.Empty)
                {
                    break;
                }
            }

            // second pass, loading neurons
            while (true)
            {
                // cheking if list has ended
                string nextLine = reader.ReadLine();
                if (nextLine == null || nextLine == string.Empty)
                {
                    break;
                }

                // finding neuron
                string[] neuronHeader = nextLine.Split(' ');
                int      neuronId     = int.Parse(neuronHeader[1]);
                Neuron   neuron       = loadedNetwork.GetNeuronById(neuronId);

                // loading weights and bias
                while (true)
                {
                    string[] weightOrBiasLine = reader.ReadLine().Trim().Split(' ');
                    string   label            = weightOrBiasLine[0];
                    double   value            = double.Parse(weightOrBiasLine[1]);
                    if (label == "w")
                    {
                        int              connection    = int.Parse(weightOrBiasLine[2]);
                        Neuron           anotherNeuron = loadedNetwork.GetNeuronById(connection);
                        Neuron.InputLink newInputLink  = loadedNetwork.Connect(anotherNeuron, neuron);
                        newInputLink.Weight = value;
                    }
                    else if (label == "b")
                    {
                        neuron.Bias = value;
                        break; // line with bias is the last line of the neuron
                    }
                }
            }
        }
        return(loadedNetwork);
    }