-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathEmbedding.lua
48 lines (40 loc) · 1.07 KB
/
Embedding.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'nn'
require 'utils'
local _ = require 'underscore'
-- read in word vectors - one per line
function readWordVec(filename)
local file = io.open(filename, "r");
local data = {}
local parts
local wordVec = {} -- global
for line in file:lines() do
parts = line:split(" ")
wordVec[parts[1]] = _.rest(parts)
end
return wordVec
end
-- override (zero out NULL INDEX)
function nn.LookupTable:updateOutput(input)
self:backCompatibility()
input = self:makeInputContiguous(input)
if input:dim() == 1 then
self.output:index(self.weight, 1, input)
elseif input:dim() == 2 then
self.output:index(self.weight, 1, input:view(-1))
self.output = self.output:view(input:size(1), input:size(2), self.weight:size(2))
else
error("input must be a vector or matrix")
end
--zero out NULL_INDEX
local output = self.output:clone()
for i=1, input:size(1) do
if input[i] == #symbols+1 then
output[i]:mul(0)
end
end
self.output = output
return self.output
end
n_hid = 20
nIndex = 2000 -- vocab size
EMBEDDING = nn.LookupTable(nIndex, n_hid)