Vowpal Wabbit
cb_explore_adf_softmax.cc
Go to the documentation of this file.
2 #include "reductions.h"
3 #include "cb_adf.h"
4 #include "rand48.h"
5 #include "bs.h"
6 #include "gen_cs_example.h"
7 #include "cb_explore.h"
8 #include "explore.h"
9 #include <vector>
10 #include <algorithm>
11 #include <cmath>
12 
13 namespace VW
14 {
15 namespace cb_explore_adf
16 {
17 namespace softmax
18 {
20 {
21  private:
22  float _epsilon;
23  float _lambda;
24 
25  public:
26  cb_explore_adf_softmax(float epsilon, float lambda);
27  ~cb_explore_adf_softmax() = default;
28 
29  // Should be called through cb_explore_adf_base for pre/post-processing
30  void predict(LEARNER::multi_learner& base, multi_ex& examples) { predict_or_learn_impl<false>(base, examples); }
31  void learn(LEARNER::multi_learner& base, multi_ex& examples) { predict_or_learn_impl<true>(base, examples); }
32 
33  private:
34  template <bool is_learn>
36 };
37 
38 cb_explore_adf_softmax::cb_explore_adf_softmax(float epsilon, float lambda) : _epsilon(epsilon), _lambda(lambda) {}
39 
40 template <bool is_learn>
42 {
43  LEARNER::multiline_learn_or_predict<is_learn>(base, examples, examples[0]->ft_offset);
44 
45  v_array<ACTION_SCORE::action_score>& preds = examples[0]->pred.a_s;
47  -_lambda, begin_scores(preds), end_scores(preds), begin_scores(preds), end_scores(preds));
48 
50 }
51 
53 {
54  using config::make_option;
55  bool cb_explore_adf_option = false;
56  bool softmax = false;
57  float epsilon = 0.;
58  float lambda = 0.;
59  config::option_group_definition new_options("Contextual Bandit Exploration with Action Dependent Features");
60  new_options
61  .add(make_option("cb_explore_adf", cb_explore_adf_option)
62  .keep()
63  .help("Online explore-exploit for a contextual bandit problem with multiline action dependent features"))
64  .add(make_option("epsilon", epsilon).keep().help("epsilon-greedy exploration"))
65  .add(make_option("softmax", softmax).keep().help("softmax exploration"))
66  .add(make_option("lambda", lambda).keep().default_value(1.f).help("parameter for softmax"));
67  options.add_and_parse(new_options);
68 
69  if (!cb_explore_adf_option || !softmax)
70  return nullptr;
71 
72  if (lambda < 0) // Lambda should always be positive because we are using a cost basis.
73  lambda = -lambda;
74 
75  // Ensure serialization of cb_adf in all cases.
76  if (!options.was_supplied("cb_adf"))
77  {
78  options.insert("cb_adf", "");
79  }
80 
82 
83  // Set explore_type
84  size_t problem_multiplier = 1;
85 
86  LEARNER::multi_learner* base = as_multiline(setup_base(options, all));
87  all.p->lp = CB::cb_label;
89 
91  auto data = scoped_calloc_or_throw<explore_type>(epsilon, lambda);
94 
95  l.set_finish_example(explore_type::finish_multiline_example);
96  return make_base(l);
97 }
98 } // namespace softmax
99 } // namespace cb_explore_adf
100 } // namespace VW
void(* delete_prediction)(void *)
Definition: global_data.h:485
void finish_multiline_example(vw &all, cbify &, multi_ex &ec_seq)
Definition: cbify.cc:373
label_type::label_type_t label_type
Definition: global_data.h:550
base_learner * make_base(learner< T, E > &base)
Definition: learner.h:462
virtual void add_and_parse(const option_group_definition &group)=0
int generate_softmax(float lambda, InputIt scores_first, InputIt scores_last, OutputIt pdf_first, OutputIt pdf_last)
Generates softmax style exploration distribution.
score_iterator begin_scores(action_scores &a_s)
Definition: action_score.h:43
parser * p
Definition: global_data.h:377
score_iterator end_scores(action_scores &a_s)
Definition: action_score.h:45
learner< T, E > & init_learner(free_ptr< T > &dat, L *base, void(*learn)(T &, L &, E &), void(*predict)(T &, L &, E &), size_t ws, prediction_type::prediction_type_t pred_type)
Definition: learner.h:369
void delete_action_scores(void *v)
Definition: action_score.cc:29
virtual bool was_supplied(const std::string &key)=0
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.
virtual void insert(const std::string &key, const std::string &value)=0
LEARNER::base_learner * setup(VW::config::options_i &options, vw &all)
option_group_definition & add(T &&op)
Definition: options.h:90
std::vector< example * > multi_ex
Definition: example.h:122
label_parser cb_label
Definition: cb.cc:167
void learn(LEARNER::multi_learner &base, multi_ex &examples)
typed_option< T > make_option(std::string name, T &location)
Definition: options.h:80
Definition: autolink.cc:11
void predict_or_learn_impl(LEARNER::multi_learner &base, multi_ex &examples)
void predict(LEARNER::multi_learner &base, multi_ex &examples)
LEARNER::base_learner * setup_base(options_i &options, vw &all)
Definition: parse_args.cc:1222
void predict(bfgs &b, base_learner &, example &ec)
Definition: bfgs.cc:956
void learn(bfgs &b, base_learner &base, example &ec)
Definition: bfgs.cc:965
float f
Definition: cache.cc:40
multi_learner * as_multiline(learner< T, E > *l)
Definition: learner.h:468
label_parser lp
Definition: parser.h:102