SHOGUN
v1.1.0
|
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 m_parameters->add(&bias, "bias", "Bias b."); 00021 m_parameters->add((CSGObject**) &features, "features", "Feature object."); 00022 } 00023 00024 COnlineLinearMachine::~COnlineLinearMachine() 00025 { 00026 // It is possible that a derived class may have already 00027 // called SG_FREE() on the weight vector 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 }