Tensor

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.

Multi-dimensional matrix

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]:
6	

In [3]:
print(x:size())


Out[3]:
 4
 5
 6
 2
 7
 3
[torch.LongStorage of size 6]

Internal data representation

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]:
0	

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]:
0	

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]:
  1   2   3   4   5
  6   7   8   9  10
 11  12  13  14  15
 16  17  18  19  20
[torch.DoubleTensor of dimension 4x5]

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]:
  1   2   3   4   5
  6   7   8   9  10
 11  12  13  14  15
 16  17  18  19  20
[torch.DoubleTensor of dimension 4x5]


In [9]:
x:stride() -- element in the last dimension are contiguous!

This is exactly like in C (and not Fortran).

Tensors of different types

Actually, several types of Tensor exist:

  • ByteTensor -- contains unsigned chars
  • CharTensor -- contains signed chars
  • ShortTensor -- contains shorts
  • IntTensor -- contains ints
  • FloatTensor -- contains floats
  • DoubleTensor -- contains doubles

Most numeric operations are implemented only for FloatTensor and DoubleTensor. Other Tensor types are useful if you want to save memory space.

Default Tensor type

For convenience, an alias torch.Tensor is provided, which allows the user to write type-independent scripts, which can then ran after choosing the desired Tensor type with a call like


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 [ ]: