internal static ActivationFunction FromXML(XMLElement root) { if (root.GetAttribute("Name").Value == "None") { return(None()); } if (root.GetAttribute("Name").Value == "Sigmoid") { XMLElement customDataXE = root.ChildWithTag("CustomData"); float steepness = int.Parse(customDataXE.ChildWithTag("steepness").GetAttribute("Value").Value); return(Sigmoid(steepness)); } if (root.GetAttribute("Name").Value == "ReLU") { XMLElement customDataXE = root.ChildWithTag("CustomData"); bool zeroIsPositive = bool.Parse(customDataXE.ChildWithTag("zeroIsPositive").GetAttribute("Value").Value); return(ReLU(zeroIsPositive)); } if (root.GetAttribute("Name").Value == "TanH") { XMLElement customDataXE = root.ChildWithTag("CustomData"); float steepness = int.Parse(customDataXE.ChildWithTag("steepness").GetAttribute("Value").Value); return(TanH(steepness)); } if (root.GetAttribute("Name").Value == "LeakyReLU") { XMLElement customDataXE = root.ChildWithTag("CustomData"); float leakynessFactor = int.Parse(customDataXE.ChildWithTag("leakynessFactor").GetAttribute("Value").Value); bool zeroIsPositive = bool.Parse(customDataXE.ChildWithTag("zeroIsPositive").GetAttribute("Value").Value); return(LeakyReLU(leakynessFactor, zeroIsPositive)); } if (root.GetAttribute("Name").Value == "SoftPlus") { XMLElement customDataXE = root.ChildWithTag("CustomData"); float steepness = int.Parse(customDataXE.ChildWithTag("steepness").GetAttribute("Value").Value); return(SoftPlus(steepness)); } if (root.GetAttribute("Name").Value == "SoftMax") { XMLElement customDataXE = root.ChildWithTag("CustomData"); float steepness = int.Parse(customDataXE.ChildWithTag("steepness").GetAttribute("Value").Value); return(SoftMax(steepness)); } throw new NotImplementedException(); // Never reached }
internal static Layer FromXML(XMLElement root, Layer previousLayer) { float[] StringToArray(string s) { string[] valStrs = s.Split(','); float[] array = new float[valStrs.Length]; for (int i = 0; i < valStrs.Length; i++) { array[i] = float.Parse(valStrs[i]); } return array; } string type = root.Tag; if (type == typeof(InputLayer).Name) { int width = int.Parse(root.GetAttribute("Width").Value); int height = int.Parse(root.GetAttribute("Height").Value); int depth = int.Parse(root.GetAttribute("Depth").Value); return new InputLayer(width, height, depth); } if (type == typeof(MaxPoolingLayer).Name) { int filterSize = int.Parse(root.GetAttribute("FilterSize").Value); int stride = int.Parse(root.GetAttribute("Stride").Value); return new MaxPoolingLayer(filterSize, stride, previousLayer); } if (type == typeof(FullyConnectedLayer).Name) { int neurons = int.Parse(root.GetAttribute("Neurons").Value); ActivationFunctions.ActivationFunction activation = ActivationFunctions.FromXML(root.ChildWithTag("ActivationFunction")); FullyConnectedLayer layer = new FullyConnectedLayer(neurons, previousLayer, activation); float[] weights = StringToArray(root.ChildWithTag("Weights").Value); for (int i = 0; i < layer.Weights; i++) { layer.SetWeight(i, weights[i]); } float[] biases = StringToArray(root.ChildWithTag("Biases").Value); for (int i = 0; i < layer.Biases; i++) { layer.SetBias(i, biases[i]); } return layer; } if (type == typeof(ConvolutionalLayer).Name) { int filterCount = int.Parse(root.GetAttribute("FilterCount").Value); int filterSize = int.Parse(root.GetAttribute("FilterSize").Value); int stride = int.Parse(root.GetAttribute("Stride").Value); int zeroPadding = int.Parse(root.GetAttribute("ZeroPadding").Value); ActivationFunctions.ActivationFunction activation = ActivationFunctions.FromXML(root.ChildWithTag("ActivationFunction")); ConvolutionalLayer layer = new ConvolutionalLayer(filterCount, filterSize, stride, zeroPadding, previousLayer, activation); float[] weights = StringToArray(root.ChildWithTag("Weights").Value); for (int i = 0; i < layer.Weights; i++) { layer.SetWeight(i, weights[i]); } float[] biases = StringToArray(root.ChildWithTag("Biases").Value); for (int i = 0; i < layer.Biases; i++) { layer.SetBias(i, biases[i]); } return layer; } throw new NotImplementedException(); // Never reached }