The Tensor class is probably the most important class in Torch. Almost every package depends on this class. It is the class for handling numeric data.
As with pretty much anything in Torch, tensors are serializable and deserializable. What that means is that you can convert a tensor to a string (and save it as a file to disk), and load it back.
A Tensor is a potentially multi-dimensional matrix. The number of dimensions is unlimited that can be created using LongStorage with more dimensions.
Example:
In [1]:
--- creation of a 4D-tensor 4x5x6x2
z = torch.Tensor(4,5,6,2)
--- for more dimensions, (here a 6D tensor) one can do:
s = torch.LongStorage(6)
s[1] = 4; s[2] = 5; s[3] = 6; s[4] = 2; s[5] = 7; s[6] = 3;
x = torch.Tensor(s)
The number of dimensions of a Tensor can be queried by nDimension() or dim(). Size of the i-th dimension is returned by size(i). A LongStorage containing all the dimensions can be returned by size().
In [2]:
print(x:nDimension())
Out[2]:
In [3]:
print(x:size())
Out[3]:
The actual data of a Tensor is contained into a Storage. It can be accessed using storage(). While the memory of a Tensor has to be contained in this unique Storage, it might not be contiguous: the first position used in the Storage is given by storageOffset() (starting at 1). And the jump needed to go from one element to another element in the i-th dimension is given by stride(i). In other words, given a 3D tensor
In [4]:
x = torch.Tensor(7,7,7)
accessing the element (3,4,5) can be done by
In [5]:
x[3][4][5]
Out[5]:
or equivalently under the hood (but slowly!)
In [6]:
x:storage()[x:storageOffset()
+(3-1)*x:stride(1)
+(4-1)*x:stride(2)
+(5-1)*x:stride(3)
]
Out[6]:
One could say that a Tensor is a particular way of viewing a Storage: a Storage only represents a chunk of memory, while the Tensor interprets this chunk of memory as having dimensions:
In [7]:
x = torch.Tensor(4,5)
s = x:storage()
for i=1,s:size() do -- fill up the Storage
s[i] = i
end
print(x)
Out[7]:
Note also that in Torch elements in the same row [elements along the last dimension] are contiguous in memory for a Tensor:
In [8]:
x = torch.Tensor(4,5)
i = 0
x:apply(function()
i = i + 1
return i
end)
print(x)
Out[8]:
In [9]:
x:stride() -- element in the last dimension are contiguous!
This is exactly like in C (and not Fortran).
Actually, several types of Tensor exist:
Most numeric operations are implemented only for FloatTensor and DoubleTensor. Other Tensor types are useful if you want to save memory space.
In [10]:
torch.setdefaulttensortype('torch.FloatTensor')
Out[10]:
See torch.setdefaulttensortype for more details. By default, the alias "points" to torch.DoubleTensor.
In [11]:
Out[11]:
In [ ]: