LinearMachine.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/LinearMachine.h>
00012 #include <shogun/base/Parameter.h>
00013 
00014 using namespace shogun;
00015 
00016 CLinearMachine::CLinearMachine()
00017 : CMachine(), w_dim(0), w(NULL), bias(0), features(NULL)
00018 {
00019 
00020     m_parameters->add_vector(&w, &w_dim, "w", "Parameter vector w.");
00021     m_parameters->add(&bias, "bias", "Bias b.");
00022     m_parameters->add((CSGObject**) &features, "features", "Feature object.");
00023 
00024 }
00025 
00026 CLinearMachine::~CLinearMachine()
00027 {
00028     SG_FREE(w);
00029     SG_UNREF(features);
00030 }
00031 
00032 bool CLinearMachine::load(FILE* srcfile)
00033 {
00034     SG_SET_LOCALE_C;
00035     SG_RESET_LOCALE;
00036     return false;
00037 }
00038 
00039 bool CLinearMachine::save(FILE* dstfile)
00040 {
00041     SG_SET_LOCALE_C;
00042     SG_RESET_LOCALE;
00043     return false;
00044 }
00045 
00046 CLabels* CLinearMachine::apply()
00047 {
00048     if (!features)
00049         return NULL;
00050 
00051     int32_t num=features->get_num_vectors();
00052     ASSERT(num>0);
00053     ASSERT(w_dim==features->get_dim_feature_space());
00054 
00055     float64_t* out=SG_MALLOC(float64_t, num);
00056     features->dense_dot_range(out, 0, num, NULL, w, w_dim, bias);
00057 
00058     return new CLabels(SGVector<float64_t>(out,num));
00059 }
00060 
00061 CLabels* CLinearMachine::apply(CFeatures* data)
00062 {
00063     if (!data)
00064         SG_ERROR("No features specified\n");
00065     if (!data->has_property(FP_DOT))
00066         SG_ERROR("Specified features are not of type CDotFeatures\n");
00067     set_features((CDotFeatures*) data);
00068     return apply();
00069 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation