/// <summary>
        /// Initializes the member network and the member teacher.
        /// </summary>
        /// <param name="network"></param>
        /// <param name="teacher"></param>
        private void SetUpNetwork(ActivationNetwork network, NeuralNetworkInfo info)
        {
            string teacher = info.Teacher;

            m_Network = network;

            if (teacher == "BackPropagation")
            {
                BackPropagationLearning learner = new BackPropagationLearning(m_Network);
                learner.LearningRate = info.LearningRate;
                learner.Momentum     = info.Momentum;
                m_Teacher            = learner;
            }
            else if (teacher == "Perceptron")
            {
                PerceptronLearning learner = new PerceptronLearning(m_Network);
                learner.LearningRate = info.LearningRate;
                m_Teacher            = learner;
            }
            else if (teacher == "DeltaRule")
            {
                DeltaRuleLearning learner = new DeltaRuleLearning(m_Network);
                learner.LearningRate = info.LearningRate;
                m_Teacher            = learner;
            }
            else
            {
                BackPropagationLearning learner = new BackPropagationLearning(m_Network);
                learner.LearningRate = info.LearningRate;
                learner.Momentum     = info.Momentum;
                m_Teacher            = learner;
            }
        }
        /// <summary>
        /// Constructor for results
        /// </summary>
        /// <param name="results"></param>
        /// <param name="expected"></param>
        /// <param name="info"></param>
        public NeuralNetworkResults(double[][] results, double[][] expected, NeuralNetworkInfo info)
        {
            m_NetworkInfo = info;
            m_BitString   = "";

            int numClasses = expected[0].Length;

            if (numClasses == 1)
            {
                numClasses = 2;
            }

            m_ConfusionMatrix = new int[numClasses][];
            for (int i = 0; i < numClasses; i++)
            {
                m_ConfusionMatrix[i] = new int[numClasses];
            }

            for (int i = 0; i < results.GetLength(0); i++)
            {
                int maxResult   = DetermineMax(results[i]);
                int maxExpected = DetermineMax(expected[i]);

                m_ConfusionMatrix[maxExpected][maxResult]++;
            }
        }
 /// <summary>
 /// Deserialization Constructor
 /// </summary>
 /// <param name="info"></param>
 /// <param name="ctxt"></param>
 public NeuralNetworkResults(SerializationInfo info, StreamingContext ctxt)
 {
     //Get the values from info and assign them to the appropriate properties
     m_ConfusionMatrix = (int[][])info.GetValue("Matrix", typeof(int[][]));
     m_NetworkInfo     = (NeuralNetworkInfo)info.GetValue("Info", typeof(NeuralNetworkInfo));
     m_BitString       = (string)info.GetValue("BitString", typeof(string));
 }
 /// <summary>
 /// Deserialization Constructor
 /// </summary>
 /// <param name="info"></param>
 /// <param name="ctxt"></param>
 public NeuralNetwork(SerializationInfo info, StreamingContext ctxt)
 {
     //Get the values from info and assign them to the appropriate properties
     m_Info    = (NeuralNetworkInfo)info.GetValue("NeuralNetworkInfo", typeof(NeuralNetworkInfo));
     m_Network = (ActivationNetwork)info.GetValue("Network", typeof(ActivationNetwork));
     m_Teacher = (ISupervisedLearning)info.GetValue("Teacher", typeof(ISupervisedLearning));
     m_Trained = (bool)info.GetValue("Trained", typeof(bool));
 }
Example #5
0
 public NeuralNetworkInfo(NeuralNetworkInfo info)
 {
     m_ActivationFunction         = info.m_ActivationFunction;
     m_DomainName                 = info.m_DomainName;
     m_FeatureNames               = info.m_FeatureNames;
     m_Layers                     = info.m_Layers;
     m_LearningMethod             = info.m_LearningMethod;
     m_LearningRate               = info.m_LearningRate;
     m_Momentum                   = info.m_Momentum;
     m_NetworkId                  = Guid.NewGuid();
     m_NetworkTrainingErrorValues = new Dictionary <int, double>();
     m_NumInputs                  = info.m_NumInputs;
     m_NumTrainingEpochs          = info.m_NumTrainingEpochs;
     m_OutputTypeNames            = info.m_OutputTypeNames;
     m_SketchId                   = info.m_SketchId;
     m_SketchName                 = info.m_SketchName;
     m_Teacher                    = info.m_Teacher;
     m_Type     = info.m_Type;
     m_UserId   = info.m_UserId;
     m_UserName = info.m_UserName;
 }
 /// <summary>
 /// Uses a preconstructed ActivationNetwork to initialize.
 /// </summary>
 /// <param name="network">Existing network</param>
 /// <param name="info">Network Information</param>
 public NeuralNetwork(ActivationNetwork network, NeuralNetworkInfo info)
 {
     m_Info = info;
     SetUpNetwork(network, info);
 }
        /// <summary>
        /// Constructor which takes in parameters necessary to
        /// create a new network.
        /// </summary>
        /// <param name="layers">Each int is a layer, value of int is #neurons in layer</param>
        /// <param name="numInputs">Number of inputs to the network</param>
        /// <param name="activationFunction">Name of the activation function to use</param>
        /// <param name="info">Network information</param>
        public NeuralNetwork(NeuralNetworkInfo info)
        {
            m_Info = info;

            SetUpNetwork(new ActivationNetwork(info.ActivationFunction, info.NumInputs, info.Layers), info);
        }
        public static NeuralNetwork CreateNetworkFromWekaOutput(string filename)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(filename);

            List <string> attributes = new List <string>();
            Dictionary <string, Dictionary <string, object> > weights =
                new Dictionary <string, Dictionary <string, object> >();

            string line;
            bool   atAttributes      = false;
            bool   atClassifierModel = false;
            bool   atRunInformation  = false;
            string currentNode       = "";
            string currentClass      = "";

            #region Read file

            while ((line = reader.ReadLine()) != null)
            {
                #region set markers for where we are in file
                if (line.Contains("=== Run information"))
                {
                    atRunInformation  = true;
                    atClassifierModel = false;
                }
                else if (line.Contains("=== Classifier model"))
                {
                    atRunInformation  = false;
                    atClassifierModel = true;
                }
                else if (line.Contains("=== Predictions on test data"))
                {
                    atRunInformation  = false;
                    atClassifierModel = false;
                }
                else if (line.Contains("=== Confusion Matrix"))
                {
                    atRunInformation  = false;
                    atClassifierModel = false;
                }

                if (atRunInformation && line.Contains("Attributes:"))
                {
                    atAttributes = true;
                    continue;
                }

                #endregion

                if (atAttributes)
                {
                    string name = line.Trim();
                    if (name == "class")
                    {
                        atAttributes = false;
                    }
                    else
                    {
                        attributes.Add(name);
                    }

                    continue;
                }

                if (atClassifierModel && line.StartsWith("Sigmoid Node"))
                {
                    currentNode = line.Substring(line.IndexOf("Node"));
                    weights.Add(currentNode, new Dictionary <string, object>());
                    continue;
                }
                else if (atClassifierModel && line.StartsWith("Class"))
                {
                    currentClass = line.Substring(line.IndexOf("Class"));
                    if (currentNode != "Class")
                    {
                        currentNode = "Class";
                        weights.Add(currentNode, new Dictionary <string, object>());
                    }
                    continue;
                }

                if (atClassifierModel && currentNode != "" && currentNode != "Class")
                {
                    if (line.Contains("Threshold"))
                    {
                        string value = line.Substring(line.IndexOf("Threshold") + 13);
                        double num;
                        bool   success = double.TryParse(value, out num);
                        if (success)
                        {
                            weights[currentNode].Add("Threshold", (object)num);
                        }
                    }
                    else if (line.Contains("Inputs"))
                    {
                    }
                    else if (line.Contains("Attrib"))
                    {
                        int    index  = line.LastIndexOf(" ");
                        int    index2 = line.IndexOf("Attrib") + 7;
                        string name   = line.Substring(index2, index - index2);
                        name = name.Trim();
                        string value = line.Substring(index);
                        double num;
                        bool   success = double.TryParse(value, out num);
                        if (success)
                        {
                            weights[currentNode].Add(name, (object)num);
                        }
                    }
                    else if (line.Contains("Node"))
                    {
                        int    index  = line.LastIndexOf(" ");
                        int    index2 = line.IndexOf("Node");
                        string name   = line.Substring(index2, index - index2);
                        name = name.Trim();
                        string value = line.Substring(index);
                        double num;
                        bool   success = double.TryParse(value, out num);
                        if (success)
                        {
                            weights[currentNode].Add(name, (object)num);
                        }
                    }
                    continue;
                }
                else if (atClassifierModel && currentNode == "Class")
                {
                    if (line.Contains("Node"))
                    {
                        string lShort = line.Trim();
                        weights[currentNode].Add(currentClass, (object)lShort);
                    }
                    continue;
                }
            }

            reader.Close();

            #endregion

            #region Generate Network

            NeuralNetworkInfo info = new NeuralNetworkInfo();
            info.FeatureNames = new List <string>(attributes);

            Dictionary <int, List <string> > layerStructure = new Dictionary <int, List <string> >();
            int currentLayer = 0;
            Dictionary <int, List <string> > children = new Dictionary <int, List <string> >();

            foreach (KeyValuePair <string, Dictionary <string, object> > kvp in weights)
            {
                string nodeName = kvp.Key;
                if (nodeName == "Class")
                {
                    continue;
                }

                // Find the current layer for this nodeName
                int childLayer = -1;
                foreach (string node in kvp.Value.Keys)
                {
                    bool found = false;
                    foreach (KeyValuePair <int, List <string> > kvp2 in children)
                    {
                        if (kvp2.Value.Contains(node))
                        {
                            childLayer = kvp2.Key;
                            found      = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }
                if (childLayer != -1)
                {
                    currentLayer = childLayer;
                }
                else
                {
                    bool found = false;
                    foreach (KeyValuePair <int, List <string> > kvp2 in children)
                    {
                        if (kvp2.Value.Contains(nodeName))
                        {
                            currentLayer = kvp2.Key + 1;
                            found        = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        int highest = -1;
                        foreach (int num in layerStructure.Keys)
                        {
                            highest = Math.Max(highest, num);
                        }

                        currentLayer = highest + 1;
                    }
                }

                if (!layerStructure.ContainsKey(currentLayer))
                {
                    layerStructure.Add(currentLayer, new List <string>());
                }

                if (!layerStructure[currentLayer].Contains(nodeName))
                {
                    layerStructure[currentLayer].Add(nodeName);
                }

                foreach (string node in kvp.Value.Keys)
                {
                    if (node.Contains("Threshold"))
                    {
                        continue;
                    }
                    else if (!node.Contains("Node"))
                    {
                        if (!children.ContainsKey(currentLayer))
                        {
                            children.Add(currentLayer, new List <string>());
                        }

                        if (!children[currentLayer].Contains(node))
                        {
                            children[currentLayer].Add(node);
                        }

                        continue;
                    }

                    if (!layerStructure.ContainsKey(currentLayer + 1))
                    {
                        layerStructure.Add(currentLayer + 1, new List <string>());
                    }

                    if (!layerStructure[currentLayer + 1].Contains(node))
                    {
                        layerStructure[currentLayer + 1].Add(node);
                    }

                    if (!children.ContainsKey(currentLayer))
                    {
                        children.Add(currentLayer, new List <string>());
                    }

                    if (!children[currentLayer].Contains(node))
                    {
                        children[currentLayer].Add(node);
                    }
                }
            }

            List <int> layers = new List <int>();
            foreach (KeyValuePair <int, List <string> > kvp in layerStructure)
            {
                layers.Add(kvp.Value.Count);
            }
            layers.Reverse();

            info.Layers    = layers.ToArray();
            info.NumInputs = attributes.Count;

            NeuralNetwork network = new NeuralNetwork(info);

            for (int i = 0; i < layers.Count; i++)
            {
                int index = layers.Count - 1 - i;
                for (int j = 0; j < layers[index]; j++)
                {
                    int k = 0;
                    foreach (KeyValuePair <string, object> kvp in weights[layerStructure[i][j]])
                    {
                        if (kvp.Key == "Threshold")
                        {
                            network.m_Network[index][j].Threshold = (double)kvp.Value;
                        }
                        else
                        {
                            network.m_Network[index][j][k] = (double)kvp.Value;
                            k++;
                        }
                    }
                }
            }

            network.m_Trained = true;

            #endregion

            return(network);
        }