Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-10-30 05:24:24.255552
Compiled: Wed Oct 30 05:26:35 2024
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).
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).
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
.
## 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.sparse
## [1] FALSE
## $delayedtensor.verbose
## [1] FALSE
## [1] 1e+08
## [1] "/tmp/RtmpvxgP3l"
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.
## <6 x 4> HDF5Matrix object of type "double":
## [,1] [,2] [,3] [,4]
## [1,] 0.47014304 0.58606794 0.91126527 0.04897287
## [2,] 0.25826117 0.55363961 0.51855392 0.11148213
## [3,] 0.05815577 0.24469745 0.07112231 0.05454439
## [4,] 0.70391860 0.66958893 0.06423918 0.21722627
## [5,] 0.19177881 0.67308431 0.39744999 0.27951222
## [6,] 0.55310328 0.24878588 0.99122817 0.31697041
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.47014304 0.05815577 0.19177881
## [2,] 0.25826117 0.70391860 0.55310328
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.5860679 0.2446974 0.6730843
## [2,] 0.5536396 0.6695889 0.2487859
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.91126527 0.07112231 0.39744999
## [2,] 0.51855392 0.06423918 0.99122817
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.04897287 0.05454439 0.27951222
## [2,] 0.11148213 0.21722627 0.31697041
## [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.
## <2 x 3 x 10> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 1.3055492 0.1754129 0.7782487
## [2,] 0.8190385 0.7632313 1.5571117
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.8663318 0.2931404 0.9762786
## [2,] 0.7577923 1.0234336 0.8747989
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.6996828 0.2248963 0.7257465
## [2,] 0.5889559 0.8615420 0.6698447
##
## ...
##
## ,,8
## [,1] [,2] [,3]
## [1,] 0.38825395 0.09240249 0.29273893
## [2,] 0.28344414 0.35855937 0.33368141
##
## ,,9
## [,1] [,2] [,3]
## [1,] 0.5858461 0.1580341 0.5958379
## [2,] 0.4630315 0.8099275 0.8029441
##
## ,,10
## [,1] [,2] [,3]
## [1,] 0.7455425 0.1265388 0.4476635
## [2,] 0.4840030 0.5965639 0.7474146
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,] 1.2942844 1.2669078 0.6719078 . 0.8228873 0.7199456
## [2,] 0.6223665 0.6055064 0.3113415 . 0.4003220 0.3326114
## ... . . . . . .
## [9,] 1.3935898 1.3747491 0.7572769 . 0.8726988 0.8142653
## [10,] 0.3922560 0.3802590 0.1918698 . 0.2540263 0.2045971
##
## ...
##
## ,,4
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 0.5667281 0.6099460 0.2842888 . 0.3865949 0.3438304
## [2,] 0.2698736 0.2875643 0.1350014 . 0.1831801 0.1613933
## ... . . . . . .
## [9,] 0.61781415 0.67324316 0.31099660 . 0.4240759 0.3815503
## [10,] 0.16911212 0.17911565 0.08445583 . 0.1144444 0.1002594
vec
collapses a DelayedArray
into a 1D DelayedArray
(vector).
## <24> HDF5Array object of type "double":
## [1] [2] [3] . [23] [24]
## 0.47014304 0.25826117 0.05815577 . 0.2795122 0.3169704
fnorm
calculates the Frobenius norm of a DelayedArray.
## [1] 2.301097
innerProd
calculates the inner product value of two
DelayedArray.
## [1] 3.673002
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.
## <2 x 3 x 2 x 3> HDF5Array object of type "double":
## ,,1,1
## [,1] [,2] [,3]
## [1,] 0.12712962 0.01572568 0.05185819
## [2,] 0.06983544 0.19034399 0.14956259
##
## ,,2,1
## [,1] [,2] [,3]
## [1,] 0.14756602 0.01825363 0.06019452
## [2,] 0.08106165 0.22094225 0.17360514
##
## ,,1,2
## [,1] [,2] [,3]
## [1,] 0.025572923 0.003163321 0.010431601
## [2,] 0.014047838 0.038288893 0.030085456
##
## ,,2,2
## [,1] [,2] [,3]
## [1,] 0.40815547 0.05048803 0.16649309
## [2,] 0.22420987 0.61110811 0.48017754
##
## ,,1,3
## [,1] [,2] [,3]
## [1,] 0.42237153 0.05224653 0.17229205
## [2,] 0.23201910 0.63239302 0.49690213
##
## ,,2,3
## [,1] [,2] [,3]
## [1,] 0.43993555 0.05441917 0.17945669
## [2,] 0.24166745 0.65869062 0.51756545
Using DelayedDiagonalArray
, we can originally create a
diagonal DelayedArray
by specifying the dimensions (modes) and the values.
## <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.
## <5> DelayedArray object of type "integer":
## [1] [2] [3] [4] [5]
## 1 2 3 4 5
## <5> DelayedArray object of type "double":
## [1] [2] [3] [4] [5]
## 1111 2222 3333 4444 5555
modeSum
calculates the summation for a given mode
m
of a DelayedArray.
The mode specified as m
is collapsed into 1D as
follows.
## <1 x 3 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.7284042 0.7620744 0.7448821
##
## ,,2
## [,1] [,2] [,3]
## [1,] 1.1397075 0.9142864 0.9218702
##
## ,,3
## [,1] [,2] [,3]
## [1,] 1.4298192 0.1353615 1.3886782
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.1604550 0.2717707 0.5964826
## <2 x 1 x 4> DelayedArray object of type "double":
## ,,1
## [,1]
## [1,] 0.7200776
## [2,] 1.5152831
##
## ,,2
## [,1]
## [1,] 1.503850
## [2,] 1.472014
##
## ,,3
## [,1]
## [1,] 1.379838
## [2,] 1.574021
##
## ,,4
## [,1]
## [1,] 0.3830295
## [2,] 0.6456788
## <2 x 3 x 1> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 2.0164491 0.4285199 1.5418253
## [2,] 1.4419368 1.6549730 2.1100877
Similar to modeSum
, modeMean
calculates the
average value for a given mode m
of a DelayedArray.
## <1 x 3 x 4> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.3642021 0.3810372 0.3724410
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.5698538 0.4571432 0.4609351
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.71490959 0.06768075 0.69433908
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.0802275 0.1358853 0.2982413
## <2 x 1 x 4> DelayedArray object of type "double":
## ,,1
## [,1]
## [1,] 0.2400259
## [2,] 0.5050944
##
## ,,2
## [,1]
## [1,] 0.5012832
## [2,] 0.4906715
##
## ,,3
## [,1]
## [1,] 0.4599459
## [2,] 0.5246738
##
## ,,4
## [,1]
## [1,] 0.1276765
## [2,] 0.2152263
## <2 x 3 x 1> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.5041123 0.1071300 0.3854563
## [2,] 0.3604842 0.4137432 0.5275219
There are some tensor specific product such as Hadamard product, Kronecker product, and Khatri-Rao product.
Suppose a tensor A ∈ ℜI × J and a tensor B ∈ ℜI × J.
Hadamard product is defined as the element-wise product of A and B.
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.
## [1] 2 3 4
hadamard_list
calculates Hadamard product of multiple
DelayedArray
objects.
## [1] 2 3 4
Suppose a tensor A ∈ ℜI × J and a tensor B ∈ ℜK × L.
Kronecker product is defined as all the possible combination of element-wise product and the dimensions of output tensor are IK × JL.
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.
## [1] 4 9 16
kronecker_list
calculates Kronecker product of multiple
DelayedArray
objects.
## [1] 4 9 16
Suppose a tensor A ∈ ℜI × J and a tensor B ∈ ℜK × J.
Khatri-Rao product is defined as the column-wise Kronecker product and the dimensions of output tensor is IK × J.
$$ A \odot B = \begin{bmatrix} a_{1} \otimes a_{1} & a_{2} \otimes a_{2} & \cdots & a_{J} \otimes a_{J} \\ \end{bmatrix} $$
Khatri-Rao product can only be used for 2D tensors (matrices).
khatri_rao
calculates Khatri-Rao product of two DelayedArray
objects.
## [1] 4 3
khatri_rao_list
calculates Khatri-Rao product of
multiple DelayedArray
objects.
## [1] 4 3
list_rep
replicates an arbitrary number of any R
object.
## 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] 1438535196 -709113948
## .. .. .. ..@ 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] 1438535196 -709113948
## .. .. .. ..@ 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] 1438535196 -709113948
## .. .. .. ..@ sparse : logi FALSE
modebind_list
collapses multiple DelayedArray
objects into single DelayedArray
object.
m
specifies the collapsed dimension.
## [1] 4 3 4
## [1] 2 6 4
## [1] 2 3 8
rbind_list
is the row-wise modebind_list
and collapses multiple 2D DelayedArray
objects into single DelayedArray
object.
## [1] 4 3
cbind_list
is the column-wise modebind_list
and collapses multiple 2D DelayedArray
objects into single DelayedArray
object.
## [1] 2 6
## R version 4.4.1 (2024-06-14)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] DelayedRandomArray_1.13.1 HDF5Array_1.33.8
## [3] rhdf5_2.49.0 DelayedArray_0.31.14
## [5] SparseArray_1.5.45 S4Arrays_1.5.11
## [7] abind_1.4-8 IRanges_2.39.2
## [9] S4Vectors_0.43.2 MatrixGenerics_1.17.1
## [11] matrixStats_1.4.1 BiocGenerics_0.53.0
## [13] Matrix_1.7-1 DelayedTensor_1.13.0
## [15] BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] dqrng_0.4.1 sass_0.4.9 lattice_0.22-6
## [4] digest_0.6.37 evaluate_1.0.1 grid_4.4.1
## [7] fastmap_1.2.0 jsonlite_1.8.9 BiocManager_1.30.25
## [10] codetools_0.2-20 jquerylib_0.1.4 cli_3.6.3
## [13] rlang_1.1.4 crayon_1.5.3 XVector_0.45.0
## [16] cachem_1.1.0 yaml_2.3.10 tools_4.4.1
## [19] beachmat_2.23.0 parallel_4.4.1 BiocParallel_1.39.0
## [22] einsum_0.1.2 Rhdf5lib_1.27.0 rsvd_1.0.5
## [25] buildtools_1.0.0 R6_2.5.1 lifecycle_1.0.4
## [28] zlibbioc_1.51.2 BiocSingular_1.23.0 irlba_2.3.5.1
## [31] ScaledMatrix_1.13.0 rTensor_1.4.8 bslib_0.8.0
## [34] Rcpp_1.0.13 xfun_0.48 sys_3.4.3
## [37] knitr_1.48 rhdf5filters_1.17.0 htmltools_0.5.8.1
## [40] rmarkdown_2.28 maketools_1.3.1 compiler_4.4.1