y = lab.map(x,f)
returns a new Tensor
f(x)
: f()
is applied successively to each element of x
.
> x = lab.rand(5,5) > print(x) 0.2828 0.2709 0.0191 0.1217 0.0161 0.4847 0.6586 0.3027 0.6749 0.4945 0.2003 0.9536 0.3208 0.9612 0.9265 0.5415 0.1827 0.6799 0.3222 0.7285 0.7835 0.5920 0.8516 0.2312 0.1857 [torch.Tensor of dimension 5x5] > = lab.map(x, math.cos) 0.9603 0.9635 0.9998 0.9926 0.9999 0.8848 0.7909 0.9545 0.7807 0.8802 0.9800 0.5788 0.9490 0.5725 0.6007 0.8569 0.9834 0.7776 0.9486 0.7462 0.7084 0.8298 0.6588 0.9734 0.9828 [torch.Tensor of dimension 5x5]
f
must return a number or nil
. If it returns nil
, the corresponding value in the returned Tensor
will be 0
.
This behavior is slightly different from the behavior of map
functions provided in the torch
package,
which do not modify the destination tensor if the function returns nil
.
> x = lab.rand(5,5) > print(x) 0.0446 0.5119 0.4007 0.9743 0.0957 0.4649 0.7575 0.4963 0.5474 0.5009 0.5523 0.8003 0.2841 0.0048 0.9853 0.5080 0.1222 0.3049 0.8647 0.2566 0.6863 0.8243 0.3053 0.9196 0.6050 [torch.Tensor of dimension 5x5] > = lab.map(x, function(z) >> if z > 0.5 then return 1 end -- nothing returned otherwise! >> end >> ) 0 1 0 1 0 0 1 0 1 1 1 1 0 0 1 1 0 0 1 0 1 1 0 1 1 [torch.Tensor of dimension 5x5]