private void ProcessRowFirstPass() { AssertValid(assertGetters: true); Single label = 0; _labelGetter(ref label); if (Single.IsNaN(label)) { NumUnlabeledInstances++; label = 0; } var intLabel = (int)label; if (intLabel != label || intLabel < 0) { throw Host.Except("Invalid label: {0}", label); } _scoreGetter(ref _scores); Host.Check(_scores.Length == _scoresArr.Length); if (VBufferUtils.HasNaNs(ref _scores) || VBufferUtils.HasNonFinite(ref _scores)) { NumBadScores++; return; } _scores.CopyTo(_scoresArr); Single weight = 1; if (_weightGetter != null) { _weightGetter(ref weight); if (!FloatUtils.IsFinite(weight)) { NumBadWeights++; weight = 1; } } int j = 0; foreach (var index in Enumerable.Range(0, _scoresArr.Length).OrderBy(i => _scoresArr[i])) { _indicesArr[j++] = index; } UnweightedCounters.UpdateFirstPass(intLabel, _scoresArr, 1, _indicesArr); if (WeightedCounters != null) { WeightedCounters.UpdateFirstPass(intLabel, _scoresArr, weight, _indicesArr); } if (_clusterCentroids != null) { _featGetter(ref _features); VectorUtils.Add(ref _features, ref _clusterCentroids[_indicesArr[0]]); } }
public PathFlows <double, TLabel> Aggregate(PathFlows <double, TLabel>[] paths) { var prices = new double[paths.First().Flows.Length]; foreach (PathFlows <double, TLabel> pathFlows in paths) { VectorUtils.Add(ref prices, pathFlows.Flows); } VectorUtils.Mult(ref prices, 1.0 / paths.Length); return(new PathFlows <double, TLabel>(prices, paths.First().Labels)); }
protected void CombineCore(ref VBuffer <Single> dst, VBuffer <Single>[] src, Single[] weights = null) { Host.AssertNonEmpty(src); Host.Assert(weights == null || Utils.Size(weights) == Utils.Size(src)); // REVIEW: Should this be tolerant of NaNs? int len = GetClassCount(src); if (!TryNormalize(src)) { GetNaNOutput(ref dst, len); return; } var values = dst.Values; if (Utils.Size(values) < len) { values = new Single[len]; } else { Array.Clear(values, 0, len); } // Set the output to values. dst = new VBuffer <Single>(len, values, dst.Indices); Single weightTotal; if (weights == null) { weightTotal = src.Length; for (int i = 0; i < src.Length; i++) { VectorUtils.Add(ref src[i], ref dst); } } else { weightTotal = 0; for (int i = 0; i < src.Length; i++) { var w = weights[i]; weightTotal += w; VectorUtils.AddMult(ref src[i], w, ref dst); } } VectorUtils.ScaleBy(ref dst, 1 / weightTotal); }
/// <summary> /// Batch-parallel optimizer /// </summary> /// <remarks> /// REVIEW: consider getting rid of multithread-targeted members /// Using TPL, the distinction between Multithreaded and Sequential implementations is unnecessary /// </remarks> protected virtual float DifferentiableFunctionMultithreaded(ref VBuffer <float> xDense, ref VBuffer <float> gradient, IProgressChannel pch) { Contracts.Assert(_data == null); Contracts.Assert(_cursorFactory == null); Contracts.Assert(_numChunks > 0); Contracts.Assert(Utils.Size(_ranges) == _numChunks + 1); Contracts.Assert(Utils.Size(_localLosses) == _numChunks); Contracts.Assert(Utils.Size(_localGradients) + 1 == _numChunks); Contracts.AssertValueOrNull(pch); // Declare a local variable, since the lambda cannot capture the xDense. The gradient // calculation will modify the local gradients, but not this xx value. var xx = xDense; var gg = gradient; Parallel.For(0, _numChunks, ichk => { if (ichk == 0) { _localLosses[ichk] = DifferentiableFunctionComputeChunk(ichk, ref xx, ref gg, pch); } else { _localLosses[ichk] = DifferentiableFunctionComputeChunk(ichk, ref xx, ref _localGradients[ichk - 1], null); } }); gradient = gg; float loss = _localLosses[0]; for (int i = 1; i < _numChunks; i++) { VectorUtils.Add(ref _localGradients[i - 1], ref gradient); loss += _localLosses[i]; } return(loss); }
/// <summary> /// Combine a bunch of models into one by averaging parameters /// </summary> protected void CombineParameters(IList <IParameterMixer <Float> > models, out VBuffer <Float> weights, out Float bias) { Type type = GetType(); Contracts.Check(type == models[0].GetType(), "Submodel for parameter mixer has the wrong type"); var first = (LinearPredictor)models[0]; weights = default(VBuffer <Float>); first.Weight.CopyTo(ref weights); bias = first.Bias; for (int i = 1; i < models.Count; i++) { var m = models[i]; Contracts.Check(type == m.GetType(), "Submodel for parameter mixer has the wrong type"); var sub = (LinearPredictor)m; var subweights = sub.Weight; VectorUtils.Add(ref subweights, ref weights); bias += sub.Bias; } VectorUtils.ScaleBy(ref weights, (Float)1 / models.Count); bias /= models.Count; }