LSTM-matlab -master程序的理解

I download this LSTM-matlab package from github, i will give the website later.

主要目的是:整理自己理解这个程序的整个流程,记录下来,方便自己回顾和分析,写给自己。

1. 待解决的问题:

问题一:
-该程序里,数据是如何产生的,是什么样的形式,比如维度
-每次运行程序,数据是如何作为输入进入网络的,比如,一个一个,还是以batch为单位进入输入层的。
问题二:
-网络是怎么产生的,结构是什么,比如几层,隐藏层有多少节点。数据的流向
问题三:
-各个参数的意义,以及参数是如何迭代的

2. 目前理解的程序的运行流程

目前知道的要用到的程序:
——————————————————————————————————*

2.1 aStart.m

作用:设置result,speed,options,problem,sgd各个结构体的值,也是程序的一些基本参数,在最后一行调用Main(result,speed,options,problem,sgd)函数。
——————————————————————————————————*

2.2 Main()的主要流程:
2.2.1 根据cpu的种类,得到产生参数的基本的匿名函数

[mones,mzeros,convert,usegpu]=gputype(speed.usegpu);

        mones = @(varargin)ones(varargin{:}, 'double');  % n行m列的一个单位矩阵
        mzeros = @(varargin)zeros(varargin{:}, 'double');   % n行m列的一个0矩阵
        convert = @(x)x;        
        useG=0;
2.2.2 产生数据
% genadding函数产生数据
[data,mask,test,masktest ]=feval(['gen' problem.name],problem);
% problem.name='adding';
[data,mask,test,masktest]= genadding(problem)

data=mzeros(problem.numsamples, 4 , ceil(problem.T*1.1));
mask=mzeros(problem.numsamples, 1 , ceil(problem.T*1.1));
test=mzeros(problem.numtest, 4 , ceil(problem.Ttest*1.1));
masktest=mzeros(problem.numtest, 1 , ceil(problem.Ttest*1.1));
problem.T = 8
> data size: 20000 * 4 * 7
> mask size: 20000 * 1 * 9
> test size: 3000  * 4 * 8
> masktest size:3000 * 1 * 9
> 
% 其中 data
length = length = T+ fix(rand  * T / 10) = 7
data(i,3,1:length)=ones(1, length);                  %bias
data(i,1,1:length)=2*rand(1,length    ) - 1
data(i,4,1:length)=ones(1,length);
data(i,2,tmp1)=1;                                   % or -1
% mask
mask=mzeros(problem.numsamples, 1 , ceil(problem.T*1.1));     %(20000 * 1 * 9)
mask(i,1,length)=1;                           % val(:,:,7) = 全部为1,其他,全部为0

%test
test(i,3,1:length)=ones(1, length);                %bias    
test(i,1,1:length)=2*rand(1,length    ) - 1;
test(i,4,1:length) = ones(1,length);               %bias
test(i,2,tmp2) = 1;                             %  or -1
finnal_test = [test(:,:,1) test(:,:,1:length)]  %  重点:这是与data的不同点,共8列,第一列重复原来的第一列,从dada的20000 * 4 * 7,变为test的(3000  * 4 * 8)
% masktets
masktest=mzeros(problem.numtest, 1 , ceil(problem.Ttest*1.1));  %  3000 *1 *9
masktest(i,1,length)=1;                                    %  3000 *1 *9
masktest=masktest(:,:,1:size(test,3));                     %  3000 *1 *8
finnal_masktest = [masktest(:,:,1) masktest(:,:,1:length)]  %  3000 *1 *9

想不太明白的地方:
函数句柄convert = @(x)x;
经过这个函数句柄处理前后的两个数组,完全一样。
所以不太明白 已经生成 data、test这些数据集之后,再经过convert函数句柄处理之后的意义
% 在genadding.m的函数中
data=convert(data);
mask=convert(mask);
test=convert(test);
masktest=convert(masktest);

2.2.3 初始化网络 netInit()

一共有几层网络,每层网络各有多少个节点呢 ???
[in_size,gate_size,out_size,share_size,share_size2,numMmcell,W,in,ingate,cellstate,cells,outgate,…
node_outgateInit,cellinInit,node_cellbiasInit,delta_outInit,cellstatusInit ]=netInit(problem);

in_size                    = 2  % 后来变为in_size + bias1 = 3
gate_size                  = 2  %  problem.gate_size
out_size                   = 1

% share_size2 is weights for a layer of lstm, 
% lstm has 4 layer:      ingate, cell, outgate, out     ,      added up to psize
% share_size include:    ingate, status, outgate, cellout 
share_size                = 15  % in_size + gate_size * (2+2*problem.numMmcell) ; 
share_size2               = 2*share_size            
numMmcell                 = 2
W
in              (1,2,3)         size = 3
ingate          (4,5)           size = 2  % gate_size * 1
cellstate       (6,7,8,9)       size = 4  % gate_size * numMmcell
cells           (10,11,12,13)   size = 4  % gate_size * numMmcell
outgate         (14,15)         size = 2  % gate_size * 1
% 一下5个矩阵,全部初始化为cell(1,8)的[](problem.T), 
% 然后每个矩阵的每个,cell{i},分别进行如下操作
node_outgateInit   % 0.5*mones(200,gate_size) 
cellinInit         %  mzeros(200,numMmcell*gate_size)   % cells   (10,11,12,13) 
node_cellbiasInit  %  mones(200,1);
delta_outInit      % mzeros(200,out_size);
cellstatusInit     % mzeros(200,numMmcell*gate_size);   % 对应cellstate   (6,7,8,9)

bias1=1 ;          % other bias
bias2=12 ;         % output bias
ingatebias = -1;   % 难道是 输入 门 的bias

此函数的功能:
生成网络结果,对 各层网络的权重矩阵 进行初始化。包括两部分:
1.确定各层网络权重矩阵的维度,m*n中m和n的值,
2.确定各个矩阵的初始值

到目前为止,还没有搞明白网络层,输入输出状态cell等这些层的具体是怎么回事、以及各个层的意义,输入输出。
所以,我尽量把每个变量的值写出来,尽量根据变量值来把各个变量的意义分成一类,然后看能否整理出些内容

W =   1e-4 *  (mod( fix( 2^50  *  rand(psize,1    ) ) , 2000 )    -1000);
for i=1:psize /gate_size
    W( ceil(rand*psize) )=0;
end
% 随机把一般的权值赋值为0,一半(125/2)
share_size   = in_size + gate_size * (2+2*problem.numMmcell) ;  % = 15 
% share_size = in_size + gate_size + gate_size + gate_size*numMmcell + gate_size*numMmcell; 
%              in_size + ingate    + outgate   + cellstate           + cells    
Wingate    15*2   %  share_size ,gate_size
Wcell      15*4   %  share_size, numMmcell*gate_size
Woutgate   15*2   %  share_size , gate_size
Wout       5*1    %  gate_size*numMmcell+1 ,  out_size
2.2.4 开始优化,更新w和cost,

[W, cost] = minFunc( Bpfunc,W,options);

Bpfunc=@batch_cell_lstm;
options.Method = 'lbfgs';
[W, cost] = minFunc( Bpfunc,W,options);
if toc(timer)>2
    timearray{1}( record  )= toc(timer);
    [errorarray{1}(record),~,~,rightarray{1}(record) ]=testmodel(W,test,masktest ,problem);
    disp([ 'lbfgs error '  num2str(errorarray{1}(record) )]  )
    record=record+1;
    timer=tic;
end

for repeat =1 :5
        for inner =1:50
            for i=1:fix(problem.numsamples/problem.batchsize) 
                globalData =  bashdata{i};
                globalMask= maskdata{i};

                [~,dw,~,~]=Bpfunc(W);     
                oldGradient = alpha*dw + momentum* oldGradient;
                W= W - alpha* oldGradient;
            end
            timearray{2}(record  )= toc(timer);
            [errorarray{2}(record),~,~, rightarray{2}(record) ]=testmodel(W,test,masktest ,problem);
            record=record+1;
            timer=tic;
        end
        disp([ 'sgd error '  num2str(errorarray{2}(record-1) )]  )
    end

关键问题:
1. batch_cell_lstm
2. minFunc 非线性优化的包
3. testmodel

% 分别是什么东西,怎求的
errorarray{1}(record)  rightarray{1}(record) 
errorarray{2}(record)  rightarray{2}(record) 
  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值