SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PyramidChi2.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) 2008-2009 Alexander Binder
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
12 #include <shogun/lib/common.h>
16 #include <shogun/io/SGIO.h>
18 
19 using namespace shogun;
20 
22 : weights(NULL)
23 {
24  // this will produce an erro in kernel computation!
25  num_cells=0;
27  width=1;
29 }
30 
32  int32_t size, int32_t num_cells2,
33  float64_t* weights_foreach_cell2,
34  int32_t width_computation_type2,
35  float64_t width2)
36 : CDotKernel(size), num_cells(num_cells2),weights(NULL),
37 width_computation_type(width_computation_type2), width(width2),
38  num_randfeats_forwidthcomputation(-1)
39 {
40  if(num_cells<=0)
41  SG_ERROR("CPyramidChi2 Constructor fatal error: parameter num_cells2 NOT positive");
43  if(weights_foreach_cell2)
44  {
45  for (int32_t i=0; i<num_cells; ++i)
46  weights[i]=weights_foreach_cell2[i];
47  }
48  else
49  { for (int32_t i=0; i<num_cells; ++i)
50  weights[i]=1;
51  }
52 
54  {
56  width=-1;
57  }
58 
59 
60 }
61 
63 {
64  // this will produce an erro in kernel computation!
65  num_cells=0;
67  width=1;
68 
70 
72  weights=NULL;
73 
75 }
76 
77 bool CPyramidChi2::init(CFeatures* l, CFeatures* r)
78 {
79  CDotKernel::init(l, r);
80  return init_normalizer();
81 }
82 
85  int32_t size, int32_t num_cells2,
86  float64_t* weights_foreach_cell2,
87  int32_t width_computation_type2,
88  float64_t width2)
89 : CDotKernel(size), num_cells(num_cells2), weights(NULL),
90 width_computation_type(width_computation_type2), width(width2),
91  num_randfeats_forwidthcomputation(-1)
92 {
93  if(num_cells<=0)
94  SG_ERROR("CPyramidChi2 Constructor fatal error: parameter num_cells2 NOT positive");
96  if(weights_foreach_cell2)
97  {
98  for (int32_t i=0; i<num_cells; ++i)
99  weights[i]=weights_foreach_cell2[i];
100  }
101  else
102  { for (int32_t i=0; i<num_cells; ++i)
103  weights[i]=1;
104  }
105 
106  if (width_computation_type>0 )
107  {
109  width=-1;
110  }
111 
112  init(l, r);
113 }
114 
116 {
117  cleanup();
118 }
119 
120 
121 
122 float64_t CPyramidChi2::compute(int32_t idx_a, int32_t idx_b)
123 {
124 
125  if(num_cells<=0)
126  SG_ERROR("CPyramidChi2::compute(...) fatal error: parameter num_cells NOT positive");
127 
128  int32_t alen, blen;
129  bool afree, bfree;
130 
131  float64_t* avec=((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a,
132  alen, afree);
133  float64_t* bvec=((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b,
134  blen, bfree);
135  if(alen!=blen)
136  SG_ERROR("CPyramidChi2::compute(...) fatal error: lhs feature dim != rhs feature dim");
137 
138  int32_t dims=alen/num_cells;
139 
140 
141  if(width<=0)
142  {
144  {
145 
146  //compute width
147  int32_t numind;
148 
150  {
152  }
153  else
154  {
155  numind= ((CSimpleFeatures<float64_t>*) lhs)->get_num_vectors();
156  }
157  float64_t* featindices = SG_MALLOC(float64_t, numind);
158 
160  {
161  for(int32_t i=0; i< numind;++i)
162  featindices[i]=CMath::random(0, ((CSimpleFeatures<float64_t>*) lhs)->get_num_vectors()-1);
163  }
164  else
165  {
166  for(int32_t i=0; i< numind;++i)
167  featindices[i]=i;
168  }
169 
170 
171  width=0;
172 
173  //get avec, get bvec only from lhs, do not free
174  for (int32_t li=0; li < numind;++li)
175  {
176  avec=((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(featindices[li],
177  alen, afree);
178  for (int32_t ri=0; ri <=li;++ri)
179  {
180  // lhs is right here!!!
181  bvec=((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(featindices[ri],
182  blen, bfree);
183 
184  float64_t result=0;
185  for (int32_t histoind=0; histoind<num_cells; ++histoind)
186  {
187  float64_t curweight=weights[histoind];
188 
189  for (int32_t i=0; i< dims; ++i)
190  {
191  int32_t index= histoind*dims+i;
192  if(avec[index] + bvec[index]>0)
193  {
194  result+= curweight*(avec[index] - bvec[index])*(avec[index]
195  - bvec[index])/(avec[index] + bvec[index]);
196  }
197  }
198  }
199  width+=result*2.0/((double)numind)/(numind+1.0);
200  }
201 
202  }
203  SG_FREE(featindices);
204  }
205  else
206  {
207  SG_ERROR("CPyramidChi2::compute(...) fatal error: width<=0");
208  }
209  }
210 
211 
212  //the actual kernel computation
213  avec=((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a,
214  alen, afree);
215  bvec=((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b,
216  blen, bfree);
217 
218  float64_t result=0;
219  for (int32_t histoind=0; histoind<num_cells; ++histoind)
220  {
221  float64_t curweight=weights[histoind];
222 
223  for (int32_t i=0; i< dims; ++i)
224  {
225  int32_t index= histoind*dims+i;
226  if(avec[index] + bvec[index]>0)
227  {
228  result+= curweight*(avec[index] - bvec[index])*(avec[index]
229  - bvec[index])/(avec[index] + bvec[index]);
230  }
231  }
232  }
233  result= CMath::exp(-result/width);
234 
235 
236  ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
237  ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
238 
239  return (result);
240 }
241 
242 void CPyramidChi2::setparams_pychi2(int32_t num_cells2,
243  float64_t* weights_foreach_cell2,
244  int32_t width_computation_type2,
245  float64_t width2)
246 {
247  num_cells=num_cells2;
248  width_computation_type=width_computation_type2;
249  width=width2;
251 
252  if(num_cells<=0)
253  SG_ERROR("CPyramidChi2::setparams_pychi2(...) fatal error: parameter num_cells2 NOT positive");
254  if(weights)
255  SG_FREE(weights);
257  if(weights_foreach_cell2)
258  {
259  for (int32_t i=0; i<num_cells; ++i)
260  weights[i]=weights_foreach_cell2[i];
261  }
262  else
263  { for (int32_t i=0; i<num_cells; ++i)
264  weights[i]=1;
265  }
266 
267  if (width_computation_type>0 )
268  {
270  width=-1;
271  }
272 }

SHOGUN Machine Learning Toolbox - Documentation