SHOGUN
v3.2.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/LinearMachine.h> 00012 #include <shogun/labels/RegressionLabels.h> 00013 #include <shogun/base/Parameter.h> 00014 00015 using namespace shogun; 00016 00017 CLinearMachine::CLinearMachine() 00018 : CMachine(), bias(0), features(NULL) 00019 { 00020 init(); 00021 } 00022 00023 CLinearMachine::CLinearMachine(CLinearMachine* machine) : CMachine(), 00024 bias(0), features(NULL) 00025 { 00026 set_w(machine->get_w().clone()); 00027 set_bias(machine->get_bias()); 00028 00029 init(); 00030 } 00031 00032 void CLinearMachine::init() 00033 { 00034 SG_ADD(&w, "w", "Parameter vector w.", MS_NOT_AVAILABLE); 00035 SG_ADD(&bias, "bias", "Bias b.", MS_NOT_AVAILABLE); 00036 SG_ADD((CSGObject**) &features, "features", "Feature object.", 00037 MS_NOT_AVAILABLE); 00038 } 00039 00040 00041 CLinearMachine::~CLinearMachine() 00042 { 00043 SG_UNREF(features); 00044 } 00045 00046 float64_t CLinearMachine::apply_one(int32_t vec_idx) 00047 { 00048 return features->dense_dot(vec_idx, w.vector, w.vlen) + bias; 00049 } 00050 00051 CRegressionLabels* CLinearMachine::apply_regression(CFeatures* data) 00052 { 00053 SGVector<float64_t> outputs = apply_get_outputs(data); 00054 return new CRegressionLabels(outputs); 00055 } 00056 00057 CBinaryLabels* CLinearMachine::apply_binary(CFeatures* data) 00058 { 00059 SGVector<float64_t> outputs = apply_get_outputs(data); 00060 return new CBinaryLabels(outputs); 00061 } 00062 00063 SGVector<float64_t> CLinearMachine::apply_get_outputs(CFeatures* data) 00064 { 00065 if (data) 00066 { 00067 if (!data->has_property(FP_DOT)) 00068 SG_ERROR("Specified features are not of type CDotFeatures\n") 00069 00070 set_features((CDotFeatures*) data); 00071 } 00072 00073 if (!features) 00074 return SGVector<float64_t>(); 00075 00076 int32_t num=features->get_num_vectors(); 00077 ASSERT(num>0) 00078 ASSERT(w.vlen==features->get_dim_feature_space()) 00079 00080 float64_t* out=SG_MALLOC(float64_t, num); 00081 features->dense_dot_range(out, 0, num, NULL, w.vector, w.vlen, bias); 00082 return SGVector<float64_t>(out,num); 00083 } 00084 00085 SGVector<float64_t> CLinearMachine::get_w() const 00086 { 00087 return w; 00088 } 00089 00090 void CLinearMachine::set_w(const SGVector<float64_t> src_w) 00091 { 00092 w=src_w; 00093 } 00094 00095 void CLinearMachine::set_bias(float64_t b) 00096 { 00097 bias=b; 00098 } 00099 00100 float64_t CLinearMachine::get_bias() 00101 { 00102 return bias; 00103 } 00104 00105 void CLinearMachine::set_features(CDotFeatures* feat) 00106 { 00107 SG_REF(feat); 00108 SG_UNREF(features); 00109 features=feat; 00110 } 00111 00112 CDotFeatures* CLinearMachine::get_features() 00113 { 00114 SG_REF(features); 00115 return features; 00116 } 00117 00118 void CLinearMachine::store_model_features() 00119 { 00120 } 00121