SHOGUN
v3.2.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) 2013 Evangelos Anagnostopoulos 00008 * Copyright (C) 2013 Evangelos Anagnostopoulos 00009 */ 00010 00011 #include <shogun/features/RandomKitchenSinksDotFeatures.h> 00012 #include <shogun/features/DenseFeatures.h> 00013 #include <typeinfo> 00014 00015 namespace shogun 00016 { 00017 00018 class CRKSFunctions; 00019 00020 CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures() 00021 : CDotFeatures() 00022 { 00023 init(NULL, 0); 00024 } 00025 00026 CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures( 00027 CDotFeatures* dataset, int32_t K) 00028 { 00029 init(dataset, K); 00030 } 00031 00032 CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures( 00033 CDotFeatures* dataset, int32_t K, SGMatrix<float64_t> coeff) 00034 { 00035 init(dataset, K); 00036 random_coeff = coeff; 00037 } 00038 00039 SGMatrix<float64_t> CRandomKitchenSinksDotFeatures::generate_random_coefficients() 00040 { 00041 SGVector<float64_t> vec = generate_random_parameter_vector(); 00042 SGMatrix<float64_t> random_params(vec.vlen, num_samples); 00043 for (index_t dim=0; dim<random_params.num_rows; dim++) 00044 random_params(dim, 0) = vec[dim]; 00045 00046 for (index_t sample=1; sample<num_samples; sample++) 00047 { 00048 vec = generate_random_parameter_vector(); 00049 for (index_t dim=0; dim<random_params.num_rows; dim++) 00050 random_params(dim, sample) = vec[dim]; 00051 } 00052 return random_params; 00053 } 00054 00055 CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures(CFile* loader) 00056 { 00057 SG_NOTIMPLEMENTED; 00058 } 00059 00060 CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures( 00061 const CRandomKitchenSinksDotFeatures& orig) 00062 { 00063 init(orig.feats, orig.num_samples); 00064 random_coeff = orig.random_coeff; 00065 } 00066 00067 CRandomKitchenSinksDotFeatures::~CRandomKitchenSinksDotFeatures() 00068 { 00069 SG_UNREF(feats); 00070 } 00071 00072 void CRandomKitchenSinksDotFeatures::init(CDotFeatures* dataset, 00073 int32_t K) 00074 { 00075 feats = dataset; 00076 SG_REF(feats); 00077 00078 num_samples = K; 00079 00080 SG_ADD((CSGObject** ) &feats, "feats", "Features to work on", 00081 MS_NOT_AVAILABLE); 00082 m_parameters->add(&random_coeff, "random_coeff", "Random function parameters"); 00083 } 00084 00085 int32_t CRandomKitchenSinksDotFeatures::get_dim_feature_space() const 00086 { 00087 return num_samples; 00088 } 00089 00090 float64_t CRandomKitchenSinksDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, 00091 int32_t vec_idx2) 00092 { 00093 ASSERT(typeid(*this) == typeid(*df)); 00094 CRandomKitchenSinksDotFeatures* other = (CRandomKitchenSinksDotFeatures* ) df; 00095 ASSERT(get_dim_feature_space()==other->get_dim_feature_space()); 00096 00097 float64_t dot_product = 0; 00098 for (index_t i=0; i<num_samples; i++) 00099 { 00100 float64_t tmp_dot_1 = dot(vec_idx1, i); 00101 float64_t tmp_dot_2 = other->dot(vec_idx2, i); 00102 00103 tmp_dot_1 = post_dot(tmp_dot_1, i); 00104 tmp_dot_2 = other->post_dot(tmp_dot_2, i); 00105 dot_product += tmp_dot_1 * tmp_dot_2; 00106 } 00107 return dot_product; 00108 } 00109 00110 float64_t CRandomKitchenSinksDotFeatures::dense_dot( 00111 int32_t vec_idx1, float64_t* vec2, int32_t vec2_len) 00112 { 00113 SG_DEBUG("entering dense_dot()\n"); 00114 ASSERT(vec2_len == get_dim_feature_space()); 00115 00116 float64_t dot_product = 0; 00117 for (index_t i=0; i<num_samples; i++) 00118 { 00119 float64_t tmp_dot = dot(vec_idx1, i); 00120 tmp_dot = post_dot(tmp_dot, i); 00121 dot_product += tmp_dot * vec2[i]; 00122 } 00123 SG_DEBUG("Leaving dense_dot()\n"); 00124 return dot_product; 00125 } 00126 00127 void CRandomKitchenSinksDotFeatures::add_to_dense_vec(float64_t alpha, 00128 int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) 00129 { 00130 SG_DEBUG("Entering add_to_dense()\n"); 00131 ASSERT(vec2_len == get_dim_feature_space()); 00132 00133 for (index_t i=0; i<num_samples; i++) 00134 { 00135 float64_t tmp_dot = dot(vec_idx1, i); 00136 tmp_dot = post_dot(tmp_dot, i); 00137 if (abs_val) 00138 vec2[i] += CMath::abs(alpha * tmp_dot); 00139 else 00140 vec2[i] += alpha * tmp_dot; 00141 } 00142 SG_DEBUG("Leaving add_to_dense()\n"); 00143 } 00144 00145 int32_t CRandomKitchenSinksDotFeatures::get_nnz_features_for_vector(int32_t num) 00146 { 00147 return num_samples; 00148 } 00149 00150 void* CRandomKitchenSinksDotFeatures::get_feature_iterator(int32_t vector_index) 00151 { 00152 SG_NOTIMPLEMENTED; 00153 return NULL; 00154 } 00155 00156 bool CRandomKitchenSinksDotFeatures::get_next_feature(int32_t& index, 00157 float64_t& value, void* iterator) 00158 { 00159 SG_NOTIMPLEMENTED; 00160 return false; 00161 } 00162 00163 void CRandomKitchenSinksDotFeatures::free_feature_iterator(void* iterator) 00164 { 00165 SG_NOTIMPLEMENTED; 00166 } 00167 00168 EFeatureType CRandomKitchenSinksDotFeatures::get_feature_type() const 00169 { 00170 return F_DREAL; 00171 } 00172 00173 EFeatureClass CRandomKitchenSinksDotFeatures::get_feature_class() const 00174 { 00175 return C_DENSE; 00176 } 00177 00178 int32_t CRandomKitchenSinksDotFeatures::get_num_vectors() const 00179 { 00180 return feats->get_num_vectors(); 00181 } 00182 00183 const char* CRandomKitchenSinksDotFeatures::get_name() const 00184 { 00185 return "RandomKitchenSinksDotFeatures"; 00186 } 00187 00188 CFeatures* CRandomKitchenSinksDotFeatures::duplicate() const 00189 { 00190 SG_NOTIMPLEMENTED; 00191 return NULL; 00192 } 00193 00194 SGMatrix<float64_t> CRandomKitchenSinksDotFeatures::get_random_coefficients() 00195 { 00196 return random_coeff; 00197 } 00198 00199 float64_t CRandomKitchenSinksDotFeatures::dot(index_t vec_idx, index_t par_idx) 00200 { 00201 return feats->dense_dot(vec_idx, random_coeff.get_column_vector(par_idx), 00202 feats->get_dim_feature_space()); 00203 } 00204 00205 float64_t CRandomKitchenSinksDotFeatures::post_dot(float64_t dot_result, index_t par_idx) 00206 { 00207 return dot_result; 00208 } 00209 00210 }