karpathy / char-rnn

Multi-layer Recurrent Neural Networks (LSTM, GRU, RNN) for character-level language models in Torch
11.59k stars 2.58k forks source link

Not able to understand code completely #186

Open jKhurana opened 7 years ago

jKhurana commented 7 years ago

Repository code is correct but I have a lot of doubts while understanding the code. May be my doubts are very silly as I recently started learning deep models. Following are my doubts:

-- make a bunch of clones after flattening, as that reallocates memory
clones = {}
for name,proto in pairs(protos) do
    print('cloning ' .. name)
    clones[name] = model_utils.clone_many_times(proto, opt.seq_length, not proto.parameters)
end
    for t=1,opt.seq_length do
        clones.rnn[t]:training() -- make sure we are in correct mode (this is cheap, sets flag)
        **local lst = clones.rnn[t]:forward{x[t], unpack(rnn_state[t-1])}**
        rnn_state[t] = {}
        for i=1,#init_state do table.insert(rnn_state[t], lst[i]) end -- extract the state, without output
        predictions[t] = lst[#lst] -- last element is the prediction
        loss = loss + clones.criterion[t]:forward(predictions[t], y[t])
    end

I searched on internet about this a lot but not found any good resource to understand. I know it is not an issue but still posting here. Please help me to understand it. Thanks in advance

squidszyd commented 7 years ago

For question 1 ~ 4: