This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathtransform.lua
373 lines (331 loc) · 9.12 KB
/
transform.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
--[[
Copyright (c) 2016-present, Facebook, Inc.
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
]]--
local tnt = require 'torchnet.env'
local argcheck = require 'argcheck'
local utils = require 'torchnet.utils'
local doc = require 'argcheck.doc'
doc[[
### tnt.transform
*Torchnet* provides a set of general data transformations.
These transformations are either directly on the data (e.g., normalization)
or on their structure. This is particularly handy
when manipulating [tnt.Dataset](#tnt.Dataset).
Most of the transformations are simple but can be [composed](#transform.compose) or
[merged](#transform.merge).
]]
local transform = {}
tnt.transform = transform
local unpack = unpack or table.unpack
doc[[
<a name="transform.identity">
#### transform.identity(...)
The identity transform takes any input and return it as it is.
For example, this function is useful when composing
transformations on data from multiple sources, and some of the sources
must not be transformed.
]]
transform.identity =
function(...)
local args = {...}
return function()
return unpack(args)
end
end
transform.compose = argcheck{
doc = [[
<a name = "transform.compose">
#### transform.compose(@ARGP)
@ARGT
This function takes a `table` of functions and
composes them to return one transformation.
This function assumes that the table of transformations
is indexed by contiguous ordered keys starting at 1.
The transformations are composed in the ascending order.
For example, the following code:
```lua
> f = transform.compose{
[1] = function(x) return 2*x end,
[2] = function(x) return x + 10 end,
foo = function(x) return x / 2 end,
[4] = function(x) return x - x end
}
> f(3)
16
```
is equivalent to compose the transformations stored in [1] and [2], i.e.,
defining the following transformation:
```lua
> f = function(x) return 2*x + 10 end
```
Note that transformations stored with keys `foo` and `4` are ignored.
]],
{name='transforms', type='table'},
call =
function(transforms)
for k,v in ipairs(transforms) do
assert(type(v) == 'function', 'table of functions expected')
end
transforms = utils.table.copy(transforms)
return
function(z)
for _, trans in ipairs(transforms) do
z = trans(z)
end
return z
end
end
}
transform.merge = argcheck{
doc = [[
<a name = "transform.merge">
#### transform.merge(@ARGP)
@ARGT
This function takes a `table` of transformations and
merge them into one transformation.
Once apply to an input, this transformation will produce a `table` of output,
containing the transformed input.
For example, the following code:
```lua
> f = transform.merge{
[1] = function(x) return 2*x end,
[2] = function(x) return x + 10 end,
foo = function(x) return x / 2 end,
[4] = function(x) return x - x end
}
```
produces a function which applies a set of transformations to the same input:
```lua
> f(3)
{
1 : 6
2 : 13
foo : 1.5
4 : 0
}
```
]],
{name='transforms', type='table'},
call =
function(transforms)
for k,v in pairs(transforms) do
assert(type(v) == 'function', 'table of functions expected')
end
transforms = utils.table.copy(transforms)
return
function(z)
local newz = {}
for k, trans in pairs(transforms) do
newz[k] = trans(z)
end
return utils.table.mergetensor(newz)
end
end
}
transform.tablenew = argcheck{
doc = [[
<a name = "transform.tablenew">
#### transform.tablenew()
This function creates a new table of functions from an
existing table of functions.
]],
call =
function()
return
function(func)
local tbl = {}
for k,v in pairs(func) do
tbl[k] = v
end
return tbl
end
end
}
transform.tableapply = argcheck{
doc = [[
<a name = "transform.tableapply">
#### transform.tableapply(@ARGP)
@ARGT
This function applies a transformation to a table of input.
It return a table of output of the same size as the input.
For example, the following code:
```lua
> f = transform.tableapply(function(x) return 2*x end)
```
produces a function which multiplies any input by 2:
```lua
> f({[1] = 1, [2] = 2, foo = 3, [4] = 4})
{
1 : 2
2 : 4
foo : 6
4 : 8
}
```
]],
{name='transform', type='function'},
call =
function(transform)
return
function(tbl)
return utils.table.foreach(tbl, transform)
end
end
}
transform.tablemergekeys = argcheck{
doc = [[
<a name = "transform.tablemergekeys">
#### transform.tablemergekeys()
This function merges tables by key. More precisely, the input must be a
`table` of `table` and this function will reverse the table orderto
make the keys from the nested table accessible first.
For example, if the input is:
```lua
> x = { sample1 = {input = 1, target = "a"} , sample2 = {input = 2, target = "b", flag = "hard"}
```
Then apply this function will produce:
```lua
> transform.tablemergekeys(x)
{
input :
{
sample1 : 1
sample2 : 2
}
target :
{
sample1 : "a"
sample2 : "b"
}
flag :
{
sample2: "hard"
}
}
```
]],
call =
function()
return
function(tbl)
local mergedtbl = {}
for idx, elem in pairs(tbl) do
for key, value in pairs(elem) do
if not mergedtbl[key] then
mergedtbl[key] = {}
end
mergedtbl[key][idx] = value
end
end
return mergedtbl
end
end
}
transform.makebatch = argcheck{
doc = [[
<a name = "transform.makebatch">
#### transform.makebatch(@ARGP)
@ARGT
This function is used in many `tnt.Dataset` to format
samples in the format used by the `tnt.Engine`.
This function first [merges keys](#transform.tablemergekeys) to
produces a table of output. Then, transform this table into a tensor by
either using a `merge` transformation provided by the user or by
simply concatenating the table into a tensor directly.
This function uses the [compose](#transform.compose) transform to apply
successive transformations.
]],
{name='merge', type='function', opt=true},
call =
function(merge)
local makebatch
if merge then
makebatch = transform.compose{
transform.tablemergekeys(),
merge
}
else
makebatch = transform.compose{
transform.tablemergekeys(),
transform.tableapply(
function(field)
if utils.table.canmergetensor(field) then
return utils.table.mergetensor(field)
else
return field
end
end
)
}
end
return
function(samples)
assert(type(samples) == 'table', 'makebatch: table of samples expected')
return makebatch(samples)
end
end
}
transform.randperm = argcheck{
doc = [[
<a name = "transform.randperm">
#### transform.randperm(@ARGP)
@ARGT
This function create a vector containing a permutation of the indices from 1 to `size`.
This vector is a `LongTensor` and `size` must be a number.
Once the vector created, this function can be used to call a specific indices in it.
For example:
```lua
> p = transform.randperm(3)
```
creates a function `p` which contains a permutation of indices:
```lua
> p(1)
2
> p(2)
1
> p(3)
3
```
]],
{name="size", type="number"},
call =
function(size)
local perm = torch.randperm(size, 'torch.LongTensor')
return
function(idx)
return perm[idx]
end
end
}
transform.normalize = argcheck{
doc = [[
<a name = "transform.normalize">
#### transform.normalize(@ARGP)
@ARGT
This function normalizes data, i.e., it removes its mean and
divide it by its standard deviation.
The input must be a `Tensor`.
Once create, a `threshold` can be given (must be a number). Then,
the data will be divided by their standard deviation, only if this
deviation is greater than the `threshold`. This is handy, if the
deviation is small and deviding by it could lead to unstability.
]],
{name='threshold', type='number', default=0},
call =
function(threshold)
return
function(z)
local std = z:std()
z:add(-z:mean())
if std > threshold then
z:div(std)
end
return z
end
end
}
return transform