CombinedDotFeatures.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) 2009 Soeren Sonnenburg
00008  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "features/CombinedDotFeatures.h"
00012 #include "lib/io.h"
00013 #include "lib/Mathematics.h"
00014 
00015 using namespace shogun;
00016 
00017 CCombinedDotFeatures::CCombinedDotFeatures() : CDotFeatures()
00018 {
00019     feature_list=new CList<CDotFeatures*>(true);
00020     update_dim_feature_space_and_num_vec();
00021 }
00022 
00023 CCombinedDotFeatures::CCombinedDotFeatures(const CCombinedDotFeatures & orig)
00024 : CDotFeatures(orig), num_vectors(orig.num_vectors),
00025     num_dimensions(orig.num_dimensions)
00026 {
00027 }
00028 
00029 CFeatures* CCombinedDotFeatures::duplicate() const
00030 {
00031     return new CCombinedDotFeatures(*this);
00032 }
00033 
00034 CCombinedDotFeatures::~CCombinedDotFeatures()
00035 {
00036     delete feature_list;
00037 }
00038 
00039 void CCombinedDotFeatures::list_feature_objs()
00040 {
00041     SG_INFO( "BEGIN COMBINED DOTFEATURES LIST (%d, %d) - ", num_vectors, num_dimensions);
00042     this->list_feature_obj();
00043 
00044     CListElement<CDotFeatures*> * current = NULL ;
00045     CDotFeatures* f=get_first_feature_obj(current);
00046 
00047     while (f)
00048     {
00049         f->list_feature_obj();
00050         f=get_next_feature_obj(current);
00051     }
00052 
00053     SG_INFO( "END COMBINED DOTFEATURES LIST (%d, %d) - ", num_vectors, num_dimensions);
00054     this->list_feature_obj();
00055 }
00056 
00057 void CCombinedDotFeatures::update_dim_feature_space_and_num_vec()
00058 {
00059     CListElement<CDotFeatures*> * current = NULL ;
00060     CDotFeatures* f=get_first_feature_obj(current);
00061 
00062     int32_t dim=0;
00063     int32_t vec=-1;
00064 
00065     while (f)
00066     {
00067         dim+= f->get_dim_feature_space();
00068         if (vec==-1)
00069             vec=f->get_num_vectors();
00070         else if (vec != f->get_num_vectors())
00071         {
00072             f->list_feature_obj();
00073             SG_ERROR("Number of vectors (%d) mismatches in above feature obj (%d)\n", vec, f->get_num_vectors());
00074         }
00075 
00076         f=get_next_feature_obj(current);
00077     }
00078 
00079     num_dimensions=dim;
00080     num_vectors=vec;
00081     SG_DEBUG("vecs=%d, dims=%d\n", num_vectors, num_dimensions);
00082 }
00083 
00084 float64_t CCombinedDotFeatures::dot(int32_t vec_idx1, int32_t vec_idx2)
00085 {
00086     float64_t result=0;
00087 
00088     CListElement<CDotFeatures*> * current = NULL ;
00089     CDotFeatures* f=get_first_feature_obj(current);
00090 
00091     while (f)
00092     {
00093         result += f->dot(vec_idx1, vec_idx2)*CMath::sq(f->get_combined_feature_weight());
00094         f=get_next_feature_obj(current);
00095     }
00096 
00097     return result;
00098 }
00099 
00100 float64_t CCombinedDotFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
00101 {
00102     float64_t result=0;
00103 
00104     CListElement<CDotFeatures*> * current = NULL ;
00105     CDotFeatures* f=get_first_feature_obj(current);
00106     uint32_t offs=0;
00107 
00108     while (f)
00109     {
00110         int32_t dim = f->get_dim_feature_space();
00111         result += f->dense_dot(vec_idx1, vec2+offs, dim)*f->get_combined_feature_weight();
00112         offs += dim;
00113         f=get_next_feature_obj(current);
00114     }
00115 
00116     return result;
00117 }
00118 
00119 void CCombinedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
00120 {
00121     CListElement<CDotFeatures*> * current = NULL ;
00122     CDotFeatures* f=get_first_feature_obj(current);
00123     uint32_t offs=0;
00124 
00125     while (f)
00126     {
00127         int32_t dim = f->get_dim_feature_space();
00128         f->add_to_dense_vec(alpha*f->get_combined_feature_weight(), vec_idx1, vec2+offs, dim, abs_val);
00129         offs += dim;
00130         f=get_next_feature_obj(current);
00131     }
00132 }
00133 
00134 
00135 int32_t CCombinedDotFeatures::get_nnz_features_for_vector(int32_t num)
00136 {
00137     CListElement<CDotFeatures*> * current = NULL ;
00138     CDotFeatures* f=get_first_feature_obj(current);
00139     int32_t result=0;
00140 
00141     while (f)
00142     {
00143         result+=f->get_nnz_features_for_vector(num);
00144         f=get_next_feature_obj(current);
00145     }
00146 
00147     return result;
00148 }
00149 
00150 void CCombinedDotFeatures::get_subfeature_weights(float64_t** weights, int32_t* num_weights)
00151 {
00152     *num_weights = get_num_feature_obj();
00153     ASSERT(*num_weights > 0);
00154 
00155     *weights=new float64_t[*num_weights];
00156     float64_t* w = *weights;
00157 
00158     CListElement<CDotFeatures*> * current = NULL;   
00159     CDotFeatures* f = get_first_feature_obj(current);
00160 
00161     while (f)
00162     {
00163         *w++=f->get_combined_feature_weight();
00164         f = get_next_feature_obj(current);
00165     }
00166 }
00167 
00168 void CCombinedDotFeatures::set_subfeature_weights(
00169     float64_t* weights, int32_t num_weights)
00170 {
00171     int32_t i=0 ;
00172     CListElement<CDotFeatures*> * current = NULL ;  
00173     CDotFeatures* f = get_first_feature_obj(current);
00174 
00175     ASSERT(num_weights==get_num_feature_obj());
00176 
00177     while(f)
00178     {
00179         f->set_combined_feature_weight(weights[i]);
00180         f = get_next_feature_obj(current);
00181         i++;
00182     }
00183 }

SHOGUN Machine Learning Toolbox - Documentation