SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
DistanceMachine.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) 1999-2009 Christian Gehl
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST
9  */
10 
13 #include <shogun/base/Parameter.h>
14 
15 #ifdef HAVE_OPENMP
16 #include <omp.h>
17 #endif
18 
19 using namespace shogun;
20 
22 : CMachine()
23 {
24  init();
25 }
26 
28 {
30 }
31 
32 void CDistanceMachine::init()
33 {
34  /* all distance machines should store their models, i.e. cluster centers
35  * At least, it has to be ensured, that after calling train(), or in the
36  * call of apply() in the cases where there is no train method, the lhs
37  * of the underlying distance is set to cluster centers */
39 
40  distance=NULL;
41  m_parameters->add((CSGObject**)&distance, "distance", "Distance to use");
42 }
43 
45 {
46  int32_t num_threads;
47  int32_t num_vec;
48  int32_t step;
49 
50  ASSERT(result)
51 
52  #pragma omp parallel shared(num_threads, step)
53  {
54 #ifdef HAVE_OPENMP
55  #pragma omp single
56  {
57  num_threads=omp_get_num_threads();
58  num_vec=idx_a2-idx_a1+1;
59  step=num_vec/num_threads;
60  }
61  int32_t thread_num=omp_get_thread_num();
62 
63  index_t idx_r_start = thread_num * step;
64  index_t idx_start = (thread_num * step) + idx_a1;
65  index_t idx_stop = (thread_num==(num_threads - 1)) ? (idx_a2 + 1) : ((thread_num + 1) * step) + idx_a1;
66  distance->run_distance_lhs(result, idx_r_start, idx_start, idx_stop, idx_b);
67 #else
68  index_t idx_r_start = idx_a1;
69  index_t idx_start = idx_a1;
70  index_t idx_stop = idx_a2 + 1;
71  distance->run_distance_lhs(result, idx_r_start, idx_start, idx_stop, idx_b);
72 #endif
73  }
74 }
75 
77 {
78  int32_t num_threads;
79  int32_t num_vec;
80  int32_t step;
81 
82  ASSERT(result)
83 
84  #pragma omp parallel shared(num_threads, step)
85  {
86 #ifdef HAVE_OPENMP
87  #pragma omp single
88  {
89  num_threads=omp_get_num_threads();
90  num_vec=idx_b2-idx_b1+1;
91  step=num_vec/num_threads;
92  }
93  int32_t thread_num=omp_get_thread_num();
94 
95  index_t idx_r_start = thread_num * step;
96  index_t idx_start = (thread_num * step) + idx_b1;
97  index_t idx_stop = (thread_num==(num_threads - 1)) ? (idx_b2 + 1) : ((thread_num + 1) * step) + idx_b1;
98  distance->run_distance_rhs(result, idx_r_start, idx_start, idx_stop, idx_a);
99 #else
100  index_t idx_r_start = idx_b1;
101  index_t idx_start = idx_b1;
102  index_t idx_stop = idx_b2 + 1;
103  distance->run_distance_rhs(result, idx_r_start, idx_start, idx_stop, idx_a);
104 #endif
105  }
106 }
107 
109 {
110  if (data)
111  {
112  /* set distance features to given ones and apply to all */
113  CFeatures* lhs=distance->get_lhs();
114  distance->init(lhs, data);
115  SG_UNREF(lhs);
116 
117  /* build result labels and classify all elements of procedure */
119  for (index_t i=0; i<data->get_num_vectors(); ++i)
120  result->set_label(i, apply_one(i));
121  return result;
122  }
123  else
124  {
125  /* call apply on complete right hand side */
126  CFeatures* all=distance->get_rhs();
127  CMulticlassLabels* result = apply_multiclass(all);
128  SG_UNREF(all);
129  return result;
130  }
131  return NULL;
132 }
133 
135 {
136  /* number of clusters */
137  CFeatures* lhs=distance->get_lhs();
138  int32_t num_clusters=lhs->get_num_vectors();
139  SG_UNREF(lhs);
140 
141  /* (multiple threads) calculate distances to all cluster centers */
142  SGVector<float64_t> dists(num_clusters);
143  distances_lhs(dists, 0, num_clusters-1, num);
144 
145  /* find cluster index with smallest distance */
146  float64_t result=dists.vector[0];
147  index_t best_index=0;
148  for (index_t i=1; i<num_clusters; ++i)
149  {
150  if (dists[i]<result)
151  {
152  result=dists[i];
153  best_index=i;
154  }
155  }
156 
157  /* implicit cast */
158  return best_index;
159 }
160 
162 {
163  SG_REF(d);
165  distance=d;
166 }
167 
169 {
170  SG_REF(distance);
171  return distance;
172 }
173 
175 {
176  SG_ERROR("store_model_features not yet implemented for %s!\n",
177  get_name());
178 }
179 
CDistance * get_distance() const
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:87
int32_t index_t
Definition: common.h:72
virtual void store_model_features()
CFeatures * get_lhs()
Definition: Distance.h:218
virtual CMulticlassLabels * apply_multiclass(CFeatures *data=NULL)
void distances_rhs(SGVector< float64_t > &result, int32_t idx_b1, int32_t idx_b2, int32_t idx_a)
virtual int32_t get_num_vectors() const =0
#define SG_ERROR(...)
Definition: SGIO.h:128
CFeatures * get_rhs()
Definition: Distance.h:224
Parameter * m_parameters
Definition: SGObject.h:567
void run_distance_rhs(SGVector< float64_t > &result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_a)
Definition: Distance.cpp:219
virtual const char * get_name() const
#define SG_REF(x)
Definition: SGObject.h:52
A generic learning machine interface.
Definition: Machine.h:143
bool set_label(int32_t idx, float64_t label)
Multiclass Labels for multi-class classification.
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:38
virtual void set_store_model_features(bool store_model)
Definition: Machine.cpp:107
#define ASSERT(x)
Definition: SGIO.h:200
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:125
void distances_lhs(SGVector< float64_t > &result, int32_t idx_a1, int32_t idx_a2, int32_t idx_b)
double float64_t
Definition: common.h:60
void run_distance_lhs(SGVector< float64_t > &result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_b)
Definition: Distance.cpp:225
virtual float64_t apply_one(int32_t num)
#define SG_UNREF(x)
Definition: SGObject.h:53
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
void set_distance(CDistance *d)
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Distance.cpp:55

SHOGUN Machine Learning Toolbox - Documentation