34 static void func(
weight& w,
float& initial, uint64_t ) { w = initial; }
57 static float x1 = 0.0;
58 static float x2 = 0.0;
59 static float temp = 0.0;
64 temp = x1 * x1 + x2 * x2;
65 }
while ((temp >= 1.0) || (temp == 0.0));
66 temp = sqrtf((-2.0
f * logf(temp)) / temp);
76 std::for_each(weights.begin(), weights.end(), [](
float& v) {
77 if (std::fabs(v) > sd * 2)
79 v = (float)std::remainder(static_cast<double>(v), sd * 2);
87 static int my_size = 0;
88 std::for_each(weights.begin(), weights.end(), [](
float ) { my_size += 1; });
90 double mean = sum / my_size;
91 std::vector<double> diff(my_size);
92 std::transform(weights.begin(), weights.end(), diff.begin(), [mean](
double x) {
return x - mean; });
93 double sq_sum = inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
94 return std::sqrt(sq_sum / my_size);
101 if (weights.not_null())
103 size_t length = ((size_t)1) << all.
num_bits;
106 uint32_t ss = weights.stride_shift();
108 new (&weights) T(length, ss);
112 THROW(
" Failed to allocate weight array with " << all.
num_bits <<
" bits: try decreasing -b <bits>");
114 if (weights.mask() == 0)
116 THROW(
" Failed to allocate weight array with " << all.
num_bits <<
" bits: try decreasing -b <bits>");
119 weights.template set_default<float, set_initial_wrapper<T> >(all.
initial_weight);
121 weights.template set_default<random_positive_wrapper<T> >();
123 weights.template set_default<random_weights_wrapper<T> >();
126 weights.template set_default<polar_normal_weights_wrapper<T> >();
130 weights.template set_default<polar_normal_weights_wrapper<T> >();
148 if (__dest_size < __n)
150 if ((new_dest = (
char*)realloc(__dest, __n)) == NULL)
151 THROW(
"Can't realloc enough memory.")
162 inline void safe_memcpy(
char*& __dest,
size_t& __dest_size,
const void* __src,
size_t __n)
165 memcpy(__dest, __src, __n);
179 size_t bytes_read_write = 0;
182 std::stringstream msg;
187 v_length = buf2_size;
199 v_length = all.
id.length() + 1;
201 msg <<
"Id " << all.
id <<
"\n";
210 file_options +=
" --id";
211 file_options +=
" " + all.
id;
228 msg <<
"bits:" << all.
num_bits <<
"\n";
229 uint32_t local_num_bits = all.
num_bits;
231 model_file, (
char*)&local_num_bits,
sizeof(local_num_bits),
"", read, msg, text);
235 file_options +=
" --bit_precision";
236 std::stringstream temp;
237 temp << local_num_bits;
238 file_options +=
" " + temp.str();
251 uint32_t pair_len = (uint32_t)all.
pairs.size();
253 msg << pair_len <<
" pairs: ";
258 for (
size_t i = 0; i < pair_len; i++)
260 char pair[3] = {0, 0, 0};
264 memcpy(pair, all.
pairs[i].c_str(), 2);
265 msg << all.
pairs[i] <<
" ";
271 std::string temp(pair);
272 if (count(all.
pairs.begin(), all.
pairs.end(), temp) == 0)
273 all.
pairs.push_back(temp);
280 uint32_t triple_len = (uint32_t)all.
triples.size();
282 msg << triple_len <<
" triples: ";
284 model_file, (
char*)&triple_len,
sizeof(triple_len),
"", read, msg, text);
287 for (
size_t i = 0; i < triple_len; i++)
289 char triple[4] = {0, 0, 0, 0};
294 memcpy(triple, all.
triples[i].c_str(), 3);
299 std::string temp(triple);
314 msg << len <<
" interactions: ";
318 for (
size_t i = 0; i < len; i++)
320 uint32_t inter_len = 0;
324 msg <<
"len: " << inter_len <<
" ";
327 model_file, (
char*)&inter_len,
sizeof(inter_len),
"", read, msg, text);
332 msg <<
"interaction: ";
340 std::string temp(buff2, inter_len);
360 msg <<
"rank:" << rank <<
"\n";
367 file_options +=
" --rank";
368 std::stringstream temp;
370 file_options +=
" " + temp.str();
373 all.
trace_message <<
"WARNING: this model file contains 'rank: " << rank
374 <<
"' value but it will be ignored as another value specified via the command line." 379 msg <<
"lda:" << all.
lda <<
"\n";
385 msg << ngram_len <<
" ngram:";
388 for (
size_t i = 0; i < ngram_len; i++)
391 char ngram[4] = {0, 0, 0, 0};
400 std::string temp(ngram);
403 file_options +=
" --ngram";
404 file_options +=
" " + temp;
413 msg << skip_len <<
" skip:";
417 for (
size_t i = 0; i < skip_len; i++)
419 char skip[4] = {0, 0, 0, 0};
429 std::string temp(skip);
432 file_options +=
" --skips";
433 file_options +=
" " + temp;
442 size_t ret = model_file.
bin_read_fixed((
char*)&len,
sizeof(len),
"");
443 if (len > 104857600 || ret <
sizeof(uint32_t))
444 THROW(
"bad model format!");
446 bytes_read_write += model_file.
bin_read_fixed(buff2, len,
"") + ret;
459 file_options = file_options +
" " + buff2;
467 if (option->m_keep && options.
was_supplied(option->m_name))
469 serializer.
add(*option);
473 auto serialized_keep_options = serializer.
str();
478 serialized_keep_options +=
" --random_seed";
482 msg <<
"options:" << serialized_keep_options <<
"\n";
484 uint32_t len = (uint32_t)serialized_keep_options.length();
486 safe_memcpy(buff2, buf2_size, serialized_keep_options.c_str(), len + 1);
489 "", read, msg, text);
499 uint32_t check_sum_saved = check_sum;
501 msg <<
"Checksum: " << check_sum <<
"\n";
502 bin_text_read_write(model_file, (
char*)&check_sum,
sizeof(check_sum),
"", read, msg, text);
504 if (check_sum_saved != check_sum)
505 THROW(
"Checksum is inconsistent, file is possibly corrupted.");
527 if (all.
l !=
nullptr)
536 if (reg_name == std::string(
""))
538 std::string start_name = reg_name + std::string(
".writing");
545 remove(reg_name.c_str());
547 if (0 != rename(start_name.c_str(), reg_name.c_str()))
548 THROW(
"WARN: dump_regressor(vw& all, std::string reg_name, bool as_text): cannot rename: " 549 << start_name.c_str() <<
" to " << reg_name.c_str());
554 std::stringstream filename;
555 filename << reg_name;
557 filename <<
"." << current_pass;
583 if (all_intial.size() > 0)
589 if (all_intial.size() > 1)
591 all.
trace_message <<
"warning: ignoring remaining " << (all_intial.size() - 1) <<
" initial regressors" 601 std::string file_options;
602 if (!feature_mask.empty())
604 if (initial_regressors.size() > 0)
606 if (feature_mask == initial_regressors[0])
620 if (initial_regressors.size() > 0)
std::vector< std::string > skip_strings
void save_load(io_buf &io, const bool read, const bool text)
#define VERSION_FILE_WITH_HEADER_ID
#define VERSION_FILE_WITH_INTERACTIONS_IN_FO
virtual std::string str() override
void accumulate(vw &all, parameters &weights, size_t offset)
static void func(weight &w, float &initial, uint64_t)
void initialize_regressor(vw &all, T &weights)
std::vector< std::string > pairs
void truncate(vw &all, T &weights)
VW::config::options_i * options
static constexpr int WRITE
virtual bool close_file()
std::vector< std::string > ngram_strings
bool random_positive_weights
#define VERSION_FILE_WITH_RANK_IN_HEADER
std::string inv_hash_regressor_name
#define VERSION_FILE_WITH_INTERACTIONS
size_t bin_text_read_write_fixed_validated(io_buf &io, char *data, size_t len, const char *read_message, bool read, std::stringstream &msg, bool text)
VW_STD14_CONSTEXPR uint64_t uniform_hash(const void *key, size_t len, uint64_t seed)
size_t bin_text_read_write(io_buf &io, char *data, size_t len, const char *read_message, bool read, std::stringstream &msg, bool text)
void finalize_regressor(vw &all, std::string reg_name)
float merand48(uint64_t &initial)
#define VERSION_FILE_WITH_HEADER_CHAINED_HASH
void validate_version(vw &all)
const version_struct version(PACKAGE_VERSION)
static constexpr int READ
void save_predictor(vw &all, std::string reg_name, size_t current_pass)
virtual std::vector< std::shared_ptr< base_option > > get_all_options()=0
std::shared_ptr< rand_state > get_random_state()
virtual int open_file(const char *name, bool stdin_off)
size_t bin_read_fixed(char *data, size_t len, const char *read_message)
void safe_memcpy(char *&__dest, size_t &__dest_size, const void *__src, size_t __n)
VW::version_struct model_file_ver
static void func(weight &w, uint64_t index)
virtual bool was_supplied(const std::string &key)=0
std::string per_feature_regularizer_output
void save_load_header(vw &all, io_buf &model_file, bool read, bool text, std::string &file_options, VW::config::options_i &options)
#define VERSION_FILE_WITH_HEADER_HASH
std::string to_string() const
dense_parameters dense_weights
void verify_hash(bool verify)
virtual void add(base_option &option) override
void parse_mask_regressor_args(vw &all, std::string feature_mask, std::vector< std::string > initial_regressors)
void set_zero(size_t offset)
bool resize_buf_if_needed(char *&__dest, size_t &__dest_size, const size_t __n)
double calculate_sd(vw &, T &weights)
std::string per_feature_regularizer_text
std::vector< std::string > triples
sparse_parameters sparse_weights
void read_regressor_file(vw &all, std::vector< std::string > all_intial, io_buf &io_temp)
std::vector< std::string > interactions
LEARNER::base_learner * l
constexpr size_t default_buf_size
void dump_regressor(vw &all, io_buf &buf, bool as_text)
void validate_num_bits(vw &all)
static void func(weight &w, uint64_t index)
const char * to_string(prediction_type_t prediction_type)
std::string text_regressor_name
void validate_default_bits(vw &all, uint32_t local_num_bits)
static void func(weight &w, uint64_t index)