public static void SaveNeuralNetwork(NeuralNetwork.Network neuralNetwork) { Stream stream = null; try { stream = File.Open(Config.Default.FilesLocation + NETWORK, FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); if (neuralNetwork != null) { bFormatter.Serialize(stream, neuralNetwork); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { if (stream != null) { stream.Close(); } } }
static void Main(string[] args) { var nn = new NeuralNetwork.Network(784, 200, 10, 0.2); TrainNetwork(ref nn); TestNetwork(ref nn); Console.WriteLine("\nDone!\nPress Enter to exit"); Console.ReadLine(); }
public static NetworkResult guessHTTPService() { NetworkResult result = new NetworkResult("Unknown", 100.0f); NeuralNetwork.Network net = new NeuralNetwork.Network(); net.addInput("Windows"); net.addInput("Linux"); net.addInput("FreeBSD"); net.addInput("PHP"); net.addInput("ASP.NET"); net.addInput("Static"); net.addInput("Ruby/Node.js"); return(result); }
private static void WithFile(string fileName, NeuralNetwork.Network network, Action <List <Int32>, NeuralNetwork.Network> action) { using (var file = File.OpenRead(fileName)) { using (var streamReader = new StreamReader(file)) { while (!streamReader.EndOfStream) { var line = streamReader.ReadLine(); var parts = line.Split(',').Select(v => int.Parse(v)).ToList(); action(parts, network); } } } }
private static void TrainNetwork(ref NeuralNetwork.Network network) { var fileName = ConfigurationManager.AppSettings["trainData"]; Stopwatch sw = Stopwatch.StartNew(); Console.WriteLine("Training Neural Network."); WithFile(fileName, network, (parts, nn) => { var inputs = GetInputs(parts); var targets = GetTargets(parts[0]); nn.Train(inputs, targets); }); sw.Stop(); Console.WriteLine("Training is finished!\n"); Console.WriteLine("Training took {0:0.000} ms", sw.ElapsedMilliseconds); }
private static void TestNetwork(ref NeuralNetwork.Network network) { var fileName = ConfigurationManager.AppSettings["testData"]; Console.WriteLine("\nTesting Neural Network."); Stopwatch sw = Stopwatch.StartNew(); List <short> results = new List <short>(); var counter = 0; WithFile(fileName, network, (parts, nn) => { var expectedValue = parts[0]; var inputs = GetInputs(parts); var result = nn.Query(inputs).MaxValueIndex(); results.Add((short)((expectedValue == result) ? 1 : 0)); counter++; }); sw.Stop(); Console.WriteLine("Testing is finished!\n"); Console.WriteLine("Testing took {0:0.000} ms", sw.ElapsedMilliseconds); var foregroundColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("\nSuccess rate is {0:0.000}%", (double)results.Sum(v => v) / counter); Console.ForegroundColor = foregroundColor; }
public static NeuralNetwork.Network LoadNeuralNetwork() { NeuralNetwork.Network neuralNetwork = null; Stream stream = null; try { stream = File.Open(Config.Default.FilesLocation + NETWORK, FileMode.Open); BinaryFormatter bFormatter = new BinaryFormatter(); neuralNetwork = (NeuralNetwork.Network)bFormatter.Deserialize(stream); } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { if (stream != null) { stream.Close(); } } return(neuralNetwork); }
public static OSResult guessOS(string[] foundServices) { OSResult result; // Initialize objects NeuralNetwork.Network net = new NeuralNetwork.Network(); // Load in data to memory if (!File.Exists(Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents", "Gerbil", "memstore", "OSServiceTraining.ini"))) { return(new OSResult("ERROR", 0.0f)); } string[] trainingData = File.ReadAllLines(Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents", "Gerbil", "memstore", "OSServiceTraining.ini")); // Calculate weights PairCounter pc = new PairCounter(); foreach (string i in trainingData) { string sName = i.Split('=')[0]; string fOS = i.Split('=')[1]; pc.Add(new Pair(sName, fOS)); } Dictionary <Pair, float> connectionWeights = getPercentagesFromPair(pc.getResults()); // TODO: Train network foreach (KeyValuePair <Pair, float> i in connectionWeights) { net.addInput(i.Key.item1); net.addOutput(i.Key.item2, i.Key.item1 + "Connector", i.Value, i.Key.item1); } // Feed data into tranined neural network foreach (string i in foundServices) { try { net.fireInput(i); } catch (NeuralNetwork.NodeNotFoundException e) { // Serive does not exist, since we are not in training mode, ignore. } catch { // A serious engine error occured. Throw fatal error. throw new FatalEngineException(); } } // Get outputs Dictionary <string, float> results = net.getResults(); string resultName = "Unknown"; float resultCertainty = 0.0f; float maxCertainty = 0.0f; // Find most likely answer foreach (KeyValuePair <string, float> i in results) { if (i.Value > resultCertainty) { resultName = i.Key; resultCertainty = i.Value; maxCertainty += i.Value; } } resultCertainty = resultCertainty / maxCertainty; result = new OSResult(resultName, resultCertainty); return(result); }