SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StreamingHashedDenseFeatures.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 
14 
15 namespace shogun
16 {
17 template <class ST>
19 {
20  init(NULL, false, 0, 0, false, true);
21 }
22 
23 template <class ST>
25  bool is_labelled, int32_t size, int32_t d, bool use_quadr, bool keep_lin_terms)
26 {
27  init(file, is_labelled, size, d, use_quadr, keep_lin_terms);
28 }
29 
30 template <class ST>
32  int32_t d, bool use_quadr, bool keep_lin_terms, float64_t* lab)
33 {
34  ASSERT(dot_features);
35 
37  new CStreamingFileFromDenseFeatures<ST>(dot_features, lab);
38  bool is_labelled = (lab != NULL);
39  int32_t size = 1024;
40 
41  init(file, is_labelled, size, d, use_quadr, keep_lin_terms);
42 
43  parser.set_free_vectors_on_destruct(false);
44  seekable=true;
45 }
46 
47 template <class ST>
49 {
50 }
51 
52 template <class ST>
53 void CStreamingHashedDenseFeatures<ST>::init(CStreamingFile* file, bool is_labelled,
54  int32_t size, int32_t d, bool use_quadr, bool keep_lin_terms)
55 {
56  dim = d;
57  use_quadratic = use_quadr;
58  keep_linear_terms = keep_lin_terms;
59 
60  SG_ADD(&use_quadratic, "use_quadratic", "Whether to use quadratic features",
62  SG_ADD(&keep_linear_terms, "keep_linear_terms", "Whether to keep the linear terms or not",
64  SG_ADD(&dim, "dim", "Size of target dimension", MS_NOT_AVAILABLE);
65 
66  has_labels = is_labelled;
67  if (file)
68  {
69  working_file = file;
70  SG_REF(working_file);
71  parser.init(file, is_labelled, size);
72  seekable = false;
73  }
74  else
75  file = NULL;
76 
77  set_read_functions();
78  parser.set_free_vector_after_release(false);
79 
80  set_generic<ST>();
81 }
82 
83 template <class ST>
85 {
86  ASSERT(df);
87  ASSERT(df->get_feature_type() == get_feature_type())
88  ASSERT(strcmp(df->get_name(),get_name())==0)
89 
91  return current_vector.sparse_dot(hdf->current_vector);
92 }
93 
94 template <class ST>
96 {
97  ASSERT(vec2_len == dim);
98 
99  float32_t result = 0;
100  for (index_t i=0; i<current_vector.num_feat_entries; i++)
101  result += vec2[current_vector.features[i].feat_index] * current_vector.features[i].entry;
102 
103  return result;
104 }
105 
106 template <class ST>
108  int32_t vec2_len, bool abs_val)
109 {
110  ASSERT(vec2_len == dim);
111 
112  if (abs_val)
113  alpha = CMath::abs(alpha);
114 
115  for (index_t i=0; i<current_vector.num_feat_entries; i++)
116  vec2[current_vector.features[i].feat_index] += alpha * current_vector.features[i].entry;
117 }
118 
119 template <class ST>
121 {
122  return dim;
123 }
124 
125 template <class ST>
127 {
128  return "StreamingHashedDenseFeatures";
129 }
130 
131 template <class ST>
133 {
134  return 1;
135 }
136 
137 template <class ST>
139 {
140  return new CStreamingHashedDenseFeatures<ST>(*this);
141 }
142 
143 template <class ST>
145 {
146  parser.set_read_vector(&CStreamingFile::get_vector);
147 }
148 
149 template <class ST>
151 {
152  parser.set_read_vector_and_label(&CStreamingFile::get_vector_and_label);
153 }
154 
155 template <class ST>
157 {
158  return F_UINT;
159 }
160 
161 template <class ST>
163 {
164  return C_STREAMING_SPARSE;
165 }
166 
167 template <class ST>
169 {
170  if (!parser.is_running())
171  parser.start_parser();
172 }
173 
174 template <class ST>
176 {
177  parser.end_parser();
178 }
179 
180 template <class ST>
182 {
183  return current_label;
184 }
185 
186 template <class ST>
188 {
189  SGVector<ST> tmp;
190  if (parser.get_next_example(tmp.vector,
191  tmp.vlen, current_label))
192  {
193  current_vector = CHashedDenseFeatures<ST>::hash_vector(tmp, dim, use_quadratic,
194  keep_linear_terms);
195  tmp.vector = NULL;
196  tmp.vlen = -1;
197  return true;
198  }
199  return false;
200 }
201 
202 template <class ST>
204 {
205  parser.finalize_example();
206 }
207 
208 template <class ST>
210 {
211  return dim;
212 }
213 
214 template <class ST>
216 {
217  return current_vector;
218 }
219 
233 }

SHOGUN Machine Learning Toolbox - Documentation