-
Notifications
You must be signed in to change notification settings - Fork 2
/
tensorop.lua
261 lines (239 loc) · 7.63 KB
/
tensorop.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
local display = require 'torch.display'
local torch = require 'torch.env'
local class = require 'class'
local ffi = require 'ffi'
local C = require 'torch.TH'
local RealTensor = class.metatable('torch.RealTensor')
local function index_table(self, k, v)
assert(#k <= self.__nDimension, 'invalid table size')
local cdim = 0
local res
self = C.THRealTensor_newWithTensor(self)[0]
for dim=0,self.__nDimension-1 do
local z = k[dim+1]
if type(z) == 'number' then
z = z - 1
if z < 0 then
z = self.__size[cdim] + z + 1
end
assert(z >= 0 and z < self.__size[cdim], 'out of range')
if self.__nDimension == 1 then
res = self.__storage.__data+self.__storageOffset+z*self.__stride[0]
else
C.THRealTensor_select(self, nil, cdim, z)
end
elseif type(z) == 'table' then
local a = 0
local b = self.__size[cdim]-1
local zz = z[1]
if type(zz) == 'number' then
a = zz-1
b = a
end
if a < 0 then
a = self.__size[cdim] + a + 1
end
assert(a >= 0 and a < self.__size[cdim], 'out of range')
local zz = z[2]
if type(zz) == 'number' then
b = zz-1
end
if b < 0 then
b = self.__size[cdim] + b + 1
end
assert(b >= 0 and b < self.__size[cdim], 'out of range')
assert(b >= a, 'end index must be greater or equal to start index')
C.THRealTensor_narrow(self, nil, cdim, a, b-a+1)
cdim = cdim + 1
elseif type(z) ~= 'nil' then
error('invalid table')
end
end
if v then
if res then
res[0] = v
C.THRealTensor_free(self)
else
self:copy(v) -- DEBUG: this could fail
C.THRealTensor_free(self)
end
else
if res then
C.THRealTensor_free(self)
return tonumber(res)
else
ffi.gc(self, C.THRealTensor_free)
return self
end
end
end
function RealTensor:__index(k)
local type_k = class.type(k)
if type_k == 'number' then
if self.__nDimension == 1 then
assert(k > 0 and k <= self.__size[0], 'out of range')
return tonumber( self.__storage.__data[(k-1)*self.__stride[0]+self.__storageOffset] )
elseif self.__nDimension > 1 then
assert(k > 0 and k <= self.__size[0], 'out of range')
return self:select(1, k)
else
error('empty tensor')
end
elseif type_k == 'torch.LongStorage' then
assert(k.__size == self.__nDimension, 'invalid storage size')
local idx = self.__storageOffset
for dim=0,tonumber(k.__size)-1 do
local z = k.__data[dim]-1
assert(z >= 0 and z < self.__size[dim], 'out of range')
idx = idx + z*self.__stride[dim]
end
return tonumber(self.__storage.__data[idx])
elseif type_k == 'torch.ByteTensor' then
local vals = torch.RealTensor()
C.THRealTensor_maskedSelect(vals, self, k)
return vals
elseif type_k == 'table' then
return index_table(self, k)
else
return RealTensor[k]
end
end
function RealTensor:__newindex(k, v)
local type_k = class.type(k)
local type_v = class.type(v)
if type_k == 'number' then
if type_v == 'number' then
if self.__nDimension == 1 then
assert(k > 0 and k <= self.__size[0], 'out of range')
self.__storage.__data[self.__storageOffset+(k-1)*self.__stride[0]] = v
elseif self.__nDimension > 1 then
local t = C.THRealTensor_newWithTensor(t)
C.THRealTensor_narrow(t, nil, 0, k-1, 1)
C.THRealTensor_fill(t, v)
C.THRealTensor_free(t)
else
error('empty tensor')
end
elseif
type_v == 'torch.ByteTensor'
or type_v == 'torch.CharTensor'
or type_v == 'torch.ShortTensor'
or type_v == 'torch.IntTensor'
or type_v == 'torch.LongTensor'
or type_v == 'torch.FloatTensor'
or type_v == 'torch.DoubleTensor' then
local t = self:narrow(1, k, 1) -- use gc, as this can fail
t:copy(v)
end
elseif type_k == 'torch.LongStorage' then
assert(type_v == 'number', 'number expected as value for a LongStorage as key')
assert(k.__size == self.__nDimension, 'invalid storage size')
local idx = self.__storageOffset
for dim=0,tonumber(k.__size)-1 do
local z = k.__data[dim]-1
assert(z >= 0 and z < self.__size[dim], 'out of range')
idx = idx + z*self.__stride[dim]
end
self.__storage.__data[idx] = v
elseif type_k == 'torch.ByteTensor' then
if type_v == 'number' then
C.THRealTensor_maskedFill(self, k, v)
elseif type_v == 'torch.RealTensor' then
C.THRealTensor_maskedCopy(self, k, v)
else
error('when using a mask as a key, number or tensor are expected as value')
end
elseif type_k == 'table' then
index_table(self, k, v)
else
rawset(self, k, v)
end
end
RealTensor.__tostring = display.tensor
function RealTensor.__add(t1, t2)
local type_t1 = class.type(t1)
local type_t2 = class.type(t2)
local r = torch.RealTensor()
if type_t1 == 'torch.RealTensor' and type_t2 == 'number' then
r:resizeAs(t1)
r:fill(t2)
r:add(t1)
elseif type_t1 == 'number' and type_t2 == 'torch.RealTensor' then
r:resizeAs(t2)
r:fill(t1)
r:add(t2)
elseif type_t1 == 'torch.RealTensor' and type_t2 == 'torch.RealTensor' then
r:resizeAs(t1)
r:copy(t1)
r:add(t2)
else
error('two tensors or one tensor and one number expected')
end
return r
end
function RealTensor.__sub(t1, t2)
local type_t1 = class.type(t1)
local type_t2 = class.type(t2)
local r = torch.RealTensor()
if type_t1 == 'torch.RealTensor' and type_t2 == 'number' then
r:resizeAs(t1)
r:copy(t1)
r:add(-t2)
elseif type_t1 == 'number' and type_t2 == 'torch.RealTensor' then
r:resizeAs(t2)
r:fill(t1)
r:add(-1, t1)
elseif type_t1 == 'torch.RealTensor' and type_t2 == 'torch.RealTensor' then
r:resizeAs(t1)
r:copy(t1)
r:add(-1, t2)
else
error('two tensors or one tensor and one number expected')
end
return r
end
function RealTensor.__unm(self)
local r = torch.RealTensor()
r:resizeAs(self)
r:zero()
r:add(-1, self)
return r
end
function RealTensor.__mul(t1, t2)
local type_t1 = class.type(t1)
local type_t2 = class.type(t2)
local r = torch.RealTensor()
if type_t1 == 'torch.RealTensor' and type_t2 == 'number' then
r:resizeAs(t1)
r:zero()
r:add(t2, t1)
elseif type_t1 == 'number' and type_t2 == 'torch.RealTensor' then
r:resizeAs(t2)
r:zero()
r:add(t1, t2)
elseif type_t1 == 'torch.RealTensor' and type_t2 == 'torch.RealTensor' then
if t1.__nDimension == 1 and t2.__nDimension == 1 then
return t1:dot(t2)
elseif t1.__nDimension == 2 and t2.__nDimension == 1 then
return t1:mv(t2)
elseif t1.__nDimension == 2 and t2.__nDimension == 2 then
return t1:mm(t2)
else
error(string.format('multiplication between %dD and %dD tensorsnot yet supported',
t1.__nDimension, t2.__nDimension))
end
else
error('two tensors or one tensor and one number expected')
end
return r
end
function RealTensor.__div(t1, t2)
local type_t1 = class.type(t1)
local type_t2 = class.type(t2)
assert(type_t2 == 'number', 'number expected')
local r = torch.RealTensor()
r:resizeAs(t1)
r:copy(t1)
r:mul(1/t2)
return r
end