-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLabeledDataFromFile.lua
More file actions
51 lines (45 loc) · 1.39 KB
/
Copy pathLabeledDataFromFile.lua
File metadata and controls
51 lines (45 loc) · 1.39 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
local LabeledDataFromFile = torch.class('LabeledDataFromFile')
function LabeledDataFromFile:__init(loaded,pad,blocksize)
if(pad) then
self.labels, self.labels_pad = self:padTensor(loaded.labels,blocksize)
self.inputs, self.inputs_pad = self:padTensor(loaded.data,blocksize)
else
self.labels = loaded.labels
self.inputs = loaded.data
self.labels_pad = self.labels
self.inputs_pad = self.inputs
end
self.unpadded_len = Util:find_first_tensor(self.inputs):size(1)
end
function LabeledDataFromFile:cuda()
self.labels:cuda()
self.inputs:cuda()
self.labels_pad:cuda()
self.inputs_pad:cuda()
end
function LabeledDataFromFile:padTensor(input,blocksize)
local len = input:size(1)
local padding = -len % blocksize
local len_pad = len + padding
local sizes = input:size()
sizes[1] = sizes[1] + padding
local paddedData = torch.Tensor(sizes)
local actualData = paddedData:narrow(1,1,len)
actualData:copy(input)
if(len_pad > len) then
paddedData:narrow(1,len+1,padding):copy(input:narrow(1,1,padding)) --pad the end with a few examples from the beginning
end
return input,paddedData
end
-- function LabeledDataFromFile:narrow(data,dim,start,len)
-- print('i',dim,start,len)
-- if(torch.isTensor(data)) then
-- return data:narrow(dim,start,len)
-- else
-- local result = {}
-- for k,v in pairs(data) do
-- result[k] = self:narrow(v,dim,start,len)
-- end
-- return result
-- end
-- end