| 341 | } |
| 342 | |
| 343 | TVector<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 |
no test coverage detected