SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StreamingSimpleFeatures.cpp
Go to the documentation of this file.
4 
5 namespace shogun
6 {
8 {
10  init();
11  parser.set_free_vector_after_release(false);
12 }
13 
15  bool is_labelled,
16  int32_t size)
18 {
19  init(file, is_labelled, size);
21  parser.set_free_vector_after_release(false);
22 }
23 
25  float64_t* lab)
27 {
29  bool is_labelled;
30  int32_t size = 1024;
31 
32  if (lab)
33  {
34  is_labelled = true;
35  file = new CStreamingFileFromSimpleFeatures<T>(simple_features, lab);
36  }
37  else
38  {
39  is_labelled = false;
40  file = new CStreamingFileFromSimpleFeatures<T>(simple_features);
41  }
42 
43  SG_REF(file);
44 
45  init(file, is_labelled, size);
47  parser.set_free_vector_after_release(false);
48  parser.set_free_vectors_on_destruct(false);
49  seekable=true;
50 }
51 
53 {
54  parser.end_parser();
55 }
56 
58 {
59  if (seekable)
60  {
61  ((CStreamingFileFromSimpleFeatures<T>*) working_file)->reset_stream();
62  parser.exit_parser();
63  parser.init(working_file, has_labels, 1);
64  parser.set_free_vector_after_release(false);
65  parser.start_parser();
66  }
67 }
68 
69 template <class T> float32_t CStreamingSimpleFeatures<T>::dense_dot(const float32_t* vec2, int32_t vec2_len)
70 {
71  ASSERT(vec2_len==current_length);
72  float32_t result=0;
73 
74  for (int32_t i=0; i<current_length; i++)
75  result+=current_vector[i]*vec2[i];
76 
77  return result;
78 }
79 
80 template <class T> float64_t CStreamingSimpleFeatures<T>::dense_dot(const float64_t* vec2, int32_t vec2_len)
81 {
82  ASSERT(vec2_len==current_length);
83  float64_t result=0;
84 
85  for (int32_t i=0; i<current_length; i++)
86  result+=current_vector[i]*vec2[i];
87 
88  return result;
89 }
90 
91 template <class T> void CStreamingSimpleFeatures<T>::add_to_dense_vec(float32_t alpha, float32_t* vec2, int32_t vec2_len , bool abs_val)
92 {
93  ASSERT(vec2_len==current_length);
94 
95  if (abs_val)
96  {
97  for (int32_t i=0; i<current_length; i++)
98  vec2[i]+=alpha*CMath::abs(current_vector[i]);
99  }
100  else
101  {
102  for (int32_t i=0; i<current_length; i++)
103  vec2[i]+=alpha*current_vector[i];
104  }
105 }
106 
107 template <class T> void CStreamingSimpleFeatures<T>::add_to_dense_vec(float64_t alpha, float64_t* vec2, int32_t vec2_len , bool abs_val)
108 {
109  ASSERT(vec2_len==current_length);
110 
111  if (abs_val)
112  {
113  for (int32_t i=0; i<current_length; i++)
114  vec2[i]+=alpha*CMath::abs(current_vector[i]);
115  }
116  else
117  {
118  for (int32_t i=0; i<current_length; i++)
119  vec2[i]+=alpha*current_vector[i];
120  }
121 }
122 
124 {
125  return current_length;
126 }
127 
129 {
130  return new CStreamingSimpleFeatures<T>(*this);
131 }
132 
133 template <class T> int32_t CStreamingSimpleFeatures<T>::get_num_vectors() const
134 {
135  if (current_vector)
136  return 1;
137  return 0;
138 }
139 
140 template <class T> int32_t CStreamingSimpleFeatures<T>::get_size()
141 {
142  return sizeof(T);
143 }
144 
145 template <class T>
147 {
148  parser.set_read_vector(&CStreamingFile::get_vector);
149 }
150 
151 template <class T>
153 {
154  parser.set_read_vector_and_label(&CStreamingFile::get_vector_and_label);
155 }
156 
157 #define GET_FEATURE_TYPE(f_type, sg_type) \
158 template<> EFeatureType CStreamingSimpleFeatures<sg_type>::get_feature_type() \
159 { \
160  return f_type; \
161 }
162 
165 GET_FEATURE_TYPE(F_BYTE, uint8_t)
166 GET_FEATURE_TYPE(F_BYTE, int8_t)
167 GET_FEATURE_TYPE(F_SHORT, int16_t)
168 GET_FEATURE_TYPE(F_WORD, uint16_t)
169 GET_FEATURE_TYPE(F_INT, int32_t)
170 GET_FEATURE_TYPE(F_UINT, uint32_t)
171 GET_FEATURE_TYPE(F_LONG, int64_t)
172 GET_FEATURE_TYPE(F_ULONG, uint64_t)
176 #undef GET_FEATURE_TYPE
177 
178 
179 template <class T>
180 void CStreamingSimpleFeatures<T>::init()
181 {
182  working_file=NULL;
183  current_vector=NULL;
184  seekable=false;
185  current_length=-1;
186 }
187 
188 template <class T>
189 void CStreamingSimpleFeatures<T>::init(CStreamingFile* file,
190  bool is_labelled,
191  int32_t size)
192 {
193  init();
194  has_labels = is_labelled;
195  working_file = file;
196  parser.init(file, is_labelled, size);
197  seekable=false;
198 }
199 
200 template <class T>
202 {
203  if (!parser.is_running())
204  parser.start_parser();
205 }
206 
207 template <class T>
209 {
210  parser.end_parser();
211 }
212 
213 template <class T>
215 {
216  bool ret_value;
217  ret_value = (bool) parser.get_next_example(current_vector,
218  current_length,
219  current_label);
220 
221  return ret_value;
222 }
223 
224 template <class T>
226 {
227  current_sgvector.vector=current_vector;
228  current_sgvector.vlen=current_length;
229 
230  return current_sgvector;
231 }
232 
233 template <class T>
235 {
236  ASSERT(has_labels);
237 
238  return current_label;
239 }
240 
241 template <class T>
243 {
244  parser.finalize_example();
245 }
246 
247 template <class T>
249 {
250  return current_length;
251 }
252 
253 template <class T>
255 {
256  ASSERT(df);
257  ASSERT(df->get_feature_type() == get_feature_type());
258  ASSERT(df->get_feature_class() == get_feature_class());
260 
261  SGVector<T> other_vector=sf->get_vector();
262 
263  float32_t result = CMath::dot(current_vector, other_vector.vector, current_length);
264 
265  return result;
266 }
267 
268 template <class T>
270 {
271  int32_t len1;
272  len1=sgvec1.vlen;
273 
274  if (len1 != current_length)
275  SG_ERROR("Lengths %d and %d not equal while computing dot product!\n", len1, current_length);
276 
277  float32_t result=CMath::dot(current_vector, sgvec1.vector, len1);
278  return result;
279 }
280 
281 template <class T>
283 {
284  return current_length;
285 }
286 
287 template <class T>
289 {
290  return C_STREAMING_SIMPLE;
291 }
292 
293 template class CStreamingSimpleFeatures<bool>;
294 template class CStreamingSimpleFeatures<char>;
295 template class CStreamingSimpleFeatures<int8_t>;
296 template class CStreamingSimpleFeatures<uint8_t>;
297 template class CStreamingSimpleFeatures<int16_t>;
299 template class CStreamingSimpleFeatures<int32_t>;
301 template class CStreamingSimpleFeatures<int64_t>;
306 }

SHOGUN Machine Learning Toolbox - Documentation