/// <summary> /// Use an array to populate the memory of the neural network. /// </summary> /// <param name="array">An array of doubles.</param> /// <param name="network">The network to encode.</param> public static void ArrayToNetwork(double[] array, BasicNetwork network) { int index = 0; foreach (ILayer layer in network.Structure.Layers) { index = NetworkCODEC.ProcessLayer(network, layer, array, index); } network.Structure.FlatUpdate = FlatUpdateNeeded.Flatten; }
/// <summary> /// Flatten the weights of a neural network. /// </summary> public void FlattenWeights() { if (this.flat != null) { this.flatUpdate = FlatUpdateNeeded.Flatten; double[] targetWeights = this.flat.Weights; double[] sourceWeights = NetworkCODEC.NetworkToArray(this.network); EngineArray.ArrayCopy(sourceWeights, targetWeights); this.flatUpdate = FlatUpdateNeeded.None; // update context layers foreach (ILayer layer in this.layers) { if (layer is ContextLayer) { ContextLayer context = (ContextLayer)layer; if (context.FlatContextIndex != -1) { EngineArray.ArrayCopy( context.Context.Data, 0, this.flat.LayerOutput, context.FlatContextIndex, context.Context.Count); } } } // handle limited connection networks if (this.connectionLimited) { this.flat.ConnectionLimit = this.connectionLimit; } else { this.flat.ClearConnectionLimit(); } } }
/// <summary> /// Determine if the two neural networks are equal. /// </summary> /// <param name="network1">The first network.</param> /// <param name="network2">The second network.</param> /// <param name="precision">How many decimal places to check.</param> /// <returns>True if the two networks are equal.</returns> public static bool Equals(BasicNetwork network1, BasicNetwork network2, int precision) { double[] array1 = NetworkCODEC.NetworkToArray(network1); double[] array2 = NetworkCODEC.NetworkToArray(network2); if (array1.Length != array2.Length) { return(false); } double test = Math.Pow(10.0, precision); if (double.IsInfinity(test) || (test > long.MaxValue)) { String str = "Precision of " + precision + " decimal places is not supported."; #if logging if (NetworkCODEC.LOGGER.IsErrorEnabled) { NetworkCODEC.LOGGER.Error(str); } #endif throw new NeuralNetworkError(str); } foreach (double element in array1) { long l1 = (long)(element * test); long l2 = (long)(element * test); if (l1 != l2) { return(false); } } return(true); }
/// <summary> /// Calculate the size that an array should be to hold all of the weights and /// bias values. /// </summary> /// /// <returns>The size of the calculated array.</returns> public int CalculateSize() { return(NetworkCODEC.NetworkSize(_network)); }