GPUMLib  0.2.2
GPU Machine Learning Library
ResourceAllocatingNetwork.h
1 /*
2  Ricardo Quintas is an MSc Student at the University of Coimbra, Portugal
3  Copyright (C) 2009, 2010 Ricardo Quintas
4 
5  This file is part of GPUMLib.
6 
7  GPUMLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef ResourceAllocatingNetwork_h
22 #define ResourceAllocatingNetwork_h
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include <vector>
27 #include <algorithm>
28 
29 #include <cuda.h>
30 #include <cuda_runtime.h>
31 
32 #include "../common/CudaDefinitions.h"
33 #include "../common/CudaStreams.h"
34 #include "../memory/HostMatrix.h"
35 #include "../memory/DeviceMatrix.h"
36 #include "../memory/DeviceAccessibleVariable.h"
37 #include "../RAN/rankernels.h"
38 
39 #include "../RAN/utils.h"
40 
41 #include <ctime>
42 
43 #define imin(X, Y) ((X) < (Y) ? (X) : (Y))
44 #define round(x) (x<0?ceil((x)-0.5):floor((x)+0.5))
45 
46 using namespace std;
47 
48 namespace GPUMLib {
49 
52 
55 
56 private:
57 
58  float scale_of_interest;
59  float scale_of_interest_max;
60  float scale_of_interest_min;
61 
62  float decay;
63 
64  float alpha;
65  float desired_accuracy;
66 
67 
68  float overlap_factor;
69 
70  HostMatrix<float> Input;
71  HostMatrix<float> Target;
72 
73  DeviceMatrix<float> dCenters;
74  DeviceMatrix<float> dIMemory;
75  DeviceArray<float> dTMemory;
76  DeviceMatrix<float> dWeights;
77  DeviceArray<float> dWidths;
78 
79 
80  HostMatrix<float> Centers;
81  HostArray<float> Weights;
82  HostArray<float> Widths;
83 
84  HostMatrix<float> Weights_2;
85  HostMatrix<float> TMemory_2;
86  DeviceMatrix<float> dTMemory_2;
87 
88  HostMatrix<float> IMemory;
89  HostArray<float> TMemory;
90 
91  int NumClasses;
92 
93  int NumCenters;
94  int NumMemoryItems;
95  int Columns;
96  float MaxWidth;
97 
98  int* CounterCenters;
99 
100  float* derror;
101  float* ddistance;
102  float* herror;
103  float* hdistance;
104 
105  float* result;
106 
108  DeviceAccessibleVariable<float> var_distance;
109 
110  CudaStream stream1;
111 
112  DeviceArray<float> Distances;
113  DeviceArray<float> error_arr;
114 
115  void FindNearestCenter(cudafloat* Sample, int Length,float *value, int* index,cudafloat *output);
116  void AddCenter(cudafloat* Sample, int Length, float* Width, float* Weight);
117  void AddMemory(cudafloat* Sample, int Length, float* Target);
118  void UpdateWeights(cudafloat* Sample, int Length, float* Target);
119 
120 public:
121 
122  unsigned int start;
123  unsigned int times[4];
124 
125  float center_time;
126  float width_time;
127  float weight_time;
128  float scaling_time;
129 
137  ResourceAllocatingNetwork(float scale_of_interest_max, float desired_accuracy, float overlap_factor, int Rows, int Columns, int NumClasses);
139 
145  void Train(cudafloat* Sample, int Length,float Target, float* dTargetArr);
146 
151  float FindMaxWidth(DeviceMatrix<float> &X,DeviceMatrix<float> &Y);
152 
157  float* CalculateNetworkActivation(cudafloat* Sample, int Length);
158 
159  int GetNumCenters(){
160  return NumCenters;
161  }
162 };
163 
166 
168 
169 }
170 
171 #endif
Represents a CUDA stream.
Definition: CudaStreams.h:32
Represents a resource allocating network with long term memory that can be trained using a CUDA imple...
float cudafloat