|
SHOGUN v0.9.3
|
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) 2010 Soeren Sonnenburg 00008 * Copyright (C) 2010 Berlin Institute of Technology 00009 */ 00010 00011 #ifndef _HASHEDWDFEATURES_H___ 00012 #define _HASHEDWDFEATURES_H___ 00013 00014 #include "lib/common.h" 00015 #include "features/DotFeatures.h" 00016 #include "features/StringFeatures.h" 00017 #include "lib/Hash.h" 00018 00019 namespace shogun 00020 { 00021 template <class ST> class CStringFeatures; 00022 00028 class CHashedWDFeatures : public CDotFeatures 00029 { 00030 public: 00031 00040 CHashedWDFeatures(CStringFeatures<uint8_t>* str, int32_t start_order, 00041 int32_t order, int32_t from_order, int32_t hash_bits=12); 00042 00044 CHashedWDFeatures(const CHashedWDFeatures & orig); 00045 00047 virtual ~CHashedWDFeatures(); 00048 00056 inline virtual int32_t get_dim_feature_space() 00057 { 00058 return w_dim; 00059 } 00060 00067 virtual float64_t dot(int32_t vec_idx1, int32_t vec_idx2); 00068 00075 virtual float64_t dense_dot(int32_t vec_idx1, float64_t* vec2, int32_t vec2_len); 00076 00085 virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val=false); 00086 00092 virtual inline int32_t get_nnz_features_for_vector(int32_t num) 00093 { 00094 int32_t vlen=-1; 00095 bool free_vec; 00096 uint8_t* vec=strings->get_feature_vector(num, vlen, free_vec); 00097 strings->free_feature_vector(vec, num, free_vec); 00098 return degree*vlen; 00099 } 00100 00102 struct hashed_wd_feature_iterator 00103 { 00105 uint16_t* vec; 00107 int32_t vidx; 00109 int32_t vlen; 00111 bool vfree; 00112 00114 int32_t index; 00115 00116 }; 00117 00127 virtual void* get_feature_iterator(int32_t vector_index) 00128 { 00129 SG_NOTIMPLEMENTED; 00130 return NULL; 00131 } 00132 00143 virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator) 00144 { 00145 SG_NOTIMPLEMENTED; 00146 return NULL; 00147 } 00148 00154 virtual void free_feature_iterator(void* iterator) 00155 { 00156 SG_NOTIMPLEMENTED; 00157 } 00158 00163 virtual CFeatures* duplicate() const; 00164 00169 inline virtual EFeatureType get_feature_type() 00170 { 00171 return F_UNKNOWN; 00172 } 00173 00178 inline virtual EFeatureClass get_feature_class() 00179 { 00180 return C_WD; 00181 } 00182 00183 inline virtual int32_t get_num_vectors() 00184 { 00185 return num_strings; 00186 } 00187 00188 inline virtual int32_t get_size() 00189 { 00190 return sizeof(float64_t); 00191 } 00192 00195 void set_normalization_const(float64_t n=0); 00196 00198 inline float64_t get_normalization_const() 00199 { 00200 return normalization_const; 00201 } 00202 00204 inline virtual const char* get_name() const { return "HashedWDFeatures"; } 00205 00206 protected: 00207 00209 void set_wd_weights(); 00210 00211 protected: 00213 CStringFeatures<uint8_t>* strings; 00214 00216 int32_t degree; 00218 int32_t start_degree; 00220 int32_t from_degree; 00222 int32_t string_length; 00224 int32_t num_strings; 00226 int32_t alphabet_size; 00228 int32_t w_dim; 00230 int32_t partial_w_dim; 00232 float64_t* wd_weights; 00234 uint32_t mask; 00236 int32_t m_hash_bits; 00237 00239 float64_t normalization_const; 00240 }; 00241 } 00242 #endif // _HASHEDWDFEATURES_H___