public void AddLayer(int size) { Layer newLayer = new Layer(this.Owner); newLayer.Add(size, activation, loss, bias); if (layers.Count == 0) { layers.Add(newLayer); } else { Layer previousLayer = layers.Last(); layers.Add(newLayer); switch (connection) { case CONNECTIONS.ALL_IN_ALL: //previousLayer.Synapses.Clear(); foreach (Neuron n1 in previousLayer.Neurons) { foreach (Neuron n2 in newLayer.Neurons) { Synapse s = new Synapse(this.Owner); s.previous = n1; s.next = n2; s.learnFactor = learnFactor; newLayer.Synapses.Add(s); } } break; default: break; } } }
public string GenerateSaveCode(List <string> values = null, int tab = 0) { string value = ""; for (int i = 1; i < tab; i++) { value += " "; } value += "ID: " + this._id.ToString() + " | Class: " + _ClassName; if (Owner == null) { return(""); } switch (_ClassName) { case "Synapse": Synapse oSynapse = (Synapse)this; value += " | Weight: " + oSynapse.weight.ToString(); value += " | Learn Factor: " + oSynapse.learnFactor.ToString(); value += " | Previous: " + oSynapse.previous.id.ToString(); value += " | Next: " + oSynapse.next.id.ToString(); if (values != null) { values.Add(value); } value += "\n"; break; case "Neuron": Neuron oNeuron = (Neuron)this; value += " | Father: " + oNeuron.father.id.ToString(); value += " | Bias: " + oNeuron.bias.ToString(); value += " | Activation: " + oNeuron.activation.ToString(); value += " | Loss: " + oNeuron.loss.ToString(); if (values != null) { values.Add(value); } value += "\n"; break; case "Layer": Layer oLayer = (Layer)this; value += " | Activation: " + oLayer.activation.ToString(); value += " | Loss: " + oLayer.loss.ToString(); if (values != null) { values.Add(value); } value += "\n"; foreach (Neuron item in oLayer.Neurons) { value += item.GenerateSaveCode(values, tab + 4); } foreach (Synapse item in oLayer.Synapses) { value += item.GenerateSaveCode(values, tab + 4); } break; case "NeuralNetwork": NeuralNetwork oNeuralNetwork = (NeuralNetwork)this; if (values != null) { values.Add(value); } value += "\n"; foreach (Layer item in oNeuralNetwork.layers) { value += item.GenerateSaveCode(values, tab + 4); } break; } return(value); }
public void LoadFromCode(List <string> code) { string s; string s2; string tag; string[] ss; string[] ss2; Synapse _synapse; Neuron _neuron; Layer _layer; Dictionary <int, NeuralObject> mapping = new Dictionary <int, NeuralObject>(); int _id = -1; string _class = ""; ACTIVATION _activation = ACTIVATION.SOFTMAX; LOSS _loss = LOSS.CROSS_ENTROPY; int _father = -1; double _bias = 0.00; double _weight = 0.00; double _learnfactor = 0.00; int _previous = -1; int _next = -1; int maxID = -1; for (int i = 0; i < code.Count; i++) { s = code[i].Trim(); ss = s.Split('|'); for (int j = 0; j < ss.Length; j++) { s = ss[j].Trim(); ss2 = s.Split(':'); s = ss2[0].Trim(); s2 = ss2[1].Trim(); switch (s) { default: throw new System.Exception("unknow parameter"); case "ID": if (!int.TryParse(s2, out _id)) { throw new System.Exception("id not integer"); } break; case "Class": _class = s2; break; case "Activation": if (!Enum.TryParse <ACTIVATION>(s2, out _activation)) { throw new System.Exception("invalid activation"); } break; case "Loss": if (!Enum.TryParse <LOSS>(s2, out _loss)) { throw new System.Exception("invalid loss"); } break; case "Bias": if (!double.TryParse(s2, out _bias)) { throw new System.Exception("bias is not double"); } break; case "Father": if (!int.TryParse(s2, out _father)) { throw new System.Exception("father not integer"); } break; case "Weight": if (!double.TryParse(s2, out _weight)) { throw new System.Exception("weight is not double"); } break; case "Learn Factor": if (!double.TryParse(s2, out _learnfactor)) { throw new System.Exception("learn factor is not double"); } break; case "Previous": if (!int.TryParse(s2, out _previous)) { throw new System.Exception("previous not integer"); } break; case "Next": if (!int.TryParse(s2, out _next)) { throw new System.Exception("next not integer"); } break; } } maxID = maxID < _id ? _id : maxID; _lastid = _id; switch (_class) { case "NeuralNetwork": break; case "Layer": _layer = new Layer(this); _layer.activation = _activation; _layer.loss = _loss; layers.Add(_layer); mapping.Add(_id, _layer); break; case "Neuron": _neuron = new Neuron(this); _neuron.father = (Layer)mapping[_father]; _neuron.bias = _bias; _neuron.activation = _activation; _neuron.loss = _loss; layers.Last().Neurons.Add(_neuron); mapping.Add(_id, _neuron); break; case "Synapse": _synapse = new Synapse(this); _synapse.weight = _weight; _synapse.learnFactor = _learnfactor; _synapse.previous = (Neuron)mapping[_previous]; _synapse.next = (Neuron)mapping[_next]; layers.Last().Synapses.Add(_synapse); mapping.Add(_id, _synapse); break; default: break; } } _lastid = maxID + 1; }