Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <shogun/machine/OnlineLinearMachine.h>
00012 #include <shogun/base/Parameter.h>
00013
00014 using namespace shogun;
00015
00016 COnlineLinearMachine::COnlineLinearMachine()
00017 : CMachine(), w_dim(0), w(NULL), bias(0), features(NULL)
00018 {
00019 m_parameters->add_vector(&w, &w_dim, "w", "Parameter vector w.");
00020 m_parameters->add(&bias, "bias", "Bias b.");
00021 m_parameters->add((CSGObject**) &features, "features", "Feature object.");
00022 }
00023
00024 COnlineLinearMachine::~COnlineLinearMachine()
00025 {
00026
00027
00028 if (w != NULL)
00029 SG_FREE(w);
00030 SG_UNREF(features);
00031 }
00032
00033 bool COnlineLinearMachine::load(FILE* srcfile)
00034 {
00035 SG_SET_LOCALE_C;
00036 SG_RESET_LOCALE;
00037 return false;
00038 }
00039
00040 bool COnlineLinearMachine::save(FILE* dstfile)
00041 {
00042 SG_SET_LOCALE_C;
00043 SG_RESET_LOCALE;
00044 return false;
00045 }
00046
00047 CLabels* COnlineLinearMachine::apply()
00048 {
00049 ASSERT(features);
00050 ASSERT(features->has_property(FP_STREAMING_DOT));
00051
00052 DynArray<float64_t>* labels_dynarray=new DynArray<float64_t>();
00053 int32_t num_labels=0;
00054
00055 features->start_parser();
00056 while (features->get_next_example())
00057 {
00058 float64_t current_lab=features->dense_dot(w, w_dim) + bias;
00059
00060 labels_dynarray->append_element(current_lab);
00061 num_labels++;
00062
00063 features->release_example();
00064 }
00065 features->end_parser();
00066
00067 SGVector<float64_t> labels_array(num_labels);
00068 for (int32_t i=0; i<num_labels; i++)
00069 labels_array.vector[i]=(*labels_dynarray)[i];
00070
00071 return new CLabels(labels_array);
00072 }
00073
00074 CLabels* COnlineLinearMachine::apply(CFeatures* data)
00075 {
00076 if (!data)
00077 SG_ERROR("No features specified\n");
00078 if (!data->has_property(FP_STREAMING_DOT))
00079 SG_ERROR("Specified features are not of type CStreamingDotFeatures\n");
00080 set_features((CStreamingDotFeatures*) data);
00081 return apply();
00082 }
00083
00084 float32_t COnlineLinearMachine::apply(float32_t* vec, int32_t len)
00085 {
00086 return CMath::dot(vec, w, len)+bias;
00087 }
00088
00089 float32_t COnlineLinearMachine::apply_to_current_example()
00090 {
00091 return features->dense_dot(w, w_dim)+bias;
00092 }