-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbintot7.lua
More file actions
61 lines (44 loc) · 1.08 KB
/
Copy pathbintot7.lua
File metadata and controls
61 lines (44 loc) · 1.08 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
--file = torch.DiskFile('/home/rotmanmi/Data/GloVe/glove.twitter.27B.25d.txt','r')
--file = torch.DiskFile(opt.binfilename,'r')
--Reading Header
local encodingsize = -1
local ctr = 0
for line in io.lines(opt.glove) do
if ctr == 0 then
for i in string.gmatch(line, "%S+") do
encodingsize = encodingsize + 1
end
end
ctr = ctr + 1
end
words = ctr
size = encodingsize
local w2vvocab = {}
local v2wvocab = {}
local M = torch.FloatTensor(words,size)
--Reading Contents
i = 1
for line in io.lines(opt.glove) do
xlua.progress(i,words)
local vecrep = {}
for i in string.gmatch(line, "%S+") do
table.insert(vecrep, i)
end
str = vecrep[1]
table.remove(vecrep,1)
vecrep = torch.FloatTensor(vecrep)
local norm = torch.norm(vecrep,2)
if norm ~= 0 then vecrep:div(norm) end
w2vvocab[str] = i
v2wvocab[i] = str
M[{{i},{}}] = vecrep
i = i + 1
end
--Writing Files
GloVe = {}
GloVe.M = M
GloVe.w2vvocab = w2vvocab
GloVe.v2wvocab = v2wvocab
torch.save(opt.glove_output,GloVe)
print('Writing t7 File for future usage.')
return GloVe