GPUMLib  0.2.2
GPU Machine Learning Library
RadialBasisFunction.cpp
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 #include "RadialBasisFunction.h"
22 
23 namespace GPUMLib {
24 
25 RadialBasisFunction::RadialBasisFunction(int network_size, int number_neighbours, float scaling_factor, int NumClasses){
26 
27  this->network_size = network_size;
28  this->number_neighbours = number_neighbours;
29  this->seed = (unsigned)time(0);
30  this->scaling_factor = scaling_factor;
31 
32  this->NumClasses = NumClasses;
33 
34  this->start = 0;
35  for(int i = 0; i < 4; i++) times[i] = 0;
36 
37  device_c_width = DeviceArray<float>(network_size);
38 
39 }
40 
41 RadialBasisFunction::~RadialBasisFunction(){
42 
43 }
44 
46 
47  DeviceMatrix<float> dev_input(Input);
48 
49  DeviceMatrix<float> device_activ_matrix(dev_input.Rows(),dCenters.Rows(),ColumnMajor);
50 
51  KernelActivationMatrix(device_activ_matrix.Pointer(),dev_input.Pointer(),dCenters.Pointer(),dev_input.Columns(),dCenters.Columns(),device_activ_matrix.Columns(),device_activ_matrix.Rows(),scaling_factor,device_c_width.Pointer());
52 
53  DeviceMatrix<float> d_Output(device_activ_matrix.Rows(),dWeights.Columns(),ColumnMajor);
54  device_activ_matrix.Multiply(device_activ_matrix,dWeights,d_Output);
55  HostMatrix<float> Output(d_Output);
56 
57  return Output;
58 }
59 
61 
62  DeviceMatrix<float> device_activ_matrix(Input.Rows(),dCenters.Rows(),ColumnMajor);
63 
64  KernelActivationMatrix(device_activ_matrix.Pointer(),Input.Pointer(),dCenters.Pointer(),Input.Columns(),dCenters.Columns(),device_activ_matrix.Columns(),device_activ_matrix.Rows(),scaling_factor,device_c_width.Pointer());
65 
66  DeviceMatrix<float> d_Output(device_activ_matrix.Rows(),dWeights.Columns(),ColumnMajor);
67  device_activ_matrix.Multiply(device_activ_matrix,dWeights,d_Output);
68 
69  return d_Output;
70 }
71 
73 
74  DeviceMatrix<float> device_activ_matrix(Input.Columns(),Centers.Rows(),ColumnMajor);
75 
76  KernelActivationMatrix(device_activ_matrix.Pointer(),Input.Pointer(),Centers.Pointer(),Input.Columns(),Centers.Columns(),device_activ_matrix.Columns(),device_activ_matrix.Rows(),scaling_factor,Widths.Pointer());
77 
78  DeviceMatrix<float> d_Output(device_activ_matrix.Rows(),Weights.Columns(),ColumnMajor);
79  device_activ_matrix.Multiply(device_activ_matrix,Weights,d_Output);
80 
81  return d_Output;
82 
83 }
84 
86 
87  //std::cout << "Training" << std::endl;
88 
89  // c_width = (float*) malloc(sizeof(float)*network_size);
90  // memset(c_width,0,sizeof(float)*network_size);
91 
92  DeviceMatrix<float> device_X(Input);
93 
94  //std::cout << "KMeans" << std::endl;
95  clock_t initialTime = clock();
96  KMeans KM;
97  KM.SetSeed(seed);
98  dCenters = KM.Execute(device_X,network_size);
99 
100  cudaThreadSynchronize();
101  times[0] = (clock() - initialTime);
102 
103  //std::cout << "Adjust Widths" << std::endl;
104  /*Adjust width using mean of distance to neighbours*/
105  initialTime = clock();
106  AdjustWidths(number_neighbours);
107 
108  cudaThreadSynchronize();
109  times[1] = (clock() - initialTime);
110 
111  /*Training weights and scaling factor*/
112  HostMatrix<float> TargetArr(Target.Rows(),NumClasses);
113  memset(TargetArr.Pointer(),0,sizeof(float)*TargetArr.Elements());
114 
115  for(int i = 0; i < Target.Rows(); i++){
116  TargetArr(i,((int)Target(i,0)-1)) = 1;
117  }
118 
119  DeviceMatrix<float> d_Target(TargetArr);
120 
121  //std::cout << "Calculating Weights" << std::endl;
122 
123  initialTime = clock();
124 
125  DeviceMatrix<float> device_activ_matrix(device_X.Rows(),dCenters.Rows(),ColumnMajor);
126 
127  KernelActivationMatrix(device_activ_matrix.Pointer(),device_X.Pointer(),dCenters.Pointer(),device_X.Columns(),dCenters.Columns(),device_activ_matrix.Columns(),device_activ_matrix.Rows(),scaling_factor,device_c_width.Pointer());
128 
129  DeviceMatrix<float> d_Aplus = UTILS::pseudoinverse(device_activ_matrix);
130 
131  dWeights = DeviceMatrix<float>(d_Aplus.Rows(),d_Target.Columns());
132 
133  d_Aplus.Multiply(d_Aplus,d_Target,dWeights);
134 
135 
136  /*Return Weights and Centers*/
137  cudaThreadSynchronize();
138  times[2] = (clock() - initialTime);
139 
140  // cudaMemcpy(c_width,device_c_width.Pointer(),sizeof(float)*device_c_width.Length(),cudaMemcpyDeviceToHost);
141  // this->Weights = HostMatrix<float>(dWeights);
142  // this->Centers = HostMatrix<float>(dCenters);
143 
144 }
145 
146 void RadialBasisFunction::AdjustWidths(int rneighbours){
147  DeviceMatrix<float> device_output2(dCenters.Rows(),dCenters.Rows());
148  KernelCalculateDistance(device_output2.Pointer(), dCenters.Pointer(), dCenters.Pointer(),dCenters.Columns(),dCenters.Columns(),device_output2.Columns(),device_output2.Rows());
149  cudaMemset(device_c_width.Pointer(),0,sizeof(float)*device_c_width.Length());
150  KernelAdjustWidths(device_output2.Pointer(),device_output2.Rows(), device_output2.Columns(),rneighbours,device_c_width.Pointer());
151 }
152 
153 }
void KernelAdjustWidths(cudafloat *Distance, int distance_height, int distance_width, int rneighbours, float *widths)
Definition: RBFKernels.cu:59
DeviceMatrix< float > Execute(DeviceMatrix< float > &Input, int kneighbours)
Definition: KMeans.cpp:155
Type * Pointer() const
Definition: BaseArray.h:70
void AdjustWidths(int rneighbours)
Type * Pointer() const
Definition: BaseMatrix.h:88
int Columns() const
Definition: BaseMatrix.h:80
Represents a clustering algorithm using the K-Means technique, implemented in CUDA.
Definition: KMeans.h:58
int Length() const
Definition: BaseArray.h:63
RadialBasisFunction(int network_size, int number_neighbours, float scaling_factor, int NumClasses)
static void Multiply(DeviceMatrix< Type > &A, DeviceMatrix< Type > &B, DeviceMatrix< Type > &C, cudafloat alpha=CUDA_VALUE(1.0), cudafloat beta=CUDA_VALUE(0.0))
Definition: DeviceMatrix.h:192
int Rows() const
Definition: BaseMatrix.h:74
void Train(HostMatrix< float > &Input, HostMatrix< float > &Target)
HostMatrix< float > Test(HostMatrix< float > &Input)