/// <summary> /// Construct with a single IActivationFunction. /// </summary> /// <param name="activationFn"></param> public NeatActivationFunctionLibrary(IActivationFunction activationFn) { _activationFn = activationFn; _activationFnInfo = new ActivationFunctionInfo(0, 1.0, activationFn); _activationFnInfoList = new List<ActivationFunctionInfo>(1); _activationFnInfoList.Add(_activationFnInfo); }
/// <summary> /// Normalize the selection probabilities of the provided ActivationFunctionInfo items. /// </summary> private static void NormalizeSelectionProbabilities(IList <ActivationFunctionInfo> fnList) { double total = 0.0; int count = fnList.Count; for (int i = 0; i < count; i++) { total += fnList[i].SelectionProbability; } if (Math.Abs(total - 1.0) < 0.0001) { // Probabilities already normalized to within acceptable limits (from rounding errors). return; } // Normalize the probabilities. Note that ActivationFunctionInfo is immutable therefore // we replace the existing items. for (int i = 0; i < count; i++) { ActivationFunctionInfo item = fnList[i]; fnList[i] = new ActivationFunctionInfo(item.Id, item.SelectionProbability / total, item.ActivationFunction); } }
/// <summary> /// Reads an IActivationFunctionLibrary from the provided XmlReader. /// </summary> public static IActivationFunctionLibrary ReadActivationFunctionLibrary(XmlReader xr) { XmlIoUtils.MoveToElement(xr, false, __ElemActivationFunctions); // Create a reader over the sub-tree. List <ActivationFunctionInfo> fnList = new List <ActivationFunctionInfo>(); using (XmlReader xrSubtree = xr.ReadSubtree()) { // Re-scan for the root element. XmlIoUtils.MoveToElement(xrSubtree, false); // Move to first function elem. XmlIoUtils.MoveToElement(xrSubtree, true, __ElemActivationFn); // Read function elements. do { int id = XmlIoUtils.ReadAttributeAsInt(xrSubtree, __AttrId); double selectionProb = XmlIoUtils.ReadAttributeAsDouble(xrSubtree, __AttrProbability); string fnName = xrSubtree.GetAttribute(__AttrName); // Lookup function name. IActivationFunction activationFn = GetActivationFunction(fnName); // Add new function to our list of functions. ActivationFunctionInfo fnInfo = new ActivationFunctionInfo(id, selectionProb, activationFn); fnList.Add(fnInfo); }while(xrSubtree.ReadToNextSibling(__ElemActivationFn)); } // If we have read library items then ensure that their selection probabilities are normalized. if (fnList.Count != 0) { NormalizeSelectionProbabilities(fnList); } return(new DefaultActivationFunctionLibrary(fnList)); }
/// <summary> /// Normalize the selection probabilities of the provided ActivationFunctionInfo items. /// </summary> private static void NormalizeSelectionProbabilities(IList<ActivationFunctionInfo> fnList) { double total = 0.0; int count = fnList.Count; for(int i=0; i<count; i++) { total += fnList[i].SelectionProbability; } if(Math.Abs(total - 1.0) < 0.0001) { // Probabilities already normalized to within acceptable limits (from rounding errors). return; } // Normalize the probabilities. Note that ActivationFunctionInfo is immutable therefore // we replace the existing items. for(int i=0; i<count; i++) { ActivationFunctionInfo item = fnList[i]; fnList[i] = new ActivationFunctionInfo(item.Id, item.SelectionProbability/total, item.ActivationFunction); } }
/// <summary> /// Reads an IActivationFunctionLibrary from the provided XmlReader. /// </summary> public static IActivationFunctionLibrary ReadActivationFunctionLibrary(XmlReader xr) { XmlIoUtils.MoveToElement(xr, false, __ElemActivationFunctions); // Create a reader over the sub-tree. List<ActivationFunctionInfo> fnList = new List<ActivationFunctionInfo>(); using(XmlReader xrSubtree = xr.ReadSubtree()) { // Re-scan for the root element. XmlIoUtils.MoveToElement(xrSubtree, false); // Move to first function elem. XmlIoUtils.MoveToElement(xrSubtree, true, __ElemActivationFn); // Read function elements. do { int id = XmlIoUtils.ReadAttributeAsInt(xrSubtree, __AttrId); double selectionProb = XmlIoUtils.ReadAttributeAsDouble(xrSubtree, __AttrProbability); string fnName = xrSubtree.GetAttribute(__AttrName); // Lookup function name. IActivationFunction activationFn = GetActivationFunction(fnName); // Add new function to our list of functions. ActivationFunctionInfo fnInfo = new ActivationFunctionInfo(id, selectionProb, activationFn); fnList.Add(fnInfo); } while(xrSubtree.ReadToNextSibling(__ElemActivationFn)); } // If we have read library items then ensure that their selection probabilities are normalized. if(fnList.Count != 0) { NormalizeSelectionProbabilities(fnList); } return new DefaultActivationFunctionLibrary(fnList); }