forked from torch/torch7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tensor.lua
561 lines (519 loc) · 15.9 KB
/
Tensor.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
-- additional methods for Storage
local Storage = {}
-- additional methods for Tensor
local Tensor = {}
-- types
local types = {'Byte', 'Char', 'Short', 'Int', 'Long', 'Float', 'Double'}
-- Lua 5.2 compatibility
local log10 = math.log10 or function(x) return math.log(x, 10) end
-- tostring() functions for Tensor and Storage
local function Storage__printformat(self)
if self:size() == 0 then
return "", nil, 0
end
local intMode = true
local type = torch.typename(self)
-- if type == 'torch.FloatStorage' or type == 'torch.DoubleStorage' then
for i=1,self:size() do
if self[i] ~= math.ceil(self[i]) then
intMode = false
break
end
end
-- end
local tensor = torch.DoubleTensor(torch.DoubleStorage(self:size()):copy(self), 1, self:size()):abs()
local expMin = tensor:min()
if expMin ~= 0 then
expMin = math.floor(log10(expMin)) + 1
else
expMin = 1
end
local expMax = tensor:max()
if expMax ~= 0 then
expMax = math.floor(log10(expMax)) + 1
else
expMax = 1
end
local format
local scale
local sz
if intMode then
if expMax > 9 then
format = "%11.4e"
sz = 11
else
format = "%SZd"
sz = expMax + 1
end
else
if expMax-expMin > 4 then
format = "%SZ.4e"
sz = 11
if math.abs(expMax) > 99 or math.abs(expMin) > 99 then
sz = sz + 1
end
else
if expMax > 5 or expMax < 0 then
format = "%SZ.4f"
sz = 7
scale = math.pow(10, expMax-1)
else
format = "%SZ.4f"
if expMax == 0 then
sz = 7
else
sz = expMax+6
end
end
end
end
format = string.gsub(format, 'SZ', sz)
if scale == 1 then
scale = nil
end
return format, scale, sz
end
function Storage.__tostring__(self)
local strt = {}
local format,scale = Storage__printformat(self)
if format:sub(2,4) == 'nan' then format = '%f' end
if scale then
table.insert(strt, string.format('%g', scale) .. ' *\n')
for i = 1,self:size() do
table.insert(strt, string.format(format, self[i]/scale) .. '\n')
end
else
for i = 1,self:size() do
table.insert(strt, string.format(format, self[i]) .. '\n')
end
end
table.insert(strt, '[' .. torch.typename(self) .. ' of size ' .. self:size() .. ']\n')
local str = table.concat(strt)
return str
end
for _,type in ipairs(types) do
local metatable = torch.getmetatable('torch.' .. type .. 'Storage')
for funcname, func in pairs(Storage) do
rawset(metatable, funcname, func)
end
end
local function Tensor__printMatrix(self, indent)
local format,scale,sz = Storage__printformat(self:storage())
if format:sub(2,4) == 'nan' then format = '%f' end
-- print('format = ' .. format)
scale = scale or 1
indent = indent or ''
local strt = {indent}
local nColumnPerLine = math.floor((80-#indent)/(sz+1))
-- print('sz = ' .. sz .. ' and nColumnPerLine = ' .. nColumnPerLine)
local firstColumn = 1
local lastColumn = -1
while firstColumn <= self:size(2) do
if firstColumn + nColumnPerLine - 1 <= self:size(2) then
lastColumn = firstColumn + nColumnPerLine - 1
else
lastColumn = self:size(2)
end
if nColumnPerLine < self:size(2) then
if firstColumn ~= 1 then
table.insert(strt, '\n')
end
table.insert(strt, 'Columns ' .. firstColumn .. ' to ' .. lastColumn .. '\n' .. indent)
end
if scale ~= 1 then
table.insert(strt, string.format('%g', scale) .. ' *\n ' .. indent)
end
for l=1,self:size(1) do
local row = self:select(1, l)
for c=firstColumn,lastColumn do
table.insert(strt, string.format(format, row[c]/scale))
if c == lastColumn then
table.insert(strt, '\n')
if l~=self:size(1) then
if scale ~= 1 then
table.insert(strt, indent .. ' ')
else
table.insert(strt, indent)
end
end
else
table.insert(strt, ' ')
end
end
end
firstColumn = lastColumn + 1
end
local str = table.concat(strt)
return str
end
local function Tensor__printTensor(self)
local counter = torch.LongStorage(self:nDimension()-2)
local strt = {''}
local finished
counter:fill(1)
counter[1] = 0
while true do
for i=1,self:nDimension()-2 do
counter[i] = counter[i] + 1
if counter[i] > self:size(i) then
if i == self:nDimension()-2 then
finished = true
break
end
counter[i] = 1
else
break
end
end
if finished then
break
end
-- print(counter)
if #strt > 1 then
table.insert(strt, '\n')
end
table.insert(strt, '(')
local tensor = self
for i=1,self:nDimension()-2 do
tensor = tensor:select(1, counter[i])
table.insert(strt, counter[i] .. ',')
end
table.insert(strt, '.,.) = \n')
table.insert(strt, Tensor__printMatrix(tensor, ' '))
end
return table.concat(strt)
end
function Tensor.__tostring__(self)
local strt = {''}
if self:nDimension() == 0 then
table.insert(strt, '[' .. torch.typename(self) .. ' with no dimension]\n')
else
local tensor = torch.DoubleTensor():resize(self:size()):copy(self)
if tensor:nDimension() == 1 then
local format,scale,sz = Storage__printformat(tensor:storage())
if format:sub(2,4) == 'nan' then format = '%f' end
if scale then
table.insert(strt, string.format('%g', scale) .. ' *\n')
for i = 1,tensor:size(1) do
table.insert(strt, string.format(format, tensor[i]/scale) .. '\n')
end
else
for i = 1,tensor:size(1) do
table.insert(strt, string.format(format, tensor[i]) .. '\n')
end
end
table.insert(strt, '[' .. torch.typename(self) .. ' of size ' .. tensor:size(1) .. ']\n')
elseif tensor:nDimension() == 2 then
table.insert(strt, Tensor__printMatrix(tensor))
table.insert(strt, '[' .. torch.typename(self) .. ' of size ' .. tensor:size(1) .. 'x' .. tensor:size(2) .. ']\n')
else
table.insert(strt, Tensor__printTensor(tensor))
table.insert(strt, '[' .. torch.typename(self) .. ' of size ')
for i=1,tensor:nDimension() do
table.insert(strt, tensor:size(i))
if i ~= tensor:nDimension() then
table.insert(strt, 'x')
end
end
table.insert(strt, ']\n')
end
end
return table.concat(strt)
end
function Tensor.type(self,type)
local current = torch.typename(self)
if not type then return current end
if type ~= current then
local new = torch.getmetatable(type).new()
if self:nElement() > 0 then
new:resize(self:size()):copy(self)
end
return new
else
return self
end
end
function Tensor.typeAs(self,tensor)
return self:type(tensor:type())
end
function Tensor.byte(self)
return self:type('torch.ByteTensor')
end
function Tensor.char(self)
return self:type('torch.CharTensor')
end
function Tensor.short(self)
return self:type('torch.ShortTensor')
end
function Tensor.int(self)
return self:type('torch.IntTensor')
end
function Tensor.long(self)
return self:type('torch.LongTensor')
end
function Tensor.float(self)
return self:type('torch.FloatTensor')
end
function Tensor.double(self)
return self:type('torch.DoubleTensor')
end
function Tensor.real(self)
return self:type(torch.getdefaulttensortype())
end
function Tensor.expand(result,tensor,...)
-- get sizes
local sizes = {...}
local t = torch.type(tensor)
if (t == 'number' or t == 'torch.LongStorage') then
table.insert(sizes,1,tensor)
tensor = result
result = tensor.new()
end
-- check type
local size
if torch.type(sizes[1])=='torch.LongStorage' then
size = sizes[1]
else
size = torch.LongStorage(#sizes)
for i,s in ipairs(sizes) do
size[i] = s
end
end
-- get dimensions
local tensor_dim = tensor:dim()
local tensor_stride = tensor:stride()
local tensor_size = tensor:size()
-- check nb of dimensions
if #size ~= tensor:dim() then
error('the number of dimensions provided must equal tensor:dim()')
end
-- create a new geometry for tensor:
for i = 1,tensor_dim do
if tensor_size[i] == 1 then
tensor_size[i] = size[i]
tensor_stride[i] = 0
elseif tensor_size[i] ~= size[i] then
error('incorrect size: only supporting singleton expansion (size=1)')
end
end
-- create new view, with singleton expansion:
result:set(tensor:storage(), tensor:storageOffset(),
tensor_size, tensor_stride)
return result
end
torch.expand = Tensor.expand
function Tensor.expandAs(result,tensor,template)
if template then
return result:expand(tensor,template:size())
end
return result:expand(tensor:size())
end
torch.expandAs = Tensor.expandAs
function Tensor.repeatTensor(result,tensor,...)
-- get sizes
local sizes = {...}
local t = torch.type(tensor)
if (t == 'number' or t == 'torch.LongStorage') then
table.insert(sizes,1,tensor)
tensor = result
result = tensor.new()
end
-- if not contiguous, then force the tensor to be contiguous
if not tensor:isContiguous() then tensor = tensor:clone() end
-- check type
local size
if torch.type(sizes[1])=='torch.LongStorage' then
size = sizes[1]
else
size = torch.LongStorage(#sizes)
for i,s in ipairs(sizes) do
size[i] = s
end
end
if size:size() < tensor:dim() then
error('Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor')
end
local xtensor = tensor.new():set(tensor)
local xsize = xtensor:size():totable()
for i=1,size:size()-tensor:dim() do
table.insert(xsize,1,1)
end
size = torch.DoubleTensor(xsize):cmul(torch.DoubleTensor(size:totable())):long():storage()
xtensor:resize(torch.LongStorage(xsize))
result:resize(size)
local urtensor = result.new(result)
for i=1,xtensor:dim() do
urtensor = urtensor:unfold(i,xtensor:size(i),xtensor:size(i))
end
for i=1,urtensor:dim()-xtensor:dim() do
table.insert(xsize,1,1)
end
xtensor:resize(torch.LongStorage(xsize))
local xxtensor = xtensor:expandAs(urtensor)
urtensor:copy(xxtensor)
return result
end
torch.repeatTensor = Tensor.repeatTensor
--- One of the size elements can be -1,
--- a new LongStorage is then returned.
--- The length of the unspecified dimension
--- is inferred from the number of remaining elements.
local function specifyFully(size, nElements)
local nCoveredElements = 1
local remainingDim = nil
local sizes = size:totable()
for i = 1, #sizes do
local wantedDimSize = sizes[i]
if wantedDimSize == -1 then
if remainingDim then
error("Only one of torch.view dimensions can be -1.")
end
remainingDim = i
else
nCoveredElements = nCoveredElements * wantedDimSize
end
end
if not remainingDim then
return size
end
assert(nElements % nCoveredElements == 0, "The number of covered elements is not a multiple of all elements.")
local copy = torch.LongStorage(sizes)
copy[remainingDim] = nElements / nCoveredElements
return copy
end
-- TODO : This should be implemented in TH and and wrapped.
function Tensor.view(result, src, ...)
local size = ...
local view, tensor
local function istensor(tensor)
return torch.typename(tensor) and torch.typename(tensor):find('torch.*Tensor')
end
local function isstorage(storage)
return torch.typename(storage) and torch.typename(storage) == 'torch.LongStorage'
end
if istensor(result) and istensor(src) and type(size) == 'number' then
size = torch.LongStorage{...}
view = result
tensor = src
elseif istensor(result) and istensor(src) and isstorage(size) then
size = size
view = result
tensor = src
elseif istensor(result) and isstorage(src) and size == nil then
size = src
tensor = result
view = tensor.new()
elseif istensor(result) and type(src) == 'number' then
size = {...}
table.insert(size,1,src)
size = torch.LongStorage(size)
tensor = result
view = tensor.new()
else
local t1 = 'torch.Tensor, torch.Tensor, number [, number ]*'
local t2 = 'torch.Tensor, torch.Tensor, torch.LongStorage'
local t3 = 'torch.Tensor, torch.LongStorage'
local t4 = 'torch.Tensor, number [, number ]*'
error(string.format('torch.view, expected (%s) or\n (%s) or\n (%s)\n or (%s)', t1, t2, t3, t4))
end
local origNElement = tensor:nElement()
size = specifyFully(size, origNElement)
assert(tensor:isContiguous(), "expecting a contiguous tensor")
view:set(tensor:storage(), tensor:storageOffset(), size)
if view:nElement() ~= origNElement then
local inputSize = table.concat(tensor:size():totable(), "x")
local outputSize = table.concat(size:totable(), "x")
error(string.format("Wrong size for view. Input size: %s. Output size: %s",
inputSize, outputSize))
end
return view
end
torch.view = Tensor.view
function Tensor.viewAs(result, src, template)
if template and torch.typename(template) then
return result:view(src, template:size())
elseif template == nil then
template = src
src = result
result = src.new()
return result:view(src, template:size())
else
local t1 = 'torch.Tensor, torch.Tensor, torch.LongStorage'
local t2 = 'torch.Tensor, torch.LongStorage'
error(string.format('expecting (%s) or (%s)', t1, t2))
end
end
torch.viewAs = Tensor.viewAs
function Tensor.split(result, tensor, splitSize, dim)
if torch.type(result) ~= 'table' then
dim = splitSize
splitSize = tensor
tensor = result
result = {}
else
-- empty existing result table before using it
for k,v in pairs(result) do
result[k] = nil
end
end
dim = dim or 1
local start = 1
while start <= tensor:size(dim) do
local size = math.min(splitSize, tensor:size(dim) - start + 1)
local split = tensor:narrow(dim, start, size)
table.insert(result, split)
start = start + size
end
return result
end
torch.split = Tensor.split
function Tensor.chunk(result, tensor, nChunk, dim)
if torch.type(result) ~= 'table' then
dim = nChunk
nChunk = tensor
tensor = result
result = {}
end
dim = dim or 1
local splitSize = math.ceil(tensor:size(dim)/nChunk)
return torch.split(result, tensor, splitSize, dim)
end
torch.chunk = Tensor.chunk
function Tensor.totable(tensor)
local result = {}
local dim = tensor:dim()
if dim == 1 then
tensor:apply(function(i) table.insert(result, i) end)
elseif dim > 0 then
for i = 1, tensor:size(1) do
table.insert(result, tensor[i]:totable())
end
end
return result
end
torch.totable = Tensor.totable
function Tensor.permute(tensor, ...)
local perm = {...}
local nDims = tensor:dim()
assert(#perm == nDims, 'Invalid permutation')
local j
for i, p in ipairs(perm) do
if p ~= i and p ~= 0 then
j = i
repeat
assert(0 < perm[j] and perm[j] <= nDims, 'Invalid permutation')
tensor = tensor:transpose(j, perm[j])
j, perm[j] = perm[j], 0
until perm[j] == i
perm[j] = j
end
end
return tensor
end
torch.permute = Tensor.permute
for _,type in ipairs(types) do
local metatable = torch.getmetatable('torch.' .. type .. 'Tensor')
for funcname, func in pairs(Tensor) do
rawset(metatable, funcname, func)
end
end