forked from jackgerrits/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vw.h example construction helpers first version
- Loading branch information
ataymano@microsoft.com
authored and
ataymano@microsoft.com
committed
Jun 4, 2019
1 parent
dfe5345
commit b9c334a
Showing
10 changed files
with
224 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef STATIC_LINK_VW | ||
#define BOOST_TEST_DYN_LINK | ||
#endif | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
#include "vw.h" | ||
|
||
template <class T> | ||
void check_weights_equal(T& first, T& second) | ||
{ | ||
auto secondIt = second.begin(); | ||
for (auto firstIt : first) | ||
{ | ||
BOOST_CHECK_EQUAL(firstIt, *secondIt); | ||
++secondIt; | ||
} | ||
BOOST_CHECK_EQUAL(secondIt == second.end(), true); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(parsed_and_constructed_example_parity) | ||
{ | ||
vw* vw1 = VW::initialize("-q st --noconstant --quiet"); | ||
vw* vw2 = VW::initialize("-q st --noconstant --quiet"); | ||
|
||
auto example_parsed = VW::read_example(*vw1, "1 |s p^the_man w^the w^man |t p^un_homme w^un w^homme"); | ||
auto fs = VW::feature_space(*vw2, 2); | ||
fs[0].reset(3); | ||
fs[0].set_name("s"); | ||
fs[0].set(0, "p^the_man", 1.0f); | ||
fs[0].set(1, "w^the", 1.0f); | ||
fs[0].set(2, "w^man", 1.0f); | ||
|
||
fs[1].reset(3); | ||
fs[1].set_name("t"); | ||
fs[1].set(0, "p^un_homme", 1.0f); | ||
fs[1].set(1, "w^un", 1.0f); | ||
fs[1].set(2, "w^homme", 1.0f); | ||
auto example_constructed = VW::import_example(*vw2, "1", fs); | ||
|
||
vw1->learn(*example_parsed); | ||
vw2->learn(*example_constructed); | ||
|
||
BOOST_CHECK_EQUAL(vw1->weights.sparse, vw2->weights.sparse); | ||
|
||
if (vw1->weights.sparse) | ||
{ | ||
check_weights_equal(vw1->weights.sparse_weights, vw2->weights.sparse_weights); | ||
} | ||
else | ||
{ | ||
check_weights_equal(vw1->weights.dense_weights, vw2->weights.dense_weights); | ||
} | ||
|
||
VW::finish(*vw1); | ||
VW::finish(*vw2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "global_data.h" | ||
#include "vw.h" | ||
|
||
#include <string> | ||
|
||
namespace VW { | ||
primitive_feature_space::primitive_feature_space(::vw& _all) : all(_all) {} | ||
|
||
primitive_feature_space::primitive_feature_space(::vw& _all, const std::string& _name, size_t _len) | ||
: all(_all), name(_name[0]), hash(hash_space(all, _name)), fs(_len) | ||
{ | ||
} | ||
|
||
primitive_feature_space::primitive_feature_space( | ||
::vw& _all, const std::string& _name, std::initializer_list<feature> features) | ||
: primitive_feature_space(_all, _name, features.size()) | ||
{ | ||
size_t i = 0; | ||
for (const auto& f : features) { | ||
fs[i] = f; | ||
} | ||
} | ||
|
||
primitive_feature_space::primitive_feature_space( | ||
::vw& _all, const std::string& _name, std::initializer_list<std::tuple<const char*, float>> features) | ||
: primitive_feature_space(_all, _name, features.size()) | ||
{ | ||
size_t i = 0; | ||
for (const auto& f : features) | ||
{ | ||
set(i, std::get<0>(f), std::get<1>(f)); | ||
} | ||
} | ||
|
||
void primitive_feature_space::reset(size_t _len) { fs.resize(_len); } | ||
|
||
const feature& primitive_feature_space::operator[](size_t index) const { return fs[index]; } | ||
feature& primitive_feature_space::operator[](size_t index) { return fs[index]; } | ||
|
||
void primitive_feature_space::set(size_t index, const std::string& feature_name, float value) | ||
{ | ||
fs[index].weight_index = hash_feature(all, feature_name, hash); | ||
fs[index].x = value; | ||
} | ||
|
||
void primitive_feature_space::set_name(const std::string& _name) | ||
{ | ||
name = _name[0]; | ||
hash = hash_space(all, _name); | ||
} | ||
unsigned char primitive_feature_space::get_name() const { return name; } | ||
size_t primitive_feature_space::size() const { return fs.size(); } | ||
|
||
feature_space::feature_space(vw& _all, size_t _size) : all(_all), fspaces(_size, all) {} | ||
|
||
const primitive_feature_space& feature_space::operator[](size_t index) const { return fspaces[index]; } | ||
primitive_feature_space& feature_space::operator[](size_t index) { return fspaces[index]; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.