예제 #1
0
파일: Topology.cs 프로젝트: pulsence/ENN
 /// <summary>
 /// Save a NetworkTopology to file. 
 /// </summary>
 /// <param name="file">Location to save the file.</param>
 /// <param name="topology">The topology to save to file.</param>
 /// <exception cref="IOException">System.IO.IOException</exception>
 public static void Save(string file, NetworkTopology topology, bool binary = false)
 {
     if(binary)
         SaveBinary(file, ref topology);
     else
         SaveText(file, ref topology);
 }
예제 #2
0
 public void LoadTextTest()
 {
     SetUpBaseTopology();
     testTopology = Topology.Load(
         basePath + "testTopology.nnt",
         ref objectFactory,
         ref settings);
     Assert.IsTrue(baseTopology.Equals(testTopology), "Test for logical equality");
     Assert.IsNotNull(testTopology.MetaData, "Test for meta data");
 }
예제 #3
0
        /// <summary>
        /// Calculates a value. Same algorithm as in the neural network clas.
        /// </summary>
        private float ComputeDecision(ref NetworkTopology topology)
        {
            float val;

            float[] inputPool = topology.PreProcessor.GenerateValues();
            topology.InputLayer.SetInputPool(ref inputPool);
            float[] layerVals = topology.InputLayer.GetValues();
            for (int i = 0; i < topology.HiddenLayers.Length; i++)
            {
                layerVals = topology.HiddenLayers[i].GetValues(layerVals);
            }
            val = topology.OutputLayer.GetValue(layerVals);
            topology.PostProcessor.FinalAction(val);
            return(val);
        }
예제 #4
0
        /// <summary>
        /// Traines the given network. Same algorithm as in the neural network class.
        /// </summary>
        private float TrainNetwork(ref NetworkTopology topology)
        {
            topology.TrainingAlgorithm.SetSettings(settings);
            int countSinceTrain    = 0;
            int maxCountSinceTrain = 40;

            if (settings.TraininPool != null &&
                settings.TraininPool > 0)
            {
                maxCountSinceTrain = settings.TraininPool;
            }
            int trainingIterations = 0;

            if (settings.TrainingIterations == -1)
            {
                trainingIterations = -2;
            }
            float value;
            float expected;
            float error;
            float average = 0;//average error

            while ((1 - average) < settings.TrainingAccuracy &&
                   trainingIterations < settings.TrainingIterations)
            {
                average = 0;
                while (countSinceTrain < maxCountSinceTrain)
                {
                    value    = ComputeDecision(ref topology);
                    expected = topology.TrainingPreProcessor.ExpectedResult();
                    error    = (expected - value) / expected;
                    error    = Math.Abs(error);
                    average  = average + ((error - average) / (countSinceTrain + 1));
                    countSinceTrain++;
                }
                topology.TrainingAlgorithm.TrainNetwork(ref topology, average);
                if (settings.TrainingIterations != -1)
                {
                    trainingIterations++;
                }
            }
            return(1 - average);
        }
예제 #5
0
        public void SaveLoadBinaryTest()
        {
            SetUpBaseTopology();

            Topology.Save(
                basePath + "testBinaryTopologySave.nntc",
                baseTopology,
                true);

            testTopology = Topology.Load(
                basePath + "testBinaryTopologySave.nntc",
                ref objectFactory,
                ref settings,
                true);

            Assert.IsTrue(baseTopology.Equals(testTopology));

            File.Delete(basePath + "testBinaryTopologySave.nntc");
        }
예제 #6
0
 /// <summary>
 /// Trains the network.
 /// </summary>
 /// <param name="topology">Reference to the topology object to train.</param>
 /// <param name="error">The current error in decimal format of the topology.
 /// Example of expected: 0.6</param>
 public void TrainNetwork(ref NetworkTopology topology, float error)
 {
     if (error > priorError)
     {
         stepValue *= -0.5f;
         flips++;
     }
     if (flips == maxFlips)
     {
         nodeIndex++;
         weightIndex = 0;
         stepValue   = baseStepValue;
     }
     if (weightIndex > topology.HiddenLayers[layerIndex].Nodes[nodeIndex].Weights.Length)
     {
         nodeIndex++;
         weightIndex = 0;
         stepValue   = baseStepValue;
     }
     if (nodeIndex > topology.HiddenLayers[layerIndex].Nodes.Length)
     {
         nodeIndex   = 0;
         weightIndex = 0;
         layerIndex++;
         stepValue = baseStepValue;
     }
     if (layerIndex > topology.HiddenLayers.Length)
     {
         nodeIndex   = 0;
         layerIndex  = 0;
         weightIndex = 0;
         stepValue   = baseStepValue;
     }
     priorError = error;
     topology.HiddenLayers[layerIndex].Nodes[nodeIndex].Weights[weightIndex] += stepValue;
 }
예제 #7
0
파일: MainForm.cs 프로젝트: pulsence/ENN
        /// <summary>
        /// Saves the current topology in the GUI.
        /// </summary>
        private void SaveTopology(object sender, EventArgs e)
        {
            NetworkTopology topology = new NetworkTopology();
            topology.MetaData = topologyMetaData;

            List<IHiddenLayer> hiddenLayers = new List<IHiddenLayer>();
            foreach (UserControl layer in topologyDisplay.Controls)
            {
                Type layerType = layer.GetType();
                Dictionary<string, string> metaData = ((LayerView)layer).GetMetaData();
                if (layerType == typeof(InputLayerView))
                {
                    InputLayerShell inputLayer = new InputLayerShell();
                    inputLayer.MetaData = metaData;
                    topology.InputLayer = inputLayer;
                }
                else if (layerType == typeof(HiddenLayerView))
                {
                    HiddenLayerView view = (HiddenLayerView)layer;
                    LayerView[] rawNodes = view.GetNodes();
                    INode[] nodes = new INode[rawNodes.Length];
                    for (int i = 0; i < rawNodes.Length; i++)
                    {
                        NodeShell node = new NodeShell();
                        node.MetaData = rawNodes[i].GetMetaData();
                        nodes[i] = node;
                    }
                    HiddenLayerShell hiddenLayer = new HiddenLayerShell();
                    hiddenLayer.MetaData = metaData;
                    hiddenLayer.Nodes = nodes;
                    hiddenLayers.Add(hiddenLayer);
                }
                else if (layerType == typeof(OutputLayerView))
                {
                    OutputLayerShell outputLayer = new OutputLayerShell();
                    outputLayer.MetaData = metaData;
                    topology.OutputLayer = outputLayer;
                }
                else if (layerType == typeof(PreProcessorView))
                {
                    PreProcessorShell preProcessor = new PreProcessorShell();
                    preProcessor.MetaData = metaData;
                    topology.PreProcessor = preProcessor;
                }
                else if (layerType == typeof(PostProcessorView))
                {
                    PostProcessorShell postProcessor = new PostProcessorShell();
                    postProcessor.MetaData = metaData;
                    topology.PostProcessor = postProcessor;
                }
                topology.HiddenLayers = hiddenLayers.ToArray();
                saveTopology.ShowDialog();

                if (saveTopology.FileName.EndsWith(".nntc"))
                {
                    saveTopology.FileName.Remove(saveTopology.FileName.Length - 1);
                }
                Topology.Save(saveTopology.FileName, topology);
            }
        }
예제 #8
0
파일: Topology.cs 프로젝트: pulsence/ENN
        /// <summary>
        /// Saves a topology to a file in the form of text. Not that the topology must
        /// have its meta data properly set or you will not be able to load the topology
        /// from the text file. The only way that you can garuntee that the file will
        /// save successfully is to have had the topology loaded from a text file at some
        /// point in time in the past.
        /// </summary>
        /// <param name="file">The location of the file</param>
        /// <param name="topology">The NetworkTopology object to persist</param>
        /// <exception cref="IOException">System.IO.IOException</exception>
        private static void SaveText(string file, ref NetworkTopology topology)
        {
            StreamWriter writer = new StreamWriter(file);

            writer.WriteLine("version:1.0");
            writer.WriteLine("#Created on {0}", DateTime.Now);
            writer.WriteLine();

            //Set up topology
            writer.WriteLine("#Topology");
            writer.WriteLine("topology{");
            WriteMeta(ref writer, topology);
            writer.WriteLine("}");
            writer.WriteLine();

            //Add layers
            writer.WriteLine("#Topology Layers");

            //Input
            writer.WriteLine("layer{");
            WriteMeta(ref writer, topology.InputLayer);
            writer.WriteLine("}");
            writer.WriteLine();

            //Output
            writer.WriteLine("layer{");
            WriteMeta(ref writer, topology.OutputLayer);
            writer.WriteLine("}");
            writer.WriteLine();

            //Hidden Layers
            foreach (IHiddenLayer hidden in topology.HiddenLayers)
            {
                writer.WriteLine("layer{");
                WriteMeta(ref writer, hidden);
                writer.WriteLine("}");
                writer.WriteLine();
            }

            //Add pre/post processors
            writer.WriteLine("#Pre and Post processors");

            //Pre
            writer.WriteLine("preprocessor{");
            WriteMeta(ref writer, topology.PreProcessor);
            writer.WriteLine("}");
            writer.WriteLine();

            //Post
            writer.WriteLine("postprocessor{");
            WriteMeta(ref writer, topology.PostProcessor);
            writer.WriteLine("}");
            writer.WriteLine();

            //Add nodes
            writer.WriteLine("#Nodes");
            foreach (IHiddenLayer layer in topology.HiddenLayers)
            {
                foreach (INode node in layer.Nodes)
                {
                    writer.WriteLine("node{");
                    WriteMeta(ref writer, node);
                    writer.WriteLine("}");
                    writer.WriteLine();
                }
            }
            writer.Close();
        }
예제 #9
0
파일: Topology.cs 프로젝트: pulsence/ENN
 /// <summary>
 /// Saves a topology to file in binary format.
 /// </summary>
 /// <param name="file">Location to save the topology.</param>
 /// <param name="topology">The topology object to persist to disk.</param>
 /// <exception cref="IOException">System.IO.IOException</exception>
 private static void SaveBinary(string file, ref NetworkTopology topology)
 {
     Stream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
     BinaryFormatter binary = new BinaryFormatter();
     binary.Serialize(fs, topology);
     fs.Close();
 }
예제 #10
0
 public NeuralNetwork(NetworkTopology topology, NetworkSettings settings)
 {
     this.topology = topology;
     this.settings = settings;
 }
예제 #11
0
        public void SetUp()
        {
            basePath = "C:\\Development Projects\\ENN\\TestSuite\\TestFiles\\";

            objectFactory = new Dictionary<string, IUserObjectFactory>()
            {
                {"standard", new StandLibFactory()},
                {"TestBinary", new TestBinary()}
            };

            settings = new NetworkSettings();
            settings.Mode = NetworkMode.Training;

            baseTopology = new NetworkTopology();
        }
예제 #12
0
 public EvolvingNeuralNetwork(NetworkTopology[] topologies, NetworkSettings settings)
 {
     this.topologies = topologies;
     this.settings = settings;
 }
예제 #13
0
 /// <summary>
 /// Traines the given network. Same algorithm as in the neural network class.
 /// </summary>
 private float TrainNetwork(ref NetworkTopology topology)
 {
     topology.TrainingAlgorithm.SetSettings(settings);
     int countSinceTrain = 0;
     int maxCountSinceTrain = 40;
     if (settings.TraininPool != null
         && settings.TraininPool > 0)
         maxCountSinceTrain = settings.TraininPool;
     int trainingIterations = 0;
     if (settings.TrainingIterations == -1) trainingIterations = -2;
     float value;
     float expected;
     float error;
     float average = 0;//average error
     while ((1 - average) < settings.TrainingAccuracy &&
            trainingIterations < settings.TrainingIterations)
     {
         average = 0;
         while (countSinceTrain < maxCountSinceTrain)
         {
             value = ComputeDecision(ref topology);
             expected = topology.TrainingPreProcessor.ExpectedResult();
             error = (expected - value) / expected;
             error = Math.Abs(error);
             average = average + ((error - average) / (countSinceTrain + 1));
             countSinceTrain++;
         }
         topology.TrainingAlgorithm.TrainNetwork(ref topology, average);
         if (settings.TrainingIterations != -1) trainingIterations++;
     }
     return 1 - average;
 }
예제 #14
0
 /// <summary>
 /// Calculates a value. Same algorithm as in the neural network clas.
 /// </summary>
 private float ComputeDecision(ref NetworkTopology topology)
 {
     float val;
     float[] inputPool = topology.PreProcessor.GenerateValues();
     topology.InputLayer.SetInputPool(ref inputPool);
     float[] layerVals = topology.InputLayer.GetValues();
     for (int i = 0; i < topology.HiddenLayers.Length; i++)
     {
         layerVals = topology.HiddenLayers[i].GetValues(layerVals);
     }
     val = topology.OutputLayer.GetValue(layerVals);
     topology.PostProcessor.FinalAction(val);
     return val;
 }
예제 #15
0
 /// <summary>
 /// Trains the network.
 /// </summary>
 /// <param name="topology">Reference to the topology object to train.</param>
 /// <param name="error">The current error in decimal format of the topology.
 /// Example of expected: 0.6</param>
 public void TrainNetwork(ref NetworkTopology topology, float error)
 {
     if (error > priorError)
     {
         stepValue *= -0.5f;
         flips++;
     }
     if (flips == maxFlips)
     {
         nodeIndex++;
         weightIndex = 0;
         stepValue = baseStepValue;
     }
     if (weightIndex > topology.HiddenLayers[layerIndex].Nodes[nodeIndex].Weights.Length)
     {
         nodeIndex++;
         weightIndex = 0;
         stepValue = baseStepValue;
     }
     if (nodeIndex > topology.HiddenLayers[layerIndex].Nodes.Length)
     {
         nodeIndex = 0;
         weightIndex = 0;
         layerIndex++;
         stepValue = baseStepValue;
     }
     if (layerIndex > topology.HiddenLayers.Length)
     {
         nodeIndex = 0;
         layerIndex = 0;
         weightIndex = 0;
         stepValue = baseStepValue;
     }
     priorError = error;
     topology.HiddenLayers[layerIndex].Nodes[nodeIndex].Weights[weightIndex] += stepValue;
 }
예제 #16
0
        /// <summary>
        /// Handles the running and testing of neural networks.
        /// </summary>
        /// <param name="commands"></param>
        private static void RunHandler(List<RawCommand> commands)
        {
            NetworkTopology topology = new NetworkTopology();

            string name = "";
            bool test = false;

            foreach(RawCommand command in commands)
            {
                if(command.CommandChar == 't')
                {
                    test = true;
                }
                else if(command.CommandChar == 'n')
                {
                    name = command.Value;
                }
            }

            if (name == "")
            {
                Console.WriteLine("Could not process request becuase no name was set");
                return;
            }

            if (topologies.ContainsKey(name))
            {
                topology = topologies[name];
            }
            else
            {
                Console.WriteLine("No topology by the name {0} is loaded", name);
                return;
            }

            if (test)
            {
                //tests to see is the specified topology is ready to be used.

                if (topology.HiddenLayers == null ||
                    topology.InputLayer == null ||
                    topology.OutputLayer == null ||
                    topology.PostProcessor == null ||
                    topology.PreProcessor == null)
                {
                    Console.WriteLine(
                        "The network is not ready to run because the" +
                        "topology is not completely loaded");
                    return;
                }

                bool error = false;
                int i = 0;
                int j = 0;
                INode[] nodes;
                while(i < topology.HiddenLayers.Length && error == false)
                {
                    nodes = topology.HiddenLayers[i].Nodes;
                    while (j < nodes.Length && error == false)
                    {
                        error = nodes[j] == null;
                        j++;
                    }
                    i++;
                }

                if (error)
                {
                    Console.WriteLine("The network is not ready to run");
                    Console.WriteLine("In layer number {0}, node {1} is null", --i, --j);
                    return;
                }

                Console.WriteLine("The network is ready to run");
            }
            else
            {
                //runs the specified topology
                Console.WriteLine("The network is starting...");
                NeuralNetwork network = new NeuralNetwork(topology, settings);
                ThreadPool.QueueUserWorkItem(network.StartNetwork);
                Console.WriteLine("The network was started");
            }
        }
예제 #17
0
        public override bool Equals(object obj)
        {
            NetworkTopology other = (NetworkTopology)obj;

            if (other == null)
            {
                return(false);
            }

            if (preProcessor == null)
            {
                if (other.PreProcessor != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!preProcessor.Equals(other.PreProcessor))
                {
                    return(false);
                }
            }

            if (postProcessor == null)
            {
                if (other.PostProcessor != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!postProcessor.Equals(other.postProcessor))
                {
                    return(false);
                }
            }

            if (TrainingAlgorithm == null)
            {
                if (other.TrainingAlgorithm != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!TrainingAlgorithm.Equals(other.TrainingAlgorithm))
                {
                    return(false);
                }
            }

            if (TrainingPreProcessor == null)
            {
                if (other.TrainingPreProcessor != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!TrainingPreProcessor.Equals(other.TrainingPreProcessor))
                {
                    return(false);
                }
            }

            if (inputLayer == null)
            {
                if (other.InputLayer != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!inputLayer.Equals(other.InputLayer))
                {
                    return(false);
                }
            }

            if (outputLayer == null)
            {
                if (other.OutputLayer != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!outputLayer.Equals(other.OutputLayer))
                {
                    return(false);
                }
            }

            if (hiddenLayers == null)
            {
                if (other.HiddenLayers != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!hiddenLayers.Length.Equals(other.HiddenLayers.Length))
                {
                    return(false);
                }
                for (int i = 0; i < hiddenLayers.Length; i++)
                {
                    if (!hiddenLayers[i].Equals(other.HiddenLayers[i]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
예제 #18
0
        public void SaveTextTest()
        {
            baseTopology = Topology.Load(
                basePath + "testTopology.nnt",
                ref objectFactory,
                ref settings);

            Topology.Save(
                basePath + "testSaveTopology.nnt",
                baseTopology);

            testTopology = Topology.Load(
                basePath + "testSaveTopology.nnt",
                ref objectFactory,
                ref settings);

            Assert.IsTrue(baseTopology.Equals(testTopology));

            File.Delete(basePath + "testSaveTopology.nnt");
        }
예제 #19
0
파일: Topology.cs 프로젝트: pulsence/ENN
        /// <summary>
        /// Creates a NetworkTopology from a text file.
        /// </summary>
        /// <param name="file">The location of the file to read from.</param>
        /// <param name="objectFactories">The object factories that will
        /// be used to tranform the text types into objects.</param>
        /// <param name="settings">The currently loaded NetworkSettings.</param>
        /// <returns>Returns the NetworkTopology located inside of the file.
        /// If the file can not be parsed null is returned.</returns>
        /// <exception cref="IOException">System.IO.IOException</exception>
        private static NetworkTopology LoadText(
            string file,
            ref Dictionary<string, IUserObjectFactory> objectFactories,
            ref NetworkSettings settings)
        {
            //Check to varify that the file can be loaded.
            if (GetVersion(file) != "1.0") return null;

            NetworkTopology topology = new NetworkTopology();
            //Retrieves the RawTypes from the file
            List<RawType> rawTypes = GetRawTypes(file);

            Dictionary<string, IHiddenLayer> hiddenLayers = new Dictionary<string, IHiddenLayer>();
            //Factory to create the object with
            string factoryName;

            //Transforms all the rawTypes into objects
            for (int i = 0; i < rawTypes.Count; i++)
            {
                factoryName = settings.DefaultFactory;
                if (rawTypes[i].Fields.ContainsKey("factory")) factoryName = rawTypes[i].Fields["factory"];
                if (rawTypes[i].Type == "layer")
                {

                    if (rawTypes[i].Fields["type"] == "hidden")
                    {
                        hiddenLayers.Add(rawTypes[i].Fields["layerName"],
                            objectFactories[factoryName].CreateUserObject<IHiddenLayer>(
                            rawTypes[i].Fields["dataType"],
                            rawTypes[i].Fields));
                    }
                    else if (rawTypes[i].Fields["type"] == "output")
                    {
                        topology.OutputLayer =
                            objectFactories[factoryName].CreateUserObject<IOutputLayer>(
                            rawTypes[i].Fields["dataType"], rawTypes[i].Fields);
                    }
                    else if (rawTypes[i].Fields["type"] == "input")
                    {
                        topology.InputLayer =
                            objectFactories[factoryName].CreateUserObject<IInputLayer>(
                            rawTypes[i].Fields["dataType"], rawTypes[i].Fields);
                    }
                }
                else if (rawTypes[i].Type == "node")
                {
                    INode node = objectFactories[factoryName].CreateUserObject<INode>(
                        rawTypes[i].Fields["dataType"], rawTypes[i].Fields);
                    if (hiddenLayers.ContainsKey(rawTypes[i].Fields["layer"]))
                    {
                        hiddenLayers[rawTypes[i].Fields["layer"]].SetNode(node, -1);
                    }
                }
                else if (rawTypes[i].Type == "preprocessor")
                {
                    topology.PreProcessor =
                        objectFactories[factoryName].CreateUserObject<IPreProcessor>(
                        rawTypes[i].Fields["dataType"], rawTypes[i].Fields);
                    if(settings.Mode == NetworkMode.Training)
                        topology.TrainingPreProcessor =
                            objectFactories[factoryName].CreateUserObject<ITrainingPreProcessor>(
                                rawTypes[i].Fields["dataType"], rawTypes[i].Fields);
                }
                else if (rawTypes[i].Type == "postprocessor")
                {
                    topology.PostProcessor = objectFactories[factoryName].
                        CreateUserObject<IPostProcessor>(rawTypes[i].Fields["dataType"], rawTypes[i].Fields);
                }
                else if (rawTypes[i].Type == "topology")
                {
                    topology.MetaData = rawTypes[i].Fields;
                    if(!rawTypes[i].Fields.ContainsKey("algorithmFactory"))
                    {
                        rawTypes[i].Fields.Add("algorithmFactory", factoryName);
                    }
                    topology.TrainingAlgorithm = objectFactories[rawTypes[i].Fields["algorithmFactory"]].
                        CreateUserObject<ITrainingAlgorithm>(
                            rawTypes[i].Fields["trainingAlgorithm"],
                            rawTypes[i].Fields);
                }
            }

            //This tranformation is required because nodes are linked to hidden layers
            //by names. The name of the layer is the key in the dictionary.
            IHiddenLayer[] layer = new IHiddenLayer[hiddenLayers.Count];
            int j = 0;
            foreach (KeyValuePair<string, IHiddenLayer> temp in hiddenLayers)
            {
                layer[j] = temp.Value;
                j++;
            }
            topology.HiddenLayers = layer;
            return topology;
        }
예제 #20
0
 public NeuralNetwork(NetworkTopology topology, NetworkSettings settings)
 {
     this.topology = topology;
     this.settings = settings;
 }