MCPcopy Create free account
hub / github.com/catboost/catboost / CalcSageValues

Function CalcSageValues

catboost/libs/fstr/sage_values.cpp:343–427  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

341}
342
343TVector<TVector<double>> CalcSageValues(
344 const TFullModel& model,
345 const TDataProvider& dataset,
346 int logPeriod,
347 NPar::ILocalExecutor* localExecutor,
348 size_t nSamples,
349 size_t batchSize,
350 bool detectConvergence)
351{
352 CB_ENSURE(model.ModelTrees->GetDimensionsCount() == 1, "Model must not be trained for multiclassification");
353
354 // setting algorithm params
355 TRestorableFastRng64 rand(228);
356 size_t featuresCount = dataset.MetaInfo.GetFeatureCount();
357 batchSize = Min(batchSize, size_t(dataset.GetObjectCount()));
358 auto featuresLayout = dataset.MetaInfo.FeaturesLayout;
359 const double convergenceThreshold = 0.1;
360
361 FullSubsetIndexingPtrWrapper fullSubsetIndexingPtrWrapper;
362 MarginalImputer imputer(dataset, localExecutor, &rand);
363
364 // creating loss holder
365 NCatboostOptions::TLossDescription metricDescription;
366 NCatboostOptions::TLossDescription lossDescription;
367 bool needYetiRankPairs = false;
368 THolder<IMetric> metric;
369
370 CreateMetricAndLossDescriptionForLossChange(
371 model,
372 &metricDescription,
373 &lossDescription,
374 &needYetiRankPairs,
375 &metric
376 );
377
378 CB_ENSURE_INTERNAL(metric->IsAdditiveMetric(), "Loss function must be additive");
379
380 TVector<THolder<IMetric>> metrics;
381 metrics.push_back(std::move(metric));
382
383 // main algorithm: calculating sage values
384 TImportanceLogger samplingIterationsLogger(nSamples, "sampling iterations passed",
385 "Calculating SAGE values...", logPeriod);
386 TProfileInfo samplingItertionsProfile(nSamples);
387 TVector<TVector<double>> sageValues(featuresCount, TVector<double>{0});
388 for (size_t i = 0; i < nSamples; ++i) {
389 samplingItertionsProfile.StartIterationBlock();
390
391 // sampling batch of dataset elements
392 auto datasetBatch = GetRandomDatasetBatch(dataset, batchSize, &rand, localExecutor);
393
394 // generating features permutation
395 auto featuresPermutation = GenerateFeaturesPermutation(featuresLayout, &rand);
396
397 // running approximation algorithm
398 double previousLoss = CalculateModelLoss(model, datasetBatch, metrics, &rand, localExecutor);
399 for (size_t j = 0; j < featuresCount; ++j) {
400 // preparing dataset, sampling disabled features

Callers 2

CalcAndOutputSageValuesFunction · 0.85
GetFeatureImportancesFunction · 0.85

Calls 15

GetRandomDatasetBatchFunction · 0.85
CalculateModelLossFunction · 0.85
StartIterationBlockMethod · 0.80
ImputeInplaceMethod · 0.80
GetExternalFeatureIdxMethod · 0.80
FinishIterationBlockMethod · 0.80
GetProfileResultsMethod · 0.80
MeanFunction · 0.70
MinFunction · 0.50

Tested by

no test coverage detected