百度了半天yusugomori,也不知道他是谁。不过这位老兄写了deep learning的代码,包括RBM、逻辑回归、DBN、autoencoder等,实现语言包括c、c++、java、python等。是学习的好材料。代码下载地址:https://github.com/yusugomori/DeepLearning。不过这位老兄不喜欢写注释,而且这些模型的原理、公式什么的,不了解的话就看不懂代码。我从给他写注释开始,边看资料、边理解它的代码、边给他写上注释。
工具包中RBM的实现包含了两个文件,RBM.h和RBM.cpp。RBM.h添加注释后,如下:
class RBM { public: // the number of training sample int N; // the number of visiable node int n_visible; // the number of hidden node int n_hidden; // the weight connecting the visiable node and the hidden node double **W; // the bias of hidden node double *hbias; // the bias of visiable node double *vbias; public: // construct the RBM by input parameters RBM (int, // N int, // n_visible int, // n_hidden double**, // W double*, // hbias double* // vbias ); // destructor, release all the memory of parameters ~RBM (); // CD-k algorithm to train RBM void contrastive_divergence (int*, // one input sample double, // the learning rate int // the k of CD-k, it is usually 1 ); // these the functions of Gibbs sample // sample the hidden node given the visiable node, 'sample' means calculating // 1. the output probability of the hidden node given the input of visiable node // and the weight of current RBM; 2. the 0-1 state of hidden node by a binomial // distribution given the calculated output probability of this hidden node void sample_h_given_v (int*, // one input sample from visiable nodes -- input double*, // the output probability of hidden nodes -- output int* // the calculated 0-1 state of hidden node -- output ); // sample the visiable node given the hidden node, 'sample' means calculating // 1. the output probability of the visiable node given the input of hidden node // and the weight of current RBM; 2. the 0-1 state of visiable node by a binomial // distribution given the calculated output probability of this visiable node void sample_v_given_h (int*, // one input sample from hidden nodes -- input double*, // the output probability of visiable nodes -- output int* // the calculated 0-1 state of visiable node -- output ); // 'propup' -- probability up. It's called by the 'sample_x_given_x' function and the reconstruct funciton // To calculate the probability in 'upper' node given the input from 'lower' node in RBM // note: what is the 'up' and 'down'? the visiable node is below (down) the hidden node. // 'probability up' means calculating the probability of hidden node given the visiable node // return value: the output probability of the hidden node given the input of visiable node // and the weight of current RBM // the probability is : p (hi|v) = sigmod ( sum_j(vj * wij) + bi) double propup (int*, // one input sample from visiable node -- input double*, // the weight W connecting one hidden node to all visible node -- input double // the bias for this hidden node -- input ); // 'propdown' -- probability down. It's called by the 'sample_x_given_x' function and the reconstruct funciton // To calculate the probability in 'lower' node given the input from 'upper' node in RBM // note: what is the 'up' and 'down'? the visiable node is below (down) the hidden node. // 'probability down' means calculating the probability of visiable node given the hidden node // return value: the output probability of the visiable node given the input of hidden node // and the weight of current RBM // the probability is : p (vi|h) = sigmod ( sum_j(hj * wij) + ci) double propdown (int*, // one input sample from hidden node -- input int, // the index of visiable node in the W matrix -- input double // the bias for this visible node -- input ); // 'gibbs_hvh' -- gibbs sample firstly from hidden node to visible node, then sample // from visiable node to hidden node. It is called by contrastive_divergence. void gibbs_hvh (int*, // one input sample from hidden node, h0 -- input double*, // the output probability of visiable nodes -- output int*, // the calculated 0-1 state of visiable node -- output double*, // the output probability of reconstructed hidden node h1 -- output int* // the calculated 0-1 state of reconstructed hidden node h1 -- output ); // reconstruct the input visiable node by the trained RBM (so as to varify the RBM model) void reconstruct (int*, // one input sample from visiable node double* // the reconstructed output by RBM model ); };
主要添加了函数说明、参数说明、计算说明、调用关系等。