SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
OnlineLinearMachine.cpp
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
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     SG_ADD(&bias, "bias", "Bias b.", MS_NOT_AVAILABLE);
00021     SG_ADD((CSGObject**) &features, "features",
00022         "Feature object.", MS_NOT_AVAILABLE);
00023 }
00024 
00025 COnlineLinearMachine::~COnlineLinearMachine()
00026 {
00027     // It is possible that a derived class may have already
00028     // called SG_FREE() on the weight vector
00029     if (w != NULL)
00030         SG_FREE(w);
00031     SG_UNREF(features);
00032 }
00033 
00034 CBinaryLabels* COnlineLinearMachine::apply_binary(CFeatures* data)
00035 {
00036     SGVector<float64_t> outputs = apply_get_outputs(data);
00037     return new CBinaryLabels(outputs);
00038 }
00039 
00040 CRegressionLabels* COnlineLinearMachine::apply_regression(CFeatures* data)
00041 {
00042     SGVector<float64_t> outputs = apply_get_outputs(data);
00043     return new CRegressionLabels(outputs);
00044 }
00045 
00046 SGVector<float64_t> COnlineLinearMachine::apply_get_outputs(CFeatures* data)
00047 {
00048     if (data)
00049     {
00050         if (!data->has_property(FP_STREAMING_DOT))
00051             SG_ERROR("Specified features are not of type CStreamingDotFeatures\n")
00052 
00053         set_features((CStreamingDotFeatures*) data);
00054     }
00055 
00056     ASSERT(features)
00057     ASSERT(features->has_property(FP_STREAMING_DOT))
00058 
00059     DynArray<float64_t>* labels_dynarray=new DynArray<float64_t>();
00060     int32_t num_labels=0;
00061 
00062     features->start_parser();
00063     while (features->get_next_example())
00064     {
00065         float64_t current_lab=features->dense_dot(w, w_dim) + bias;
00066 
00067         labels_dynarray->append_element(current_lab);
00068         num_labels++;
00069 
00070         features->release_example();
00071     }
00072     features->end_parser();
00073 
00074     SGVector<float64_t> labels_array(num_labels);
00075     for (int32_t i=0; i<num_labels; i++)
00076         labels_array.vector[i]=(*labels_dynarray)[i];
00077 
00078     delete labels_dynarray;
00079     return labels_array;
00080 }
00081 
00082 float32_t COnlineLinearMachine::apply_one(float32_t* vec, int32_t len)
00083 {
00084         return SGVector<float32_t>::dot(vec, w, len)+bias;
00085 }
00086 
00087 float32_t COnlineLinearMachine::apply_to_current_example()
00088 {
00089         return features->dense_dot(w, w_dim)+bias;
00090 }
00091 
00092 bool COnlineLinearMachine::train_machine(CFeatures *data)
00093 {
00094     if (data)
00095     {
00096         if (!data->has_property(FP_STREAMING_DOT))
00097             SG_ERROR("Specified features are not of type CStreamingDotFeatures\n")
00098         set_features((CStreamingDotFeatures*) data);
00099     }
00100     start_train();
00101     features->start_parser();
00102     while (features->get_next_example())
00103     {
00104         train_example(features, features->get_label());
00105         features->release_example();
00106     }
00107 
00108     features->end_parser();
00109     stop_train();
00110 
00111     return true;
00112 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation