GPUMLib  0.2.2
GPU Machine Learning Library
DBN.h
1 /*
2  Noel Lopes is an Assistant Professor at the Polytechnic Institute of Guarda, Portugal
3  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Noel de Jesus Mendonša Lopes
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 GPUMLib_DBN_h
22 #define GPUMLib_DBN_h
23 
24 #include "../RBM/RBM.h"
25 #include "../reduction/reduction.h"
26 
27 namespace GPUMLib {
28 
31 
33 class DBN {
34  private:
35  HostArray<RBM *> rbms;
36 
37  public:
45  DBN(HostArray<int> & layers, HostMatrix<cudafloat> & inputs, cudafloat initialLearningRate, cudafloat momentum = DEFAULT_MOMENTUM, bool useBinaryValuesVisibleReconstruction = false, cudafloat stdWeights = STD_WEIGHTS) {
46  int nlayers = layers.Length();
47  assert(nlayers >= 2);
48 
49  rbms.ResizeWithoutPreservingData(nlayers - 1);
50 
51  rbms[0] = new RBM(layers[0], layers[1], inputs, initialLearningRate, momentum, useBinaryValuesVisibleReconstruction, stdWeights);
52 
53  for(int r = 1; r < nlayers - 1; r++) {
54  rbms[r] = new RBM(rbms[r - 1], layers[r + 1], initialLearningRate, momentum, useBinaryValuesVisibleReconstruction, stdWeights);
55  }
56  }
57 
58  ~DBN() {
59  int nlayers = rbms.Length();
60 
61  for(int l = 0; l < nlayers; l++) {
62  delete rbms[l];
63  rbms[l] = nullptr;
64  }
65  }
66 
70  // //int nlayers = rbms.Length();
71  // //for(int l = 0; l < nlayers; l++) rbms[l]->RandomizeWeights(stdWeights, initialLearningRate);
73 
77  RBM * GetRBM(int layer) {
78  assert(layer < rbms.Length());
79  return rbms[layer];
80  }
81 
84  int GetNumberRBMs() const {
85  return rbms.Length();
86  }
87 
94  bool Train(int epochs, int cd, int miniBatchSize, cudafloat errorStop = CUDA_VALUE(0.0)) {
95  RBM * layer;
96 
97  int nlayers = rbms.Length();
98  for(int l = 0; l < nlayers; l++) {
99  layer = rbms[l];
100  if (!layer->Init(miniBatchSize, cd)) return false;
101 
102  for (int e = 0; e < epochs; e++) {
103  layer->ContrastiveDivergence(cd);
104  if (errorStop != CUDA_VALUE(0.0) && layer->GetMSEestimate() < errorStop) break;
105  }
106  }
107 
108  layer->DisposeDeviceInformation();
109 
110  return true;
111  }
112 };
113 
116 
118 
119 }
120 
121 #endif
void DisposeDeviceInformation()
Definition: RBM.h:194
void ContrastiveDivergence(int n)
Definition: rbm.cu:87
int GetNumberRBMs() const
Definition: DBN.h:84
cudafloat GetMSEestimate()
Definition: RBM.h:258
Create an array of any type, on the host, that automatically manages the memory used to hold its elem...
Definition: HostArray.h:40
DBN(HostArray< int > &layers, HostMatrix< cudafloat > &inputs, cudafloat initialLearningRate, cudafloat momentum=DEFAULT_MOMENTUM, bool useBinaryValuesVisibleReconstruction=false, cudafloat stdWeights=STD_WEIGHTS)
Definition: DBN.h:45
int Length() const
Definition: BaseArray.h:63
Represents a Deep Belief Network (Device - GPU).
Definition: DBN.h:33
RBM * GetRBM(int layer)
Definition: DBN.h:77
#define CUDA_VALUE(X)
Represents a Restricted Boltzman Machine (GPU).
Definition: RBM.h:44
float cudafloat
bool Train(int epochs, int cd, int miniBatchSize, cudafloat errorStop=CUDA_VALUE(0.0))
Definition: DBN.h:94
bool Init(int miniBatchSize, int cd)
Definition: RBM.h:152