public static Network fromDescription(string description) { Network network = new Network(); string[] desc = description.Split(); int idx = 0; Layer ant = null; while (idx < desc.Length) { int nNeurons = Int32.Parse(desc[idx++]); string sAct = desc[idx++]; ActivationFunction fAct = Layer.stringToActivation(sAct); if (ant == null) { network.input = new Layer(nNeurons, fAct); ant = network.input; continue; } Layer newLayer = new Layer(nNeurons, fAct); Connections newConnection = new Connections(ant, newLayer); network.actionList.Add(newConnection); network.actionList.Add(newLayer); ant = newLayer; } network.output = ant; return network; }
/// <summary> /// Parser of the lua format /// </summary> /// <param name="reader">Correctly open stream file reader</param> public void readfromFile(StreamReader reader) { /* return { "256 inputs 1024 tanh 10 softmax", matrix.fromString[[273418 ascii */ string line; //Avoid comments and lua lines do { line = reader.ReadLine().Trim(); } while (line.StartsWith("#") || line.StartsWith("return")); string description = line; description = description.Replace("\"", ""); string[] camps = description.Split(); int ncamps = camps.Length; // Read the Network info // Nneurons0 tipe0 .. NneuronsN tipeN Layer ant = null; //Avoid lua code do { line = reader.ReadLine().Trim(); } while (line.StartsWith("#") || line.StartsWith("matrix")); for (int i = 0; i < ncamps; i+=2) { int nNeurons = int.Parse(camps[i]); string type = camps[i + 1]; if (ant == null) { // Is the first layer this.input = addLayer(nNeurons, Layer.stringToActivation(type)); ant = this.input; continue; } Layer newLayer = new Layer(nNeurons, Layer.stringToActivation(type)); Connections newConnection = new Connections(ant, newLayer); this.actionList.Add(newConnection); this.actionList.Add(newLayer); newConnection.readWeightsfromStream(reader); ant = newLayer; //Console.WriteLine(newConnection.ToString()); } if (ant != null) this.output = ant; }