GPUMLib  0.2.2
GPU Machine Learning Library
HostDBN.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_DBNH_h
22 #define GPUMLib_DBNH_h
23 
24 #include "../RBM/HostRBM.h"
25 
26 namespace GPUMLib {
27 
30 
32 class DBNhost {
33  private:
35 
36  public:
44  DBNhost(HostArray<int> & layers, HostMatrix<cudafloat> & inputs, cudafloat initialLearningRate, cudafloat momentum = DEFAULT_MOMENTUM, bool useBinaryValuesVisibleReconstruction = false, cudafloat stdWeights = STD_WEIGHTS) {
45  int nlayers = layers.Length();
46  assert(nlayers >= 2);
47 
48  rbms.ResizeWithoutPreservingData(nlayers - 1);
49 
50  rbms[0] = new RBMhost(layers[0], layers[1], inputs, initialLearningRate, momentum, useBinaryValuesVisibleReconstruction, stdWeights);
51 
52  for(int r = 1; r < nlayers - 1; r++) {
53  rbms[r] = new RBMhost(rbms[r - 1], layers[r + 1], initialLearningRate, momentum, useBinaryValuesVisibleReconstruction, stdWeights);
54  }
55  }
56 
57  ~DBNhost() {
58  int nlayers = rbms.Length();
59 
60  for(int l = 0; l < nlayers; l++) {
61  delete rbms[l];
62  rbms[l] = nullptr;
63  }
64  }
65 
69  RBMhost * GetRBM(int layer) {
70  assert(layer < rbms.Length());
71  return rbms[layer];
72  }
73 
76  int GetNumberRBMs() const {
77  return rbms.Length();
78  }
79 
83  void Train(int epochs, int cd) {
84  int nlayers = rbms.Length();
85  for(int l = 0; l < nlayers; l++) {
86  RBMhost * layer = rbms[l];
87  if (l > 0) layer->RandomizeWeights();
88 
89  for (int e = 0; e < epochs; e++) layer->ContrastiveDivergence(cd);
90  layer->ComputeStatusHiddenUnits(*(layer->v), layer->h_data);
91  }
92  }
93 
98  void Train(int epochs, int cd, cudafloat errorStop) {
99  int nlayers = rbms.Length();
100 
101  for(int l = 0; l < nlayers; l++) {
102  RBMhost * layer = rbms[l];
103  if (l > 0) layer->RandomizeWeights();
104 
105  for (int e = 0; e < epochs; e++) {
106  layer->ContrastiveDivergence(cd);
107  cudafloat error = layer->MeanSquareError();
108  if (error < errorStop) break;
109  }
110  layer->ComputeStatusHiddenUnits(*(layer->v), layer->h_data);
111  }
112  }
113 };
114 
117 
119 
120 }
121 
122 #endif
Represents a Deep Belief Network (Host - CPU).
Definition: HostDBN.h:32
void Train(int epochs, int cd, cudafloat errorStop)
Definition: HostDBN.h:98
Create an array of any type, on the host, that automatically manages the memory used to hold its elem...
Definition: HostArray.h:40
void Train(int epochs, int cd)
Definition: HostDBN.h:83
void RandomizeWeights(cudafloat stdWeights)
Definition: HostRBM.h:162
int Length() const
Definition: BaseArray.h:63
cudafloat MeanSquareError() const
Definition: HostRBM.h:335
RBMhost * GetRBM(int layer)
Definition: HostDBN.h:69
int GetNumberRBMs() const
Definition: HostDBN.h:76
float cudafloat
Represents a Restricted Boltzman Machine (Host - CPU).
Definition: HostRBM.h:39
DBNhost(HostArray< int > &layers, HostMatrix< cudafloat > &inputs, cudafloat initialLearningRate, cudafloat momentum=DEFAULT_MOMENTUM, bool useBinaryValuesVisibleReconstruction=false, cudafloat stdWeights=STD_WEIGHTS)
Definition: HostDBN.h:44