Skip to content

Commit

Permalink
Added VW python example, removed doc warning and added a missing sum_…
Browse files Browse the repository at this point in the history
…feat_sq init.
  • Loading branch information
frx committed Aug 24, 2011
1 parent 7e47856 commit 0c3aaf8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
26 changes: 26 additions & 0 deletions examples/undocumented/python_modular/streaming_vw_modular.py
@@ -0,0 +1,26 @@
from modshogun import StreamingVwFile
from modshogun import T_SVMLIGHT
from modshogun import StreamingVwFeatures
from modshogun import VowpalWabbit

def run_vw():
"""Runs the VW algorithm on a toy dataset in SVMLight format."""

# Open the input file as a StreamingVwFile
input_file = StreamingVwFile("../data/fm_train_sparsereal.dat")

# Tell VW that the file is in SVMLight format
# Supported types are T_DENSE, T_SVMLIGHT and T_VW
input_file.set_parser_type(T_SVMLIGHT)

# Create a StreamingVwFeatures object, `True' indicating the examples are labelled
features = StreamingVwFeatures(input_file, True, 1024)

# Create a VW object from the features
vw = VowpalWabbit(features)

# Train
vw.train()

if __name__ == "__main__":
run_vw()
2 changes: 2 additions & 0 deletions src/interfaces/modular/Classifier.i
Expand Up @@ -109,6 +109,8 @@
%include <shogun/classifier/vw/VowpalWabbit.h>
%include <shogun/classifier/svm/DomainAdaptationSVMLinear.h>

enum E_VW_PARSER_TYPE { T_VW, T_SVMLIGHT, T_DENSE };

#ifdef HAVE_PYTHON
%pythoncode %{
class SVM(CSVM):
Expand Down
21 changes: 13 additions & 8 deletions src/shogun/classifier/vw/VowpalWabbit.cpp
Expand Up @@ -135,18 +135,22 @@ bool CVowpalWabbit::train_machine(CFeatures* feat)
{
example = features->get_example();

if (example->pass != current_pass)
// Check if we shouldn't train (generally used for cache creation)
if (!no_training)
{
env->eta *= env->eta_decay_rate;
current_pass = example->pass;
}
if (example->pass != current_pass)
{
env->eta *= env->eta_decay_rate;
current_pass = example->pass;
}

predict_and_finalize(example);
predict_and_finalize(example);

learner->train(example, example->eta_round);
example->eta_round = 0.;
learner->train(example, example->eta_round);
example->eta_round = 0.;

output_example(example);
output_example(example);
}

features->release_example();
}
Expand Down Expand Up @@ -216,6 +220,7 @@ void CVowpalWabbit::init(CStreamingVwFeatures* feat)
SG_REF(reg);

quiet = false;
no_training = false;
dump_interval = exp(1.);
reg_name = NULL;
reg_dump_text = true;
Expand Down
13 changes: 13 additions & 0 deletions src/shogun/classifier/vw/VowpalWabbit.h
Expand Up @@ -62,6 +62,16 @@ class CVowpalWabbit: public COnlineLinearMachine
*/
void reinitialize_weights();

/**
* Set whether one desires to not train and only
* make passes over all examples instead.
*
* This is useful if one wants to create a cache file from data.
*
* @param dont_train true if one doesn't want to train
*/
void set_no_training(bool dont_train) { no_training = dont_train; }

/**
* Set whether learning is adaptive or not
*
Expand Down Expand Up @@ -265,6 +275,9 @@ class CVowpalWabbit: public COnlineLinearMachine
/// Whether to display statistics or not
bool quiet;

/// Whether we should just run over examples without training
bool no_training;

/// Multiplication factor for number of examples to dump after
float32_t dump_interval;

Expand Down
7 changes: 5 additions & 2 deletions src/shogun/classifier/vw/cache/VwNativeCacheReader.h
Expand Up @@ -21,13 +21,16 @@
namespace shogun
{

/// Packed structure for efficient storage
#ifndef DOXYGEN_SHOULD_SKIP_THIS
// Packed structure for efficient storage
struct one_float
{
/// The float to store
// The float to store
float32_t f;
} __attribute__((packed));

#endif // DOXYGEN_SHOULD_SKIP_THIS

/** @brief Class CVwNativeCacheReader reads from a cache exactly as
* that which has been produced by VW's default cache format.
*
Expand Down
1 change: 1 addition & 0 deletions src/shogun/features/StreamingVwFeatures.cpp
Expand Up @@ -98,6 +98,7 @@ void CStreamingVwFeatures::setup_example(VwExample* ae)
VwFeature temp = {1,constant_hash & env->mask};
ae->indices.push(constant_namespace);
ae->atomics[constant_namespace].push(temp);
ae->sum_feat_sq[constant_namespace] = 0;

if(env->stride != 1)
{
Expand Down

0 comments on commit 0c3aaf8

Please sign in to comment.