Skip to content

Commit

Permalink
Added oneHot encoding and softmax
Browse files Browse the repository at this point in the history
  • Loading branch information
guillitte committed Jul 31, 2015
1 parent 920ef81 commit c54a856
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions model/RNN.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
local RNN = {}

function RNN.rnn(input_size, rnn_size, n)
function RNN.rnn(input_size, rnn_size, n, dropout)

-- there are n+1 inputs (hiddens on each layer and x)
local inputs = {}
table.insert(inputs, nn.Identity()()) -- x
for L = 1,n do
table.insert(inputs, nn.Identity()()) -- prev_h[L]

end

local x, input_size_L
local outputs = {}
for L = 1,n do

local prev_h = inputs[L+1]
if L == 1 then x = inputs[1] else x = outputs[L-1] end
if L == 1 then input_size_L = input_size else input_size_L = rnn_size end
if L == 1 then
x = OneHot(input_size)(inputs[1])
input_size_L = input_size
else
x = outputs[(L-1)]
if dropout > 0 then x = nn.Dropout(dropout)(x) end -- apply dropout, if any
input_size_L = rnn_size
end

-- RNN tick
local i2h = nn.Linear(input_size_L, rnn_size)(x)
Expand All @@ -24,6 +31,12 @@ function RNN.rnn(input_size, rnn_size, n)

table.insert(outputs, next_h)
end
-- set up the decoder
local top_h = outputs[#outputs]
if dropout > 0 then top_h = nn.Dropout(dropout)(top_h) end
local proj = nn.Linear(rnn_size, input_size)(top_h)
local logsoft = nn.LogSoftMax()(proj)
table.insert(outputs, logsoft)

return nn.gModule(inputs, outputs)
end
Expand Down

0 comments on commit c54a856

Please sign in to comment.