SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DenseSubSamplesFeatures.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (W) 2015 Wu Lin
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  *
30  */
32 
33 namespace shogun
34 {
35 
37  :CDotFeatures()
38 {
39  init();
40 }
41 
44  :CDotFeatures()
45 {
46  init();
47  set_features(fea);
48  set_subset_idx(idx);
49 }
50 
52 {
53  SG_UNREF(m_fea);
54 }
55 
57 {
58  if (m_fea!=fea)
59  {
60  SG_REF(fea);
61  SG_UNREF(m_fea);
62  m_fea = fea;
63  }
64 }
65 
66 template<class ST> void CDenseSubSamplesFeatures<ST>::init()
67 {
68  set_generic<ST>();
69  m_fea=NULL;
70  m_idx=SGVector<int32_t>();
71  SG_ADD(&m_idx, "idx", "idx", MS_NOT_AVAILABLE);
72  SG_ADD((CSGObject **)&m_fea, "fea", "fea", MS_NOT_AVAILABLE);
73 }
74 
76 {
77  return new CDenseSubSamplesFeatures(m_fea, m_idx);
78 }
79 
81 {
82  if (this->get_feature_class()==rhs || m_fea->get_feature_class()==rhs)
83  return true;
84  return false;
85 }
86 
88 
89 {
90  return m_fea->get_feature_type();
91 }
92 
94 {
95  return C_SUB_SAMPLES_DENSE;
96 }
97 
98 template<class ST> int32_t CDenseSubSamplesFeatures<ST>::get_dim_feature_space() const
99 {
100  return m_fea->get_dim_feature_space();
101 }
102 
103 template<class ST> int32_t CDenseSubSamplesFeatures<ST>::get_num_vectors() const
104 {
105  return m_idx.vlen;
106 }
107 
109 {
110  REQUIRE(m_fea, "Please set the features first\n");
111  int32_t total_vlen=m_fea->get_num_vectors();
112  for (int32_t i=0; i<idx.vlen; i++)
113  {
114  int32_t index=idx[i];
115  REQUIRE(index>=0 && index<total_vlen,
116  "The index (%d) in the vector idx is not valid since there are %d samples\n",
117  index, total_vlen);
118  }
119  m_idx = idx;
120 }
121 
123  int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
124 {
125  check_bound(vec_idx1);
127  float64_t res=0.0;
128  if (df_f)
129  {
130  REQUIRE(df_f->get_feature_type()==get_feature_type(),"Features type do not match\n");
131  REQUIRE(df_f->get_feature_class()==get_feature_class(),"Features class do not match\n");
132  df_f->check_bound(vec_idx2);
133  res=m_fea->dot(m_idx[vec_idx1], df_f->m_fea, df_f->m_idx[vec_idx2]);
134  }
135  else
136  res=m_fea->dot(m_idx[vec_idx1], df, vec_idx2);
137  return res;
138 }
139 
141  int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
142 {
143  check_bound(vec_idx1);
144  return m_fea->dense_dot(m_idx[vec_idx1], vec2, vec2_len);
145 }
146 
147 template<class ST> void CDenseSubSamplesFeatures<ST>::check_bound(int32_t index)
148 {
149  REQUIRE(index<m_idx.vlen && index>=0,
150  "Index (%d) is out of bound (%d)\n", index, m_idx.vlen);
151 }
152 
154  float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
155 {
156  check_bound(vec_idx1);
157  m_fea->add_to_dense_vec(alpha, m_idx[vec_idx1], vec2, vec2_len, abs_val);
158 }
159 
161  int32_t num)
162 {
163  return m_fea->get_nnz_features_for_vector(num);
164 }
165 
167  int32_t vector_index)
168 {
170  return NULL;
171 }
172 
174  int32_t& index, float64_t& value, void* iterator)
175 {
177  return false;
178 }
179 
181  void* iterator)
182 {
184 }
185 
186 template class CDenseSubSamplesFeatures<bool>;
187 template class CDenseSubSamplesFeatures<char>;
188 template class CDenseSubSamplesFeatures<int8_t>;
189 template class CDenseSubSamplesFeatures<uint8_t>;
190 template class CDenseSubSamplesFeatures<int16_t>;
192 template class CDenseSubSamplesFeatures<int32_t>;
194 template class CDenseSubSamplesFeatures<int64_t>;
199 
200 }
void set_subset_idx(SGVector< int32_t > idx)
virtual bool get_next_feature(int32_t &index, float64_t &value, void *iterator)
The class DenseFeatures implements dense feature matrices.
Definition: LDA.h:41
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)
#define REQUIRE(x,...)
Definition: SGIO.h:206
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
Features that support dot products among other operations.
Definition: DotFeatures.h:44
virtual int32_t get_dim_feature_space() const
#define SG_REF(x)
Definition: SGObject.h:51
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
virtual EFeatureType get_feature_type() const
void set_features(CDenseFeatures< ST > *fea)
index_t vlen
Definition: SGVector.h:494
virtual bool get_feature_class_compatibility(EFeatureClass rhs) const
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
virtual CFeatures * duplicate() const
double float64_t
Definition: common.h:50
virtual void * get_feature_iterator(int32_t vector_index)
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)
#define SG_UNREF(x)
Definition: SGObject.h:52
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 void free_feature_iterator(void *iterator)
#define SG_ADD(...)
Definition: SGObject.h:81
virtual int32_t get_nnz_features_for_vector(int32_t num)
virtual EFeatureClass get_feature_class() const

SHOGUN Machine Learning Toolbox - Documentation