-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreateDatasets.lua
69 lines (51 loc) · 1.95 KB
/
createDatasets.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'Dataset'
torch.manualSeed(1234)
local method = 'contrastive'
local hdf5 = require 'hdf5'
local f = hdf5.open('dataset/wordnet.h5', 'r')
local originalHypernyms = f:read('hypernyms'):all():add(1) -- convert to 1-based indexing
local numEntities = torch.max(originalHypernyms)
f:close()
print("Loaded data")
local graph = require 'Graph'
-----
-- split hypernyms into train, dev, test
-----
for _, hypernymType in ipairs{'trans', 'notrans'} do
local methodName = method
local hypernyms = originalHypernyms
if hypernymType == 'trans' then
hypernyms = graph.transitiveClosure(hypernyms)
methodName = methodName .. '_trans'
end
local N_hypernyms = hypernyms:size(1)
local splitSize = 4000
-- shuffle randomly
torch.manualSeed(1)
local order = torch.randperm(N_hypernyms):long()
local hypernyms = hypernyms:index(1, order)
print("Building sets ...")
local sets = {
test = hypernyms:narrow(1, 1, splitSize),
val = hypernyms:narrow(1, splitSize + 1, splitSize),
train = hypernyms:narrow(1, splitSize*2+ 1, N_hypernyms - 2*splitSize)
}
print("Done. Building Datasets ...")
local datasets = {}
for name, hnyms in pairs(sets) do
datasets[name] = Dataset(numEntities, hnyms, method)
end
datasets.numEntities = numEntities
-- save visualization info
local paths = require 'paths'
local json = require 'cjson'
local function write_json(file, t)
local filename = file .. '.json'
paths.mkdir(paths.dirname(filename))
local f = io.open(filename, 'w')
f:write(json.encode(t))
f:close()
end
torch.save('dataset/' .. methodName .. '.t7', datasets)
write_json('vis/static/' .. methodName .. '/hypernyms', datasets.train.hypernyms:totable())
end