DelayedTensor 1.8.0
Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2023-10-24 14:41:21.735014
Compiled: Tue Oct 24 16:56:13 2023
suppressPackageStartupMessages(library("DelayedTensor"))
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/RtmpNtbkDZ"
Unfold (a.k.a. matricizing) operations are used to reshape a tensor into a matrix.
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.14201871 0.74171836 0.89457361 0.22288631
## [2,] 0.36894753 0.61222268 0.50908460 0.56925740
## [3,] 0.03697118 0.65646913 0.83733904 0.47793673
## [4,] 0.56545517 0.43655634 0.70188635 0.59687095
## [5,] 0.92319873 0.35864518 0.28317491 0.38453918
## [6,] 0.30437558 0.53211447 0.01556021 0.36664361
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.14201871 0.03697118 0.92319873
## [2,] 0.36894753 0.56545517 0.30437558
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.7417184 0.6564691 0.3586452
## [2,] 0.6122227 0.4365563 0.5321145
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.89457361 0.83733904 0.28317491
## [2,] 0.50908460 0.70188635 0.01556021
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.2228863 0.4779367 0.3845392
## [2,] 0.5692574 0.5968710 0.3666436
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,] 1.762417 1.762341 1.783421
## [2,] 1.852990 2.043180 1.143807
##
## ,,2
## [,1] [,2] [,3]
## [1,] 1.1389921 1.0942043 1.3704663
## [2,] 1.2973165 1.3599547 0.9420316
##
## ,,3
## [,1] [,2] [,3]
## [1,] 1.4108541 1.3425973 1.2340361
## [2,] 1.2877555 1.4934812 0.6834915
##
## ...
##
## ,,8
## [,1] [,2] [,3]
## [1,] 0.9122185 1.1025346 0.7308660
## [2,] 1.0006646 1.1934765 0.4297805
##
## ,,9
## [,1] [,2] [,3]
## [1,] 1.0394879 0.9587450 1.2648265
## [2,] 1.1345717 1.2275962 0.8009346
##
## ,,10
## [,1] [,2] [,3]
## [1,] 0.3717713 0.4356210 0.6050505
## [2,] 0.5576262 0.6335131 0.3729372
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.3030929 0.9363495 0.7489267 . 0.3584228 0.7491084
## [2,] 0.4764815 1.3444037 1.0762268 . 0.3804979 1.0374256
## ... . . . . . .
## [9,] 0.4486242 1.3743412 1.0993325 . 0.5138868 1.0960479
## [10,] 0.2032797 0.6180891 0.4944421 . 0.2261841 0.4915294
##
## ...
##
## ,,4
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 0.2835593 0.9837275 0.8079973 . 0.4845977 0.8058227
## [2,] 0.3566083 1.2210540 0.9633387 . 0.5958302 1.0201492
## ... . . . . . .
## [9,] 0.4116051 1.4264823 1.1680603 . 0.7021886 1.1703174
## [10,] 0.1832565 0.6345062 0.5180863 . 0.3121257 0.5213038
vec
collapses a DelayedArray into
a 1D DelayedArray (vector).
DelayedTensor::vec(darr1)
## <24> HDF5Array object of type "double":
## [1] [2] [3] . [23] [24]
## 0.14201871 0.36894753 0.03697118 . 0.3845392 0.3666436
fnorm
calculates the Frobenius norm of a DelayedArray.
DelayedTensor::fnorm(darr1)
## [1] 2.637225
innerProd
calculates the inner product value of two
DelayedArray.
DelayedTensor::innerProd(darr1, darr2)
## [1] 6.776278
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.