22 long num_dense_features) :
23 m_NumDenseFeatures(num_dense_features), m_DenseInit(std::move(di)), m_SparseInit(std::move(si)) {
28 m_DenseInit->get_initial_weight(label_id, target.head(m_NumDenseFeatures),
objective);
29 m_SparseInit->get_initial_weight(label_id, target.tail(target.size() - m_NumDenseFeatures),
objective);
47 auto minimizer = std::make_unique<solvers::NewtonWithLineSearch>(
m_NumFeatures);
56 throw std::logic_error(
"Could not cast minimizer to <NewtonWithLineSearch>");
60 double small_count =
static_cast<double>(std::min(num_pos,
get_data().num_examples() - num_pos));
61 double epsilon_scale = std::max(small_count, 1.0) /
static_cast<double>(
get_data().
num_examples());
63 std::size_t actual_num_pos = 0;
64 std::size_t actual_num_neg = 0;
67 for(
const auto& row : shortlist) {
68 if(label_vec->coeff(row)) {
74 epsilon_scale = std::max(
static_cast<double>(std::min(actual_num_neg, actual_num_pos)), 1.0 ) /
static_cast<double>( actual_num_pos + actual_num_neg );
83 throw std::logic_error(
"Could not cast objective to <DenseAndSparseLinearBase>");
92 objective->update_features(shortlisted_dense, shortlisted_sparse);
94 target_labels.resize(
ssize(shortlist));
97 for(
const auto& row : shortlist) {
98 target_labels.coeffRef(target_id) = label_vec->coeff(row);
115 return std::make_unique<CombinedWeightInitializer>(std::move(dense_init), std::move(sparse_init), dense->cols());
120 return std::make_shared<model::SparseModel>(
num_features, spec);
123 std::unique_ptr<postproc::PostProcessor>
133 std::shared_ptr<const GenericFeatureMatrix> dense_data,
135 std::shared_ptr<init::WeightInitializationStrategy> dense_init,
137 std::shared_ptr<init::WeightInitializationStrategy> sparse_init,
139 std::shared_ptr<postproc::PostProcessFactory> post_proc,
140 std::shared_ptr<TrainingStatsGatherer> gatherer,
141 std::shared_ptr<
const std::vector<std::vector<long>>> shortlist) :
143 m_NewtonSettings( std::move(hyper_params) ),
145 m_DenseReplicator(std::move(dense_data) ),
146 m_Shortlist( std::move(shortlist) ),
147 m_PostProcessor( std::move(post_proc) ),
148 m_DenseInitStrategy( std::move(dense_init) ),
149 m_SparseInitStrategy( std::move(sparse_init) ),
150 m_StatsGather( std::move(gatherer) ),
151 m_NumFeatures(m_SparseReplicator.get_local()->cols() + m_DenseReplicator.get_local()->cols()),
152 m_DenseReg(dense_reg),
153 m_SparseReg(sparse_reg)
162 std::shared_ptr<const DatasetBase> data,
163 std::shared_ptr<const GenericFeatureMatrix> dense,
164 std::shared_ptr<
const std::vector<std::vector<long>>> shortlist,
174 return std::make_shared<CascadeTraining>(std::move(data),
std::unique_ptr< init::WeightsInitializer > m_SparseInit
std::unique_ptr< init::WeightsInitializer > m_DenseInit
CombinedWeightInitializer(std::unique_ptr< init::WeightsInitializer > di, std::unique_ptr< init::WeightsInitializer > si, long num_dense_features)
void get_initial_weight(label_id_t label_id, Eigen::Ref< DenseRealVector > target, objective::Objective &objective) override
Generate an initial vector for the given label. The result should be placed in target.
long num_features() const override
std::shared_ptr< objective::Objective > make_objective() const override
Makes an Objective object suitable for the dataset.
HyperParameters m_NewtonSettings
std::unique_ptr< solvers::Minimizer > make_minimizer() const override
Makes a Minimizer object suitable for the dataset.
std::shared_ptr< init::WeightInitializationStrategy > m_DenseInitStrategy
std::shared_ptr< model::Model > make_model(long num_features, model::PartialModelSpec spec) const override
Creates the model that will be used to store the results.
std::unique_ptr< init::WeightsInitializer > make_initializer() const override
Makes a WeightsInitializer object.
std::shared_ptr< const std::vector< std::vector< long > > > m_Shortlist
CascadeTraining(std::shared_ptr< const DatasetBase > tfidf_data, std::shared_ptr< const GenericFeatureMatrix > dense_data, HyperParameters hyper_params, std::shared_ptr< init::WeightInitializationStrategy > dense_init, real_t dense_reg, std::shared_ptr< init::WeightInitializationStrategy > sparse_init, real_t sparse_reg, std::shared_ptr< postproc::PostProcessFactory > post_proc, std::shared_ptr< TrainingStatsGatherer > gatherer, std::shared_ptr< const std::vector< std::vector< long >>> shortlist=nullptr)
TrainingStatsGatherer & get_statistics_gatherer() override
parallel::NUMAReplicator< const GenericFeatureMatrix > m_DenseReplicator
std::unique_ptr< postproc::PostProcessor > make_post_processor(const std::shared_ptr< objective::Objective > &objective) const override
Makes a PostProcessor object.
std::shared_ptr< postproc::PostProcessFactory > m_PostProcessor
std::shared_ptr< TrainingStatsGatherer > m_StatsGather
void update_minimizer(solvers::Minimizer &base_minimizer, label_id_t label_id) const override
Updates the setting of the Minimizer for handling label label_id.
void update_objective(objective::Objective &base_objective, label_id_t label_id) const override
Updates the setting of the Objective for handling label label_id.
parallel::NUMAReplicator< const GenericFeatureMatrix > m_SparseReplicator
std::shared_ptr< init::WeightInitializationStrategy > m_SparseInitStrategy
long num_examples() const noexcept
Get the total number of instances, i.e. the number of rows in the feature matrix.
std::shared_ptr< const BinaryLabelVector > get_labels(label_id_t id) const
virtual long num_positives(label_id_t id) const
This class represents a set of hyper-parameters.
hyper_param_t get(const std::string &name) const
Gets the hyper-parameter with the given name, or throws if it does not exist.
void apply(HyperParameterBase &target) const
This class gathers the setting-specific parts of the training process.
const DatasetBase & get_data() const
Base class for all weight initializers.
Strong typedef for an int to signify a label id.
Base class for implementationa of an objective that combines dense features and sparse features.
Class that models an optimization objective.
constexpr T to_index() const
! Explicitly convert to an integer.
auto get_features(const DatasetBase &ds)
std::shared_ptr< WeightInitializationStrategy > create_zero_initializer()
Creates an initialization strategy that initializes all weight vectors to zero.
std::unique_ptr< DenseAndSparseLinearBase > make_sp_dense_squared_hinge(std::shared_ptr< const GenericFeatureMatrix > dense_features, real_t dense_reg_strength, std::shared_ptr< const GenericFeatureMatrix > sparse_features, real_t sparse_reg_strength)
FactoryPtr create_identity()
Main namespace in which all types, classes, and functions are defined.
types::DenseRowMajor< real_t > DenseFeatures
Dense Feature Matrix in Row Major format.
constexpr auto ssize(const C &c) -> std::common_type_t< std::ptrdiff_t, std::make_signed_t< decltype(c.size())>>
signed size free function. Taken from https://en.cppreference.com/w/cpp/iterator/size
types::DenseVector< std::int8_t > BinaryLabelVector
Dense vector for storing binary labels.
types::SparseRowMajor< real_t > SparseFeatures
Sparse Feature Matrix in Row Major format.
SparseFeatures shortlist_features(const SparseFeatures &source, const std::vector< long > &shortlist)
std::shared_ptr< TrainingSpec > create_cascade_training(std::shared_ptr< const DatasetBase > data, std::shared_ptr< const GenericFeatureMatrix > dense, std::shared_ptr< const std::vector< std::vector< long >>> shortlist, HyperParameters params, CascadeTrainingConfig config)
float real_t
The default type for floating point values.
std::shared_ptr< TrainingStatsGatherer > StatsGatherer
std::shared_ptr< init::WeightInitializationStrategy > DenseInit
std::shared_ptr< init::WeightInitializationStrategy > SparseInit
std::shared_ptr< postproc::PostProcessFactory > PostProcessing
Specifies how to interpret a weight matrix for a partial model.