From b26f0b224fe27cbb6af8367c1e6bb472119d57d6 Mon Sep 17 00:00:00 2001 From: Yusuke Sugomori Date: Mon, 11 Feb 2013 22:25:58 +0900 Subject: [PATCH] DBN --- .gitignore | 3 +- cpp/DBN.cpp | 545 ++++++++++++++++++++++++++++++++++++++++++++ cpp/DBN.h | 17 ++ cpp/HiddenLayer.cpp | 81 +++++++ cpp/HiddenLayer.h | 13 ++ cpp/utils.cpp | 25 ++ 6 files changed, 683 insertions(+), 1 deletion(-) create mode 100644 cpp/DBN.cpp create mode 100644 cpp/DBN.h create mode 100644 cpp/HiddenLayer.cpp create mode 100644 cpp/HiddenLayer.h create mode 100644 cpp/utils.cpp diff --git a/.gitignore b/.gitignore index 0c52d43..51df09e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store *.pyc -*.out \ No newline at end of file +*.out +*.o \ No newline at end of file diff --git a/cpp/DBN.cpp b/cpp/DBN.cpp new file mode 100644 index 0000000..8a25e2e --- /dev/null +++ b/cpp/DBN.cpp @@ -0,0 +1,545 @@ +#include +#include +#include "HiddenLayer.h" +#include "RBM.h" +#include "LogisticRegression.h" +#include "DBN.h" +using namespace std; + + +double uniform(double min, double max) { + return rand() / (RAND_MAX + 1.0) * (max - min) + min; +} + +int binomial(int n, double p) { + if(p < 0 || p > 1) return 0; + + int c = 0; + double r; + + for(int i=0; iW, sigmoid_layers[i]->b, NULL); + } + + // layer for output using LogisticRegression + log_layer = new LogisticRegression(N, hidden_layer_sizes[n_layers-1], n_outs); +} + +DBN::~DBN() { + delete log_layer; + + for(int i=0; isample_h_given_v(prev_layer_input, layer_input); + delete[] prev_layer_input; + } + } + + rbm_layers[i]->contrastive_divergence(layer_input, lr, k); + } + + } + } + + delete[] train_X; + delete[] layer_input; +} + +void DBN::finetune(int *input, int *label, double lr, int epochs) { + int *layer_input; + int prev_layer_input_size; + int *prev_layer_input; + + int *train_X = new int[n_ins]; + int *train_Y = new int[n_outs]; + + for(int epoch=0; epochsample_h_given_v(prev_layer_input, layer_input); + delete[] prev_layer_input; + } + + log_layer->train(layer_input, train_Y, lr); + } + // lr *= 0.95; + } + + delete[] layer_input; + delete[] train_X; + delete[] train_Y; +} + +void DBN::predict(int *x, double *y) { + double *layer_input; + int prev_layer_input_size; + double *prev_layer_input; + + double linear_output; + + prev_layer_input = new double[n_ins]; + for(int j=0; jn_out]; + + linear_output = 0.0; + for(int k=0; kn_out; k++) { + for(int j=0; jn_in; j++) { + linear_output += sigmoid_layers[i]->W[k][j] * prev_layer_input[j]; + } + linear_output += sigmoid_layers[i]->b[k]; + layer_input[k] = sigmoid(linear_output); + } + delete[] prev_layer_input; + + if(i < n_layers-1) { + prev_layer_input = new double[sigmoid_layers[i]->n_out]; + for(int j=0; jn_out; j++) prev_layer_input[j] = layer_input[j]; + delete[] layer_input; + } + } + + for(int i=0; in_out; i++) { + for(int j=0; jn_in; j++) { + y[i] += log_layer->W[i][j] * layer_input[j]; + } + y[i] += log_layer->b[i]; + } + + log_layer->softmax(y); + + + delete[] layer_input; +} + + +// HiddenLayer +HiddenLayer::HiddenLayer(int size, int in, int out, double **w, double *bp) { + N = size; + n_in = in; + n_out = out; + + if(w == NULL) { + W = new double*[n_out]; + for(int i=0; i +#include +#include "HiddenLayer.h" +using namespace std; + +double uniform(double min, double max) { + return rand() / (RAND_MAX + 1.0) * (max - min) + min; +} + +int binomial(int n, double p) { + if(p < 0 || p > 1) return 0; + + int c = 0; + double r; + + for(int i=0; i +#include +using namespace std; + +double uniform(double min, double max) { + return rand() / (RAND_MAX + 1.0) * (max - min) + min; +} + +int binomial(int n, double p) { + if(p < 0 || p > 1) return 0; + + int c = 0; + double r; + + for(int i=0; i