9 #define DISABLE_SHARED_WEIGHTS 21 template <
typename It1,
typename It2>
24 template <
typename It1,
typename It2>
28 using Val1 =
typename std::iterator_traits<It1>::value_type;
29 using Val2 =
typename std::iterator_traits<It2>::value_type;
37 template <
typename It1,
typename It2>
73 template <
typename It1,
typename It2>
75 :
public std::iterator<std::random_access_iterator_tag, location_value<It1, It2>,
77 location_reference<It1, It2>
217 template <
typename W>
248 int load(
const char* model,
size_t length)
250 if (!model || length == 0)
253 _model_loaded =
false;
256 _ignore_linear.fill(
false);
276 mp.
skip(3 * ngram_len);
280 mp.
skip(3 * skips_len);
285 _no_constant = _command_line_arguments.find(
"--noconstant") != std::string::npos;
289 if (
find_opt_int(_command_line_arguments,
"--hash_seed", hash_seed) && hash_seed)
292 _interactions.clear();
293 find_opt(_command_line_arguments,
"-q", _interactions);
294 find_opt(_command_line_arguments,
"--quadratic", _interactions);
295 find_opt(_command_line_arguments,
"--cubic", _interactions);
296 find_opt(_command_line_arguments,
"--interactions", _interactions);
300 std::vector<std::string> vec_sorted;
301 for (
const std::string& interaction : _interactions)
303 std::string sorted_i(interaction);
304 std::sort(std::begin(sorted_i), std::end(sorted_i));
305 vec_sorted.push_back(sorted_i);
307 _interactions = vec_sorted;
312 if (_command_line_arguments.find(
"--cb_explore_adf") != std::string::npos)
315 if (
find_opt_int(_command_line_arguments,
"--bag", _bag_size))
318 num_weights = _bag_size;
321 _minimum_epsilon = 0.f;
322 find_opt_float(_command_line_arguments,
"--epsilon", _minimum_epsilon);
324 else if (_command_line_arguments.find(
"--softmax") != std::string::npos)
333 else if (
find_opt_float(_command_line_arguments,
"--epsilon", _epsilon))
340 uint32_t check_sum_computed = mp.
checksum();
343 uint32_t check_sum_len;
345 if (check_sum_len !=
sizeof(uint32_t))
351 if (check_sum_computed != check_sum)
354 if (_command_line_arguments.find(
"--cb_adf") != std::string::npos)
367 uint64_t weight_length = (uint64_t)1 << _num_bits;
368 _stride_shift = (uint32_t)
ceil_log_2(num_weights);
374 _model_loaded =
true;
385 bool is_cb_explore_adf() {
return _command_line_arguments.find(
"--cb_explore_adf") != std::string::npos; }
394 bool is_csoaa_ldf() {
return _command_line_arguments.find(
"--csoaa_ldf") != std::string::npos; }
410 std::unique_ptr<namespace_copy_guard> ns_copy_guard;
419 score = GD::inline_predict<W>(*_weights,
false, _ignore_linear, _interactions,
false, ex);
433 out_scores.resize(num_actions);
436 for (
size_t i = 0; i < num_actions; i++, action++)
438 std::vector<std::unique_ptr<namespace_copy_guard>> ns_copy_guards;
444 auto ns_copy_guard = std::unique_ptr<namespace_copy_guard>(
new namespace_copy_guard(*action, ns));
447 for (
auto fs : shared.
feature_space[ns]) ns_copy_guard->feature_push_back(fs.value(), fs.index());
450 ns_copy_guards.push_back(std::move(ns_copy_guard));
460 std::vector<float>& pdf, std::vector<int>& ranking)
465 if (!is_cb_explore_adf())
468 std::vector<float> scores;
471 pdf.resize(num_actions);
472 ranking.resize(num_actions);
474 switch (_exploration)
483 auto top_action_iterator = std::min_element(std::begin(scores), std::end(scores));
484 uint32_t top_action = (uint32_t)(top_action_iterator - std::begin(scores));
497 _lambda, std::begin(scores), std::end(scores), std::begin(pdf), std::end(pdf)));
502 std::vector<uint32_t> top_actions(num_actions);
505 std::vector<std::unique_ptr<stride_shift_guard>> stride_shift_guards;
506 stride_shift_guards.push_back(
510 stride_shift_guards.push_back(
513 for (
size_t i = 0; i < _bag_size; i++)
515 std::vector<std::unique_ptr<feature_offset_guard>> feature_offset_guards;
517 feature_offset_guards.push_back(
522 auto top_action_iterator = std::min_element(std::begin(scores), std::end(scores));
523 uint32_t top_action = (uint32_t)(top_action_iterator - std::begin(scores));
525 top_actions[top_action]++;
532 if (_minimum_epsilon > 0)
543 std::begin(pdf), std::end(pdf), std::begin(scores), std::end(scores), std::begin(ranking), std::end(ranking)));
546 uint32_t chosen_action_idx;
551 if (chosen_action_idx != 0)
553 std::iter_swap(std::begin(ranking), std::begin(ranking) + chosen_action_idx);
554 std::iter_swap(std::begin(pdf), std::begin(pdf) + chosen_action_idx);
560 template <
typename PdfIt,
typename InputScoreIt,
typename OutputIt>
561 static int sort_by_scores(PdfIt pdf_first, PdfIt pdf_last, InputScoreIt scores_first, InputScoreIt scores_last,
562 OutputIt ranking_begin, OutputIt ranking_last)
564 const size_t pdf_size = pdf_last - pdf_first;
565 const size_t ranking_size = ranking_last - ranking_begin;
567 if (pdf_size != ranking_size)
571 std::iota(ranking_begin, ranking_last, 0);
576 using Iter =
typename CP::Iter;
577 using Loc =
typename CP::Loc;
578 const Iter begin_coll(ranking_begin, pdf_first);
579 const Iter end_coll(ranking_last, pdf_last);
580 std::sort(begin_coll, end_coll, [&scores_first](
const Loc& l,
const Loc& r) {
581 return scores_first[size_t(l._val1)] < scores_first[size_t(r._val1)];
#define E_VW_PREDICT_ERR_CB_EXPLORATION_MISSING
int generate_bag(InputIt top_actions_first, InputIt top_actions_last, OutputIt pdf_first, OutputIt pdf_last)
Generates an exploration distribution according to votes on actions.
v_array< namespace_index > indices
#define E_VW_PREDICT_ERR_NOT_A_CB_MODEL
#define RETURN_ON_FAIL(stmt)
static int sort_by_scores(PdfIt pdf_first, PdfIt pdf_last, InputScoreIt scores_first, InputScoreIt scores_last, OutputIt ranking_begin, OutputIt ranking_last)
int read_weights(std::unique_ptr< W > &weights, uint64_t weight_length)
#define E_VW_PREDICT_ERR_INVALID_MODEL_CHECK_SUM
Iter & operator+=(size_t n)
bool operator>(const Iter &rhs) const
location_value(const location_reference< It1, It2 > &rhs)
bool is_cb_explore_adf()
True if the model describes a contextual bandit (cb) model using action dependent features (afd) ...
int sample_after_normalizing(uint64_t seed, It pdf_first, It pdf_last, uint32_t &chosen_index)
Sample an index from the provided pdf. If the pdf is not normalized it will be updated in-place...
bool find_opt_int(std::string const &command_line_args, std::string arg_name, int &value)
Vowpal Wabbit slim predictor. Supports: regression, multi-class classification and contextual bandits...
friend void swap(const location_reference &a, const location_reference &b)
Iter operator+(const size_t n) const
bool operator!=(const Iter &rhs) const
int generate_softmax(float lambda, InputIt scores_first, InputIt scores_last, OutputIt pdf_first, OutputIt pdf_last)
Generates softmax style exploration distribution.
vw_predict_exploration
Exploration algorithm specified by the model.
typename std::iterator_traits< It2 >::value_type Val2
#define RETURN_EXPLORATION_ON_FAIL(stmt)
int predict(const char *event_id, example_predict &shared, example_predict *actions, size_t num_actions, std::vector< float > &pdf, std::vector< int > &ranking)
std::array< bool, NUM_NAMESPACES > _ignore_linear
bool is_csoaa_ldf()
True if the model describes a cost sensitive one-against-all (csoaa). This is also true for cb_explor...
size_t operator-(const Iter &rhs) const
std::array< features, NUM_NAMESPACES > feature_space
int generate_epsilon_greedy(float epsilon, uint32_t top_action, It pdf_first, It pdf_last)
Generates epsilon-greedy style exploration distribution.
int load(const char *model, size_t length)
Reads the Vowpal Wabbit model from the supplied buffer (produced using vw -f <modelname>) ...
int read(const char *field_name, size_t field_length, const char **ret)
bool operator<(const Iter &rhs) const
void find_opt(std::string const &command_line_args, std::string arg_name, std::vector< std::string > &out_values)
Iter & operator-=(size_t n)
bool operator>=(const Iter &rhs) const
location_reference(It1 first, It2 second)
constexpr uint64_t constant
bool operator<=(const Iter &rhs) const
#define E_VW_PREDICT_ERR_INVALID_MODEL
int enforce_minimum_probability(float minimum_uniform, bool update_zero_elements, It pdf_first, It pdf_last)
Updates the pdf to ensure each action is explored with at least minimum_uniform/num_actions.
collection_pair_iterator(It1 first, It2 second)
std::vector< std::string > _interactions
location_reference & operator=(const Val &rhs)
uint32_t feature_index_num_bits()
location_reference & operator=(const Ref &rhs)
bool operator==(typed_option< T > &lhs, typed_option< T > &rhs)
#define E_VW_PREDICT_ERR_NO_A_CSOAA_MODEL
location_reference(const Ref &other)
#define E_VW_PREDICT_ERR_HASH_SEED_NOT_SUPPORTED
#define E_VW_PREDICT_ERR_NO_MODEL_LOADED
vw_predict_exploration _exploration
int predict(example_predict &shared, example_predict *actions, size_t num_actions, std::vector< float > &out_scores)
bool operator==(const Iter &rhs) const
Iter operator-(const size_t n) const
void predict(bfgs &b, base_learner &, example &ec)
std::unique_ptr< W > _weights
uint32_t num_weights(vw &all)
constexpr unsigned char constant_namespace
typename std::iterator_traits< It1 >::value_type Val1
int predict(example_predict &ex, float &score)
Predicts a score (as in regression) for the provided example.
uint64_t ceil_log_2(uint64_t v)
int read_string(const char *field_name, std::string &s)
#define E_VW_PREDICT_ERR_GD_RESUME_NOT_SUPPORTED
bool find_opt_float(std::string const &command_line_args, std::string arg_name, float &value)
#define E_EXPLORATION_PDF_RANKING_SIZE_MISMATCH
std::string _command_line_arguments