SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RandomKitchenSinksDotFeatures.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Evangelos Anagnostopoulos
8  * Copyright (C) 2013 Evangelos Anagnostopoulos
9  */
10 
13 #include <typeinfo>
14 
15 namespace shogun
16 {
17 
18 class CRKSFunctions;
19 
21  : CDotFeatures()
22 {
23  init(NULL, 0);
24 }
25 
27  CDotFeatures* dataset, int32_t K)
28 {
29  init(dataset, K);
30 }
31 
33  CDotFeatures* dataset, int32_t K, SGMatrix<float64_t> coeff)
34 {
35  init(dataset, K);
36  random_coeff = coeff;
37 }
38 
40 {
42  SGMatrix<float64_t> random_params(vec.vlen, num_samples);
43  for (index_t dim=0; dim<random_params.num_rows; dim++)
44  random_params(dim, 0) = vec[dim];
45 
46  for (index_t sample=1; sample<num_samples; sample++)
47  {
49  for (index_t dim=0; dim<random_params.num_rows; dim++)
50  random_params(dim, sample) = vec[dim];
51  }
52  return random_params;
53 }
54 
56 {
58 }
59 
62 {
63  init(orig.feats, orig.num_samples);
65 }
66 
68 {
69  SG_UNREF(feats);
70 }
71 
72 void CRandomKitchenSinksDotFeatures::init(CDotFeatures* dataset,
73  int32_t K)
74 {
75  feats = dataset;
76  SG_REF(feats);
77 
78  num_samples = K;
79 
80  SG_ADD((CSGObject** ) &feats, "feats", "Features to work on",
82  m_parameters->add(&random_coeff, "random_coeff", "Random function parameters");
83 }
84 
86 {
87  return num_samples;
88 }
89 
91  int32_t vec_idx2)
92 {
93  ASSERT(typeid(*this) == typeid(*df));
96 
97  float64_t dot_product = 0;
98  for (index_t i=0; i<num_samples; i++)
99  {
100  float64_t tmp_dot_1 = dot(vec_idx1, i);
101  float64_t tmp_dot_2 = other->dot(vec_idx2, i);
102 
103  tmp_dot_1 = post_dot(tmp_dot_1, i);
104  tmp_dot_2 = other->post_dot(tmp_dot_2, i);
105  dot_product += tmp_dot_1 * tmp_dot_2;
106  }
107  return dot_product;
108 }
109 
111  int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
112 {
113  SG_DEBUG("entering dense_dot()\n");
114  ASSERT(vec2_len == get_dim_feature_space());
115 
116  float64_t dot_product = 0;
117  for (index_t i=0; i<num_samples; i++)
118  {
119  float64_t tmp_dot = dot(vec_idx1, i);
120  tmp_dot = post_dot(tmp_dot, i);
121  dot_product += tmp_dot * vec2[i];
122  }
123  SG_DEBUG("Leaving dense_dot()\n");
124  return dot_product;
125 }
126 
128  int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
129 {
130  SG_DEBUG("Entering add_to_dense()\n");
131  ASSERT(vec2_len == get_dim_feature_space());
132 
133  for (index_t i=0; i<num_samples; i++)
134  {
135  float64_t tmp_dot = dot(vec_idx1, i);
136  tmp_dot = post_dot(tmp_dot, i);
137  if (abs_val)
138  vec2[i] += CMath::abs(alpha * tmp_dot);
139  else
140  vec2[i] += alpha * tmp_dot;
141  }
142  SG_DEBUG("Leaving add_to_dense()\n");
143 }
144 
146 {
147  return num_samples;
148 }
149 
151 {
153  return NULL;
154 }
155 
157  float64_t& value, void* iterator)
158 {
160  return false;
161 }
162 
164 {
166 }
167 
169 {
170  return F_DREAL;
171 }
172 
174 {
175  return C_DENSE;
176 }
177 
179 {
180  return feats->get_num_vectors();
181 }
182 
184 {
185  return "RandomKitchenSinksDotFeatures";
186 }
187 
189 {
191  return NULL;
192 }
193 
195 {
196  return random_coeff;
197 }
198 
200 {
201  return feats->dense_dot(vec_idx, random_coeff.get_column_vector(par_idx),
203 }
204 
206 {
207  return dot_result;
208 }
209 
210 }
class that implements the Random Kitchen Sinks (RKS) for the DotFeatures as mentioned in http://books...
int32_t index_t
Definition: common.h:62
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)=0
virtual bool get_next_feature(int32_t &index, float64_t &value, void *iterator)
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)
virtual int32_t get_num_vectors() const =0
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
Parameter * m_parameters
Definition: SGObject.h:378
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:51
index_t num_rows
Definition: SGMatrix.h:376
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
virtual int32_t get_dim_feature_space() const =0
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:37
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
A File access base class.
Definition: File.h:34
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:115
virtual SGVector< float64_t > generate_random_parameter_vector()=0
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
#define SG_UNREF(x)
Definition: SGObject.h:52
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)
virtual void * get_feature_iterator(int32_t vector_index)
virtual float64_t post_dot(float64_t dot_result, index_t par_idx)
#define SG_ADD(...)
Definition: SGObject.h:81
static T abs(T a)
Definition: Math.h:179

SHOGUN Machine Learning Toolbox - Documentation