2. Tensor arithmetic by DelayedTensor (original) (raw)
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2025-04-15 15:16:31.229983
Compiled: Tue Apr 15 18:06:37 2025
Setting
suppressPackageStartupMessages(library("DelayedTensor"))
## Warning: multiple methods tables found for 'kronecker'
suppressPackageStartupMessages(library("DelayedArray"))
suppressPackageStartupMessages(library("HDF5Array"))
suppressPackageStartupMessages(library("DelayedRandomArray"))
darr1 <- RandomUnifArray(c(2,3,4))
darr2 <- RandomUnifArray(c(2,3,4))
There are several settings in DelayedTensor.
First, the sparsity of the intermediate DelayedArray objects calculated inside DelayedTensor is set by setSparse
.
Note that the sparse mode is experimental.
Whether it contributes to higher speed and lower memory is quite dependent on the sparsity of the DelayedArray, and the current implementation does not recognize the block size, which may cause out-of-memory errors, when the data is extremely huge.
Here, we specify as.sparse
as FALSE
(this is also the default value for now).
DelayedTensor::setSparse(as.sparse=FALSE)
Next, the verbose message is suppressed by setVerbose
. This is useful when we want to monitor the calculation process.
Here we specify as.verbose
as FALSE
(this is also the default value for now).
DelayedTensor::setVerbose(as.verbose=FALSE)
The block size of block processing is specified by setAutoBlockSize
. When the sparse mode is off, all the functions of _DelayedTensor_are performed as block processing, in which each block vector/matrix/tensor is expanded to memory space from on-disk file incrementally so as not to exceed the specified size.
Here, we specify the block size as 1E+8
.
setAutoBlockSize(size=1E+8)
## automatic block size set to 1e+08 bytes (was 1e+08)
Finally, the temporal directory to store the intermediate HDF5 files during running DelayedTensor is specified by setHDF5DumpDir
.
Note that in many systems the /var
directory has the storage limitation, so if there is no enough space, user should specify the other directory.
# tmpdir <- paste(sample(c(letters,1:9), 10), collapse="")
# dir.create(tmpdir, recursive=TRUE))
tmpdir <- tempdir()
setHDF5DumpDir(tmpdir)
These specified values are also extracted by each getter function.
DelayedTensor::getSparse()
## $delayedtensor.sparse
## [1] FALSE
DelayedTensor::getVerbose()
## $delayedtensor.verbose
## [1] FALSE
getAutoBlockSize()
## [1] 1e+08
getHDF5DumpDir()
## [1] "/tmp/Rtmpbxi2ul"
Tensor Arithmetic Operations
Unfold/Fold Operations
Unfold (a.k.a. matricizing) operations are used to reshape a tensor into a matrix.
Figure 1: Unfold/Fold Operasions
In unfold
, row_idx
and col_idx
are specified to set which modes are used as the row/column.
dmat1 <- DelayedTensor::unfold(darr1, row_idx=c(1,2), col_idx=3)
dmat1
## <6 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.63980643 0.21812896 0.39132046 0.38622902
## [2,] 0.36112747 0.15872188 0.25084649 0.77221451
## [3,] 0.46437259 0.06308050 0.88684214 0.99110827
## [4,] 0.16787903 0.28369303 0.55987210 0.52723608
## [5,] 0.99802572 0.07327655 0.70939028 0.82786761
## [6,] 0.26354919 0.54930979 0.97977149 0.22547246
fold
is the inverse operation of unfold
, which is used to reshape a matrix into a tensor.
In fold
, row_idx
/col_idx
are specified to set which modes correspond the row/column of the output tensor and modes
is specified to set the mode of the output tensor.
dmat1_to_darr1 <- DelayedTensor::fold(dmat1,
row_idx=c(1,2), col_idx=3, modes=dim(darr1))
dmat1_to_darr1
## <2 x 3 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.6398064 0.4643726 0.9980257
## [2,] 0.3611275 0.1678790 0.2635492
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.21812896 0.06308050 0.07327655
## [2,] 0.15872188 0.28369303 0.54930979
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.3913205 0.8868421 0.7093903
## [2,] 0.2508465 0.5598721 0.9797715
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.3862290 0.9911083 0.8278676
## [2,] 0.7722145 0.5272361 0.2254725
identical(as.array(darr1), as.array(dmat1_to_darr1))
## [1] TRUE
There are some wrapper functions of unfold
and fold
.
For example, in k_unfold
, mode m
is used as the row, and the other modes are is used as the column.
k_fold
is the inverse operation of k_unfold
.
dmat2 <- DelayedTensor::k_unfold(darr1, m=1)
dmat2_to_darr1 <- k_fold(dmat2, m=1, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat2_to_darr1))
## [1] TRUE
dmat3 <- DelayedTensor::k_unfold(darr1, m=2)
dmat3_to_darr1 <- k_fold(dmat3, m=2, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat3_to_darr1))
## [1] TRUE
dmat4 <- DelayedTensor::k_unfold(darr1, m=3)
dmat4_to_darr1 <- k_fold(dmat4, m=3, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat4_to_darr1))
## [1] TRUE
In rs_unfold
, mode m
is used as the row, and the other modes are is used as the column.
rs_fold
and rs_unfold
also perform the same operations.
On the other hand, cs_unfold
specifies the mode m
as the column and the other modes are specified as the column.
cs_fold
is the inverse operation of cs_unfold
.
dmat8 <- DelayedTensor::cs_unfold(darr1, m=1)
dmat8_to_darr1 <- DelayedTensor::cs_fold(dmat8, m=1, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat8_to_darr1))
## [1] TRUE
dmat9 <- DelayedTensor::cs_unfold(darr1, m=2)
dmat9_to_darr1 <- DelayedTensor::cs_fold(dmat9, m=2, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat9_to_darr1))
## [1] TRUE
dmat10 <- DelayedTensor::cs_unfold(darr1, m=3)
dmat10_to_darr1 <- DelayedTensor::cs_fold(dmat10, m=3, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat10_to_darr1))
## [1] TRUE
In matvec
, m=2 is specified as unfold.
unmatvec
is the inverse operation of matvec
.
dmat11 <- DelayedTensor::matvec(darr1)
dmat11_darr1 <- DelayedTensor::unmatvec(dmat11, modes=dim(darr1))
identical(as.array(darr1), as.array(dmat11_darr1))
## [1] TRUE
ttm
multiplies a tensor by a matrix.
m
specifies in which mode the matrix will be multiplied.
dmatZ <- RandomUnifArray(c(10,4))
DelayedTensor::ttm(darr1, dmatZ, m=3)
## <2 x 3 x 10> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.4517518 0.5809756 0.7419058
## [2,] 0.3194207 0.3171582 0.4784609
##
## ,,2
## [,1] [,2] [,3]
## [1,] 1.1495812 1.5768782 1.9111394
## [2,] 0.9740938 0.8780892 1.1456059
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.6584407 1.1554729 1.1252288
## [2,] 0.6712638 0.7101045 0.9051443
##
## ...
##
## ,,8
## [,1] [,2] [,3]
## [1,] 0.6390674 1.0221947 0.9292410
## [2,] 0.4771093 0.7823520 1.3188745
##
## ,,9
## [,1] [,2] [,3]
## [1,] 1.045773 1.894941 1.811938
## [2,] 1.178257 1.158676 1.349677
##
## ,,10
## [,1] [,2] [,3]
## [1,] 0.3492449 0.4391800 0.5401703
## [2,] 0.3443835 0.2781354 0.3192288
ttl
multiplies a tensor by multiple matrices.
ms
specifies in which mode these matrices will be multiplied.
dmatX <- RandomUnifArray(c(10,2))
dmatY <- RandomUnifArray(c(10,3))
dlizt <- list(dmatX = dmatX, dmatY = dmatY)
DelayedTensor::ttl(darr1, dlizt, ms=c(1,2))
## <10 x 10 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 0.4631219 0.8175270 1.1962316 . 0.7278337 0.9532120
## [2,] 0.4566411 0.8583608 1.2182456 . 0.7352409 0.9903492
## ... . . . . . .
## [9,] 0.3417113 0.5843939 0.8686841 . 0.5306957 0.6851551
## [10,] 0.1821927 0.4214434 0.5446068 . 0.3199266 0.4713888
##
## ...
##
## ,,4
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 0.4672476 1.0828373 1.3988457 . 0.9927304 1.0914396
## [2,] 0.4956689 1.3146581 1.6069485 . 1.1045764 1.3211309
## ... . . . . . .
## [9,] 0.3321738 0.7100817 0.9501905 . 0.6872278 0.7171518
## [10,] 0.2505783 0.8976332 0.9851022 . 0.6306623 0.8971808
Vectorization
vec
collapses a DelayedArray into a 1D DelayedArray (vector).
Figure 2: Vectorization
DelayedTensor::vec(darr1)
## <24> HDF5Array object of type "double":
## [1] [2] [3] . [23] [24]
## 0.6398064 0.3611275 0.4643726 . 0.8278676 0.2254725
Norm Operations
fnorm
calculates the Frobenius norm of a DelayedArray.
Figure 3: Norm Operations
DelayedTensor::fnorm(darr1)
## [1] 2.799036
innerProd
calculates the inner product value of two_DelayedArray_.
DelayedTensor::innerProd(darr1, darr2)
## [1] 4.509744
Outer Product
Inner product multiplies two tensors and collapses to 0D tensor (norm). On the other hand, the outer product is an operation that leaves all subscripts intact.
Figure 4: Outer Product
DelayedTensor::outerProd(darr1[,,1], darr2[,,1])
## <2 x 3 x 2 x 3> HDF5Array object of type "double":
## ,,1,1
## [,1] [,2] [,3]
## [1,] 0.6150449 0.4464006 0.9594005
## [2,] 0.3471513 0.1613818 0.2533494
##
## ,,2,1
## [,1] [,2] [,3]
## [1,] 0.18882752 0.13705134 0.29454959
## [2,] 0.10658037 0.04954652 0.07778187
##
## ,,1,2
## [,1] [,2] [,3]
## [1,] 0.06298534 0.04571487 0.09825002
## [2,] 0.03555097 0.01652675 0.02594493
##
## ,,2,2
## [,1] [,2] [,3]
## [1,] 0.34635505 0.25138509 0.54027474
## [2,] 0.19549401 0.09088022 0.14267064
##
## ,,1,3
## [,1] [,2] [,3]
## [1,] 0.2883616 0.2092933 0.4498115
## [2,] 0.1627606 0.0756633 0.1187820
##
## ,,2,3
## [,1] [,2] [,3]
## [1,] 0.3301520 0.2396249 0.5149998
## [2,] 0.1863485 0.0866287 0.1359963
Diagonal Operations
Using DelayedDiagonalArray
, we can originally create a diagonal_DelayedArray_ by specifying the dimensions (modes) and the values.
Figure 5: Diagonal Operations
dgdarr <- DelayedTensor::DelayedDiagonalArray(c(5,6,7), 1:5)
dgdarr
## <5 x 6 x 7> sparse DelayedArray object of type "integer":
## ,,1
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 0 0 0
## [2,] 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0
##
## ...
##
## ,,7
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0 0 0 0 0 0
## [2,] 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0
Similar to the diag
of the base package, the diag
of DelayedTensor is used to extract and assign values to DelayedArray.
DelayedTensor::diag(dgdarr)
## <5> DelayedArray object of type "integer":
## [1] [2] [3] [4] [5]
## 1 2 3 4 5
DelayedTensor::diag(dgdarr) <- c(1111, 2222, 3333, 4444, 5555)
DelayedTensor::diag(dgdarr)
## <5> DelayedArray object of type "double":
## [1] [2] [3] [4] [5]
## 1111 2222 3333 4444 5555
Mode-wise Operations
modeSum
calculates the summation for a given mode m
of a DelayedArray. The mode specified as m
is collapsed into 1D as follows.
Figure 6: Mode-wise Operations
DelayedTensor::modeSum(darr1, m=1)
## <1 x 3 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 1.0009339 0.6322516 1.2615749
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.3768508 0.3467735 0.6225863
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.6421669 1.4467142 1.6891618
##
## ,,4
## [,1] [,2] [,3]
## [1,] 1.158444 1.518344 1.053340
DelayedTensor::modeSum(darr1, m=2)
## <2 x 1 x 4> DelayedArray object of type "double":
## ,,1
## [,1]
## [1,] 2.1022047
## [2,] 0.7925557
##
## ,,2
## [,1]
## [1,] 0.3544860
## [2,] 0.9917247
##
## ,,3
## [,1]
## [1,] 1.987553
## [2,] 1.790490
##
## ,,4
## [,1]
## [1,] 2.205205
## [2,] 1.524923
DelayedTensor::modeSum(darr1, m=3)
## <2 x 3 x 1> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 1.635485 2.405404 2.608560
## [2,] 1.542910 1.538680 2.018103
Similar to modeSum
, modeMean
calculates the average value for a given mode m
of a DelayedArray.
DelayedTensor::modeMean(darr1, m=1)
## <1 x 3 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.5004669 0.3161258 0.6307875
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.1884254 0.1733868 0.3112932
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.3210835 0.7233571 0.8445809
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.5792218 0.7591722 0.5266700
DelayedTensor::modeMean(darr1, m=2)
## <2 x 1 x 4> DelayedArray object of type "double":
## ,,1
## [,1]
## [1,] 0.7007349
## [2,] 0.2641852
##
## ,,2
## [,1]
## [1,] 0.1181620
## [2,] 0.3305749
##
## ,,3
## [,1]
## [1,] 0.6625176
## [2,] 0.5968300
##
## ,,4
## [,1]
## [1,] 0.7350683
## [2,] 0.5083077
DelayedTensor::modeMean(darr1, m=3)
## <2 x 3 x 1> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.4088712 0.6013509 0.6521400
## [2,] 0.3857276 0.3846701 0.5045257
Tensor Product Operations
There are some tensor specific product such as Hadamard product, Kronecker product, and Khatri-Rao product.
Hadamard Product
Suppose a tensor \(A \in \Re ^{I \times J}\) and a tensor \(B \in \Re ^{I \times J}\).
Hadamard product is defined as the element-wise product of \(A\) and \(B\).
Figure 7: Hadamard Product
Hadamard product can be extended to higher-order tensors.
\[ A \circ B = \begin{bmatrix} a_{11}b_{11} & a_{12}b_{12} & \cdots & a_{1J}b_{1J} \\ a_{21}b_{21} & a_{22}b_{22} & \cdots & a_{2J}b_{2J} \\ \vdots & \vdots & \ddots & \vdots \\ a_{I1}b_{I1} & a_{I2}b_{I2} & \cdots & a_{IJ}b_{IJ} \\ \end{bmatrix} \]
hadamard
calculates Hadamard product of two _DelayedArray_objects.
prod_h <- DelayedTensor::hadamard(darr1, darr2)
dim(prod_h)
## [1] 2 3 4
hadamard_list
calculates Hadamard product of multiple_DelayedArray_ objects.
prod_hl <- DelayedTensor::hadamard_list(list(darr1, darr2))
dim(prod_hl)
## [1] 2 3 4
Kronecker Product
Suppose a tensor \(A \in \Re ^{I \times J}\) and a tensor \(B \in \Re ^{K \times L}\).
Kronecker product is defined as all the possible combination of element-wise product and the dimensions of output tensor are \({IK \times JL}\).
Figure 8: Kronecker Product
Kronecker product can be extended to higher-order tensors.
\[ A \otimes B = \begin{bmatrix} a_{11}B & a_{12}B & \cdots & a_{1J}B \\ a_{21}B & a_{22}B & \cdots & a_{2J}B \\ \vdots & \vdots & \ddots & \vdots \\ a_{I1}B & a_{I2}B & \cdots & a_{IJ}B \\ \end{bmatrix} \]
kronecker
calculates Kronecker product of two _DelayedArray_objects.
prod_kron <- DelayedTensor::kronecker(darr1, darr2)
dim(prod_kron)
## [1] 4 9 16
kronecker_list
calculates Kronecker product of multiple_DelayedArray_ objects.
prod_kronl <- DelayedTensor::kronecker_list(list(darr1, darr2))
dim(prod_kronl)
## [1] 4 9 16
Khatri-Rao Product
Suppose a tensor \(A \in \Re ^{I \times J}\) and a tensor \(B \in \Re ^{K \times J}\).
Khatri-Rao product is defined as the column-wise Kronecker product and the dimensions of output tensor is \({IK \times J}\).
\[ A \odot B = \begin{bmatrix} a_{1} \otimes a_{1} & a_{2} \otimes a_{2} & \cdots & a_{J} \otimes a_{J} \\ \end{bmatrix} \]
Figure 9: Khatri-Rao Product
Khatri-Rao product can only be used for 2D tensors (matrices).
khatri_rao
calculates Khatri-Rao product of two _DelayedArray_objects.
prod_kr <- DelayedTensor::khatri_rao(darr1[,,1], darr2[,,1])
dim(prod_kr)
## [1] 4 3
khatri_rao_list
calculates Khatri-Rao product of multiple_DelayedArray_ objects.
prod_krl <- DelayedTensor::khatri_rao_list(list(darr1[,,1], darr2[,,1]))
dim(prod_krl)
## [1] 4 3
Utilities Functions
list_rep
replicates an arbitrary number of any R object.
str(DelayedTensor::list_rep(darr1, 3))
## List of 3
## $ :Formal class 'RandomUnifArray' [package "DelayedRandomArray"] with 1 slot
## .. ..@ seed:Formal class 'RandomUnifArraySeed' [package "DelayedRandomArray"] with 6 slots
## .. .. .. ..@ min : num 0
## .. .. .. ..@ max : num 1
## .. .. .. ..@ dim : int [1:3] 2 3 4
## .. .. .. ..@ chunkdim: int [1:3] 2 3 4
## .. .. .. ..@ seeds :List of 1
## .. .. .. .. ..$ : int [1:2] 1671670259 1397338226
## .. .. .. ..@ sparse : logi FALSE
## $ :Formal class 'RandomUnifArray' [package "DelayedRandomArray"] with 1 slot
## .. ..@ seed:Formal class 'RandomUnifArraySeed' [package "DelayedRandomArray"] with 6 slots
## .. .. .. ..@ min : num 0
## .. .. .. ..@ max : num 1
## .. .. .. ..@ dim : int [1:3] 2 3 4
## .. .. .. ..@ chunkdim: int [1:3] 2 3 4
## .. .. .. ..@ seeds :List of 1
## .. .. .. .. ..$ : int [1:2] 1671670259 1397338226
## .. .. .. ..@ sparse : logi FALSE
## $ :Formal class 'RandomUnifArray' [package "DelayedRandomArray"] with 1 slot
## .. ..@ seed:Formal class 'RandomUnifArraySeed' [package "DelayedRandomArray"] with 6 slots
## .. .. .. ..@ min : num 0
## .. .. .. ..@ max : num 1
## .. .. .. ..@ dim : int [1:3] 2 3 4
## .. .. .. ..@ chunkdim: int [1:3] 2 3 4
## .. .. .. ..@ seeds :List of 1
## .. .. .. .. ..$ : int [1:2] 1671670259 1397338226
## .. .. .. ..@ sparse : logi FALSE
Bind Operations
modebind_list
collapses multiple DelayedArray objects into single DelayedArray object.
m
specifies the collapsed dimension.
Figure 10: Bind Operations
dim(DelayedTensor::modebind_list(list(darr1, darr2), m=1))
## [1] 4 3 4
dim(DelayedTensor::modebind_list(list(darr1, darr2), m=2))
## [1] 2 6 4
dim(DelayedTensor::modebind_list(list(darr1, darr2), m=3))
## [1] 2 3 8
rbind_list
is the row-wise modebind_list
and collapses multiple 2D DelayedArray objects into single DelayedArray object.
dim(DelayedTensor::rbind_list(list(darr1[,,1], darr2[,,1])))
## [1] 4 3
cbind_list
is the column-wise modebind_list
and collapses multiple 2D DelayedArray objects into single DelayedArray object.
dim(DelayedTensor::cbind_list(list(darr1[,,1], darr2[,,1])))
## [1] 2 6