forked from microsoft/Multiverso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
34 lines (28 loc) · 888 Bytes
/
util.lua
File metadata and controls
34 lines (28 loc) · 888 Bytes
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
#!/usr/bin/env lua
util = {}
ffi = require('ffi')
util.tensor_type = {
['unsigned char'] = 'torch.ByteTensor',
['char'] = 'torch.CharTensor',
['short'] = 'torch.ShortTensor',
['int'] = 'torch.IntTensor',
['long'] = 'torch.LongTensor',
['float'] ='torch.FloatTensor',
['double'] = 'torch.DoubleTensor'
}
function util.tensor2cdata(data, data_type)
if type(data) == 'table' then
data = torch.Tensor(data)
end
data_type = data_type or 'float'
tensor_type = util.tensor_type[data_type]
return data:contiguous():type(tensor_type):data()
end
function util.cdata2tensor(cdata, sizes, data_type)
data_type = data_type or 'float'
tensor_type = util.tensor_type[data_type]
data = torch.Tensor(sizes):type(tensor_type)
ffi.copy(data:data(), cdata, data:nElement() * ffi.sizeof(data_type))
return data
end
return util