public void TestWeightedNetworkMergerMerge() { INetworkMerger merger = new WeightedNetworkMerger(3, 8); INetwork netA = NetworkMergerTestUtils.GenerateNetwork(1); INetwork netB = NetworkMergerTestUtils.GenerateNetwork(9); merger.AddMergeEntry("layers.*.weights"); merger.Merge(netA, netB); IRegistryResolver resolverA = new RegistryResolver(netA.Registry); IRegistryResolver resolverB = new RegistryResolver(netB.Registry); INDArray weightsA = resolverA.ResolveGet <INDArray>("layers.*.weights")[0]; INDArray weightsB = resolverB.ResolveGet <INDArray>("layers.*.weights")[0]; float firstValueA = weightsA.GetValue <float>(0, 0); float firstValueB = weightsB.GetValue <float>(0, 0); // the first value will change Assert.AreEqual(6.82, System.Math.Round(firstValueA * 100) / 100); // the second net may not be changed Assert.AreEqual(9, firstValueB); merger.RemoveMergeEntry("layers.*.weights"); merger.Merge(netA, netB); weightsA = resolverA.ResolveGet <INDArray>("layers.*.weights")[0]; firstValueA = weightsA.GetValue <float>(0, 0); // may not change Assert.AreEqual(6.82, System.Math.Round(firstValueA * 100) / 100); }
/// <summary> /// Process a certain ndarray with a certain computation handler. /// </summary> /// <param name="array">The ndarray to process.</param> /// <param name="handler">The computation handler to do the processing with.</param> /// <returns>An ndarray with the processed contents of the given array (can be the same or a new one).</returns> internal override INDArray ProcessDirect(INDArray array, IComputationHandler handler) { int recordLength = (int)(array.Length / array.Shape[0]); long[] firstBufferIndices = new long[array.Shape.Length]; long[] secondBufferIndices = new long[array.Shape.Length]; _random = new Random(31415926); // fixed rng for reproducability for (int i = 0; i < array.Shape[0]; i++) { int swapIndex = _random.Next((int)array.Shape[0]); for (int y = 0; y < recordLength; y++) { NDArrayUtils.GetIndices(recordLength * i + y, array.Shape, array.Strides, firstBufferIndices); NDArrayUtils.GetIndices(recordLength * swapIndex + y, array.Shape, array.Strides, secondBufferIndices); double firstValue = array.GetValue <double>(firstBufferIndices); double secondValue = array.GetValue <double>(secondBufferIndices); array.SetValue(secondValue, firstBufferIndices); array.SetValue(firstValue, secondBufferIndices); } } return(array); }
public void TestAverageNetworkMergerMerge() { INetworkMerger merger = new AverageNetworkMerger(); INetwork netA = NetworkMergerTestUtils.GenerateNetwork(1); INetwork netB = NetworkMergerTestUtils.GenerateNetwork(5); merger.AddMergeEntry("layers.*.weights"); merger.Merge(netA, netB); IRegistryResolver resolverA = new RegistryResolver(netA.Registry); IRegistryResolver resolverB = new RegistryResolver(netB.Registry); INDArray weightsA = resolverA.ResolveGet <INDArray>("layers.*.weights")[0]; INDArray weightsB = resolverB.ResolveGet <INDArray>("layers.*.weights")[0]; float firstValueA = weightsA.GetValue <float>(0, 0); float firstValueB = weightsB.GetValue <float>(0, 0); // the first value will change Assert.AreEqual(3, firstValueA); // the second net may not be changed Assert.AreEqual(5, firstValueB); merger.RemoveMergeEntry("layers.*.weights"); merger.Merge(netA, netB); weightsA = resolverA.ResolveGet <INDArray>("layers.*.weights")[0]; firstValueA = weightsA.GetValue <float>(0, 0); Assert.AreEqual(3, firstValueA); }
/// <summary> /// Score an intermediate result (part of the entire validation dataset was used as specified by the validation iterator). /// </summary> /// <param name="predictions">The predictions.</param> /// <param name="targets">The targets.</param> /// <param name="handler">The computation handler.</param> protected override void ScoreIntermediate(INDArray predictions, INDArray targets, IComputationHandler handler) { predictions = handler.FlattenTimeAndFeatures(predictions); targets = handler.FlattenTimeAndFeatures(targets); if (predictions.Shape[1] != 1) { throw new InvalidOperationException($"Cannot score uni-class classification accuracy on targets with != 1 feature shape (feature shape length was {predictions.Shape[1]})."); } int totalClassifications = ParameterRegistry.Get <int>("total_classifications"); int correctClassifications = ParameterRegistry.Get <int>("correct_classifications"); double lowerThreshold = ParameterRegistry.Get <double>("lower_threshold"); double upperThreshold = ParameterRegistry.Get <double>("upper_threshold"); for (int i = 0; i < predictions.Shape[0]; i++) { double value = predictions.GetValue <double>(i, 0); int target = targets.GetValue <int>(i, 0); if (value < lowerThreshold && target == 0 || value > upperThreshold && target == 1) { correctClassifications++; } } totalClassifications += (int)predictions.Shape[0]; ParameterRegistry["total_classifications"] = totalClassifications; ParameterRegistry["correct_classifications"] = correctClassifications; }
/// <inheritdoc /> internal override INDArray ProcessDirect(INDArray array, IComputationHandler handler) { //BTF with single feature dimension, TODO couldn't feature dimension just be flattened and ignored? if (array.Rank != 3) { throw new ArgumentException($"Cannot one-hot encode ndarrays which are not of rank 3 (BTF with single feature dimension), but given ndarray was of rank {array.Rank}."); } INDArray encodedArray = handler.NDArray(array.Shape[0], array.Shape[1], array.Shape[2] * _valueToIndexMapping.Count); long[] bufferIndices = new long[3]; for (long i = 0; i < array.Shape[0] * array.Shape[1]; i++) { bufferIndices = NDArrayUtils.GetIndices(i, array.Shape, array.Strides, bufferIndices); object value = array.GetValue <int>(bufferIndices); if (!_valueToIndexMapping.ContainsKey(value)) { throw new ArgumentException($"Cannot one-hot encode unknown value {value}, value was not registered as a possible value."); } bufferIndices[2] = _valueToIndexMapping[value]; encodedArray.SetValue(1, bufferIndices); } return(encodedArray); }
internal override INDArray ProcessDirect(INDArray array, IComputationHandler handler) { double outputRange = MaxOutputValue - MinOutputValue; for (int i = 0; i < array.Shape[0]; i++) { for (int j = 0; j < array.Shape[1]; j++) { foreach (int index in PerIndexMinMaxMappings.Keys) { double[] minMaxRange = PerIndexMinMaxMappings[index]; double value = array.GetValue <double>(i, j, index); value = (value - minMaxRange[0]) / minMaxRange[2]; value = (value + MinOutputValue) * outputRange; array.SetValue(value, i, j, index); } } } return(array); }