LinearStructuredOutputMachine.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) 2012 Fernando José Iglesias García
00008  * Copyright (C) 2012 Fernando José Iglesias García
00009  */
00010 
00011 #include <shogun/machine/LinearStructuredOutputMachine.h>
00012 #include <shogun/structure/MulticlassSOLabels.h>
00013 
00014 using namespace shogun;
00015 
00016 CLinearStructuredOutputMachine::CLinearStructuredOutputMachine()
00017 : CStructuredOutputMachine()
00018 {
00019     register_parameters();
00020 }
00021 
00022 CLinearStructuredOutputMachine::CLinearStructuredOutputMachine(
00023         CStructuredModel*  model, 
00024         CLossFunction*     loss, 
00025         CStructuredLabels* labs)
00026 : CStructuredOutputMachine(model, loss, labs)
00027 {
00028     register_parameters();
00029 }
00030 
00031 CLinearStructuredOutputMachine::~CLinearStructuredOutputMachine()
00032 {
00033 }
00034 
00035 void CLinearStructuredOutputMachine::set_features(CFeatures* f)
00036 {
00037     m_model->set_features(f);
00038 }
00039 
00040 CFeatures* CLinearStructuredOutputMachine::get_features() const
00041 {
00042     return m_model->get_features();
00043 }
00044 
00045 SGVector< float64_t > CLinearStructuredOutputMachine::get_w() const
00046 {
00047     return m_w;
00048 }
00049 
00050 CStructuredLabels* CLinearStructuredOutputMachine::apply_structured(CFeatures* data)
00051 {
00052     if (data)
00053     {
00054         set_features(data);
00055     }
00056 
00057     CStructuredLabels* out;
00058     CFeatures* model_features = this->get_features();
00059     if (!model_features)
00060     {
00061         out = new CStructuredLabels();
00062     }
00063     else
00064     {
00065         out = new CStructuredLabels(model_features->get_num_vectors());
00066         for ( int32_t i = 0 ; i < model_features->get_num_vectors() ; ++i )
00067         {
00068             CResultSet* result = m_model->argmax(m_w, i, false);
00069             out->add_label(result->argmax);
00070 
00071             SG_UNREF(result);
00072         }
00073     }
00074     SG_UNREF(model_features);
00075     return out;
00076 }
00077 
00078 void CLinearStructuredOutputMachine::register_parameters()
00079 {
00080     SG_ADD(&m_w, "m_w", "Weight vector", MS_NOT_AVAILABLE);
00081 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation