private static Task<Tuple<long, TrainedNework_Simple>> TrainAsync(Tuple<string, EncogOCR_SketchData[]>[] sketches, long token, bool shouldGenerateGarbage, CancellationTokenSource cancel) { CancellationToken cancelToken = cancel.Token; return Task.Run(() => { string[] outputMap = sketches. Select(o => o.Item1). ToArray(); int inputSize = sketches[0].Item2[0].NNInput.Length; #region Training Data List<double[]> inputs = new List<double[]>(); List<double[]> outputs = new List<double[]>(); int groupIndex = 0; foreach (var group in sketches) { double[] output = Enumerable.Range(0, outputMap.Length). Select((o, i) => i == groupIndex ? 1d : 0d). ToArray(); foreach (var input in group.Item2) { inputs.Add(input.NNInput); outputs.Add(output); } groupIndex++; } if (shouldGenerateGarbage) { GenerateGarbageData(sketches.SelectMany(o => o.Item2).ToArray(), outputMap.Length, inputs, outputs); } #endregion //NOTE: If there is an exception, the network couldn't be trained BasicNetwork network = null; try { network = UtilityEncog.GetTrainedNetwork2(inputs.ToArray(), outputs.ToArray(), 20, 300, cancelToken).NetworkOrNull; } catch (Exception) { } var returnProps = new TrainedNework_Simple() { InputSize = inputSize, Outputs = outputMap, Network = network, }; return Tuple.Create(token, returnProps); }, cancelToken); }
private static Chain[] GetChainsSprtFragments(List<Tuple<int, int>> segments, Tuple<int, int>[] pointCounts) { List<int> ends = pointCounts.SelectMany(o => Enumerable.Repeat(o.Item1, o.Item2)).ToList(); List<Chain> retVal = new List<Chain>(); while (ends.Count > 0) { Chain chain = GetChainsSprtSingle(segments, ends[0], ends.Where(o => o != ends[0]).ToArray()); ends.Remove(chain.Points[0]); ends.Remove(chain.Points[chain.Points.Length - 1]); retVal.Add(chain); } return retVal.ToArray(); }
private void AssertThatWeWontDuplicateQueueNames() { var queueCounts = new Tuple<string, Type>[0] .Union(CommandTypes.Select(t => new Tuple<string, Type>(PathFactory.QueuePathFor(t), t))) .Union(RequestTypes.Select(t => new Tuple<string, Type>(PathFactory.QueuePathFor(t), t))) .Union(EventTypes.Select(t => new Tuple<string, Type>(PathFactory.TopicPathFor(t), t))) .GroupBy(queue => queue.Item1) .Where(dupe => dupe.Count() > 1).ToArray(); if (queueCounts.None()) return; var badTypes = queueCounts.SelectMany(dupe => dupe.Select( d => d.Item2.Name)); var message = "Your message types {0} will result in a duplicate queue name.".FormatWith(string.Join(", ", badTypes)); throw new BusException(message); }
// Not required, just for convenience. public static Tuple <TResult> SelectMany <TSource, TResult> (this Tuple <TSource> source, Func <TSource, Tuple <TResult> > selector) => source.SelectMany(selector, Functions.False);
private static double[][] GetForces(double[][] positions, Tuple<int, int, double>[] desiredDistances, double mult) { // Calculate forces Tuple<int, double[]>[] forces = desiredDistances. //AsParallel(). //TODO: if distances.Length > threshold, do this in parallel SelectMany(o => GetForces_Calculate(o.Item1, o.Item2, o.Item3, positions, mult)). ToArray(); // Give them a very slight pull toward the origin so that the cloud doesn't drift away double[] center = MathND.GetCenter(positions); double centerMult = mult * -5; double[] centerPullForce = MathND.Multiply(center, centerMult); // Group by item var grouped = forces. GroupBy(o => o.Item1). OrderBy(o => o.Key); return grouped. Select(o => { double[] retVal = centerPullForce.ToArray(); // clone the center pull force foreach (var force in o) { retVal = MathND.Add(retVal, force.Item2); } return retVal; }). ToArray(); }
/// <param name="path">Application relative path to the module directory.</param> /// <param name="resources">All the unresolved resources found in the module.</param> /// <param name="isResourceOrderFixed">When true, the resources will not be sorted by their dependency ordering.</param> public UnresolvedModule(string path, UnresolvedResource[] resources, string location, bool isResourceOrderFixed) { this.path = path; this.location = location; var pathsInModule = new HashSet<string>(resources.Select(resource => resource.Path)); partition = PartitionResourceReferences(resources, pathsInModule); // Store all the references to external resources. this.externalReferences = partition.SelectMany(p => p.Item2).Distinct().ToArray(); // The resources now only contain references found in this module. var resolvedResources = partition.Select(p => p.Item1).ToArray(); if (isResourceOrderFixed) { this.resources = resolvedResources; } else { this.resources = OrderScriptsByDependency(resolvedResources); } }