Tensor-class: S4 Class for a Tensor

Description Details Slots Methods Note Author(s) References See Also Examples

Description

An S4 class for a tensor with arbitrary number of modes. The Tensor class extends the base 'array' class to include additional tensor manipulation (folding, unfolding, reshaping, subsetting) as well as a formal class definition that enables more explicit tensor algebra.

Details

This can be seen as a wrapper class to the base array class. While it is possible to create an instance using new, it is also possible to do so by passing the data into as.tensor.

Each slot of a Tensor instance can be obtained using @.

The following methods are overloaded for the Tensor class: dim-methods, head-methods, tail-methods, print-methods, show-methods, element-wise array operations, array subsetting (extract via ‘[’), array subset replacing (replace via ‘[<-’), and tperm-methods, which is a wrapper around the base aperm method.

To sum across any one mode of a tenor, use the function modeSum-methods. To compute the mean across any one mode, use modeMean-methods.

You can always unfold any Tensor into a matrix, and the unfold-methods, k_unfold-methods, and matvec-methods methods are for that purpose. The output can be kept as a Tensor with 2 modes or a matrix object. The vectorization function is also provided as vec. See the attached vignette for a visualization of the different unfoldings.

Conversion from array/matrix to Tensor is facilitated via as.tensor. To convert from a Tensor instance, simply invoke @data.

The Frobenius norm of the Tensor is given by fnorm-methods, while the inner product between two Tensors (of equal modes) is given by innerProd-methods. You can also sum through any one mode to obtain the K-1 Tensor sum using modeSum-methods. modeMean-methods provides similar functionality to obtain the K-1 Tensor mean. These are primarily meant to be used internally but may be useful in doing statistics with Tensors.

For Tensors with 3 modes, we also overloaded t (transpose) defined by Kilmer et.al (2013). See t-methods.

To create a Tensor with i.i.d. random normal(0, 1) entries, see rand_tensor.

Slots

num_modes

number of modes (integer)

modes

vector of modes (integer), aka sizes/extents/dimensions

data

actual data of the tensor, which can be 'array' or 'vector'

Methods

[

signature(tnsr = "Tensor"): ...

[<-

signature(tnsr = "Tensor"): ...

matvec

signature(tnsr = "Tensor"): ...

dim

signature(tnsr = "Tensor"): ...

fnorm

signature(tnsr = "Tensor"): ...

head

signature(tnsr = "Tensor"): ...

initialize

signature(.Object = "Tensor"): ...

innerProd

signature(tnsr1 = "Tensor", tnsr2 = "Tensor"): ...

modeMean

signature(tnsr = "Tensor"): ...

modeSum

signature(tnsr = "Tensor"): ...

Ops

signature(e1 = "array", e2 = "Tensor"): ...

Ops

signature(e1 = "numeric", e2 = "Tensor"): ...

Ops

signature(e1 = "Tensor", e2 = "array"): ...

Ops

signature(e1 = "Tensor", e2 = "numeric"): ...

Ops

signature(e1 = "Tensor", e2 = "Tensor"): ...

print

signature(tnsr = "Tensor"): ...

k_unfold

signature(tnsr = "Tensor"): ...

show

signature(tnsr = "Tensor"): ...

t

signature(tnsr = "Tensor"): ...

tail

signature(tnsr = "Tensor"): ...

unfold

signature(tnsr = "Tensor"): ...

tperm

signature(tnsr = "Tensor"): ...

image

signature(tnsr = "Tensor"): ...

Note

All of the decompositions and regression models in this package require a Tensor input.

Author(s)

James Li jamesyili@gmail.com

References

James Li, Jacob Bien, Martin T. Wells (2018). rTensor: An R Package for Multidimensional Array (Tensor) Unfolding, Multiplication, and Decomposition. Journal of Statistical Software, 87(10), 1-31. URL http://www.jstatsoft.org/v087/i10/.

See Also

as.tensor

Examples

1
2
3
4
5
6
7
tnsr <- rand_tensor()
class(tnsr)
tnsr
print(tnsr)
dim(tnsr)
tnsr@num_modes
tnsr@data

Example output

[1] "Tensor"
attr(,"package")
[1] "rTensor"
Numeric Tensor of 3 Modes
Modes:  3 4 5 
Data: 
[1] -1.0128345  0.7871034 -1.6562702  1.5331019  1.0195140  0.2200949
Numeric Tensor of 3 Modes
Modes:  3 4 5 
Data: 
[1] -1.0128345  0.7871034 -1.6562702  1.5331019  1.0195140  0.2200949
[1] 3 4 5
[1] 3
, , 1

           [,1]      [,2]       [,3]        [,4]
[1,] -1.0128345 1.5331019 -0.1953827 -0.04263477
[2,]  0.7871034 1.0195140  0.2254262 -2.13980611
[3,] -1.6562702 0.2200949  1.2372848  0.95037632

, , 2

             [,1]       [,2]       [,3]       [,4]
[1,]  0.949824744  0.4553205  1.6105107 -0.5203266
[2,] -0.006238914 -0.1568827 -0.5843481 -0.8414010
[3,] -1.988318672 -0.2119606  0.4533796  1.0695332

, , 3

           [,1]        [,2]      [,3]       [,4]
[1,]  0.5484438  0.33196828 0.9956914  0.6807836
[2,]  1.6433682 -0.07500559 1.5777350  0.3818016
[3,] -1.3258225  0.70144481 1.8834256 -0.1541139

, , 4

            [,1]        [,2]       [,3]       [,4]
[1,]  0.07448127  1.09916556  0.2464791 -0.4662460
[2,]  0.12633909 -0.13267347  0.6585041 -0.7402890
[3,] -0.38974725  0.03477215 -0.5197894  0.6300712

, , 5

           [,1]      [,2]      [,3]        [,4]
[1,]  1.2109645 0.5665082 0.3791015 -1.35215802
[2,] -0.4222240 0.1404043 1.4875916  0.56092085
[3,]  0.1657349 0.5929871 0.5819677 -0.07543038

rTensor documentation built on May 15, 2021, 9:06 a.m.