SHOGUN
v2.0.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 }