CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

How to build BLSTM applying NDL

Open minghuaiping opened this issue 10 years ago • 10 comments

I want to build a bidirectional-LSTM (BLSTM) with network definition language (NDL). There is an example shows how to build LSTM, but not BLSTM. Anyone can help? It is appreciated if an example of build LSTM using NDL can be shared.

Best, Huaiping

minghuaiping avatar Apr 08 '16 07:04 minghuaiping

You can use FutureValue node (replace PastValue by FutureValue in the NDL). It will reverse the sequence automatically.

yzhang87 avatar Apr 08 '16 17:04 yzhang87

Here is an example:

layer 1

LSTMForwardOutput1 = LSTMPComponentForward(baseFeatDim, hiddenDim, cellDim, featNorm);
LSTMBackwardOutput1 = LSTMPComponentBackward(baseFeatDim, hiddenDim, cellDim, featNorm);
LSTMoutput1 = RowStack(LSTMForwardOutput1, LSTMBackwardOutput1)

LSTMPComponentBackward(inputDim, outputDim, cellDim, inputx)
{
    Wxo = Parameter(cellDim, inputDim, init=uniform, initValueScale=1);
    Wxi = Parameter(cellDim, inputDim, init=uniform, initValueScale=1);
    Wxf = Parameter(cellDim, inputDim, init=uniform, initValueScale=1);
    Wxc = Parameter(cellDim, inputDim, init=uniform, initValueScale=1);

    bo = Parameter(cellDim, init=fixedValue, value=0.0);
    bc = Parameter(cellDim, init=fixedValue, value=0.0);
    bi = Parameter(cellDim, init=fixedValue, value=0.0);
    bf = Parameter(cellDim, init=fixedValue, value=0.0);

    Whi = Parameter(cellDim, outputDim, init=uniform, initValueScale=1);

    Wci = Parameter(cellDim, init=uniform, initValueScale=1);


    Whf = Parameter(cellDim, outputDim, init=uniform, initValueScale=1);
    Wcf = Parameter(cellDim, init=uniform, initValueScale=1);
    Who = Parameter(cellDim, outputDim, init=uniform, initValueScale=1);
    Wco = Parameter(cellDim, init=uniform, initValueScale=1);
    Whc = Parameter(cellDim, outputDim, init=uniform, initValueScale=1);

    dh = FutureValue(outputDim, output, timeStep=1);
    dc = FutureValue(cellDim, ct, timeStep=1);


    Wxix = Times(Wxi, inputx);
    Whidh = Times(Whi, dh);
    Wcidc = DiagTimes(Wci, dc);

    it = Sigmoid (Plus ( Plus (Plus (Wxix, bi), Whidh), Wcidc));

    Wxcx = Times(Wxc, inputx);
    Whcdh = Times(Whc, dh);
    bit = ElementTimes(it, Tanh( Plus(Wxcx, Plus(Whcdh, bc))));

    Wxfx = Times(Wxf, inputx);
    Whfdh = Times(Whf, dh);
    Wcfdc = DiagTimes(Wcf, dc);

    ft = Sigmoid( Plus (Plus (Plus(Wxfx, bf), Whfdh), Wcfdc));

    bft = ElementTimes(ft, dc);

    ct = Plus(bft, bit);

    Wxox  = Times(Wxo, inputx);
    Whodh = Times(Who, dh);
    Wcoct = DiagTimes(Wco, ct);

    ot = Sigmoid( Plus( Plus( Plus(Wxox, bo), Whodh), Wcoct));

    mt = ElementTimes(ot, Tanh(ct));

    Wmr = Parameter(outputDim, cellDim, init=uniform, initValueScale=1);
    output = Times(Wmr, mt); 

}

yzhang87 avatar Apr 08 '16 17:04 yzhang87

Thank you very much! It really helps!

minghuaiping avatar Apr 09 '16 05:04 minghuaiping

I have already built a BLSTM using the futurevalue node. But it cause an error, have you ever meet this error? EXCEPTION occurred: The delay node tries to access future values that are out of bound, possibly because there is no sentence end marker in the MBLayout.

oscarzbz avatar Apr 11 '16 02:04 oscarzbz

make sure truncated is set to false

dongyu888 avatar Apr 11 '16 02:04 dongyu888

I have change the set of truncated just now , the original error disappeared , but a new one occurred: EXCEPTION occurred: The training criterion is not a number (NAN). Stop

oscarzbz avatar Apr 11 '16 03:04 oscarzbz

Learning rate is too high because now gradients are computed for whole utterances (i.e., more samples).

dongyu888 avatar Apr 11 '16 03:04 dongyu888

Thanks alot ,I got it.

oscarzbz avatar Apr 11 '16 03:04 oscarzbz

I assume this got answered by Dong ...

wolfma61 avatar Apr 13 '16 15:04 wolfma61

@yzhang87 Do you know how to build a BLSTM in caffe?

xiaozeyuan avatar Feb 20 '17 10:02 xiaozeyuan