Authors: Koki Tsuyuzaki [aut, cre]
Last modified: 2024-11-29 05:23:18.287735
Compiled: Fri Nov 29 05:25:27 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/Rtmp1iRh0G"
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.82127706 0.51961651 0.01325686 0.51778849
## [2,] 0.49599883 0.25311377 0.95893576 0.83705893
## [3,] 0.76484797 0.72844099 0.38854432 0.24027168
## [4,] 0.17347972 0.55337794 0.97439015 0.38379134
## [5,] 0.68920304 0.15939984 0.27223428 0.92109197
## [6,] 0.50630600 0.97549399 0.73128784 0.62488164
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.8212771 0.7648480 0.6892030
## [2,] 0.4959988 0.1734797 0.5063060
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.5196165 0.7284410 0.1593998
## [2,] 0.2531138 0.5533779 0.9754940
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.01325686 0.38854432 0.27223428
## [2,] 0.95893576 0.97439015 0.73128784
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.5177885 0.2402717 0.9210920
## [2,] 0.8370589 0.3837913 0.6248816
## [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,] 0.6428587 0.6654191 0.9963871
## [2,] 1.2722561 0.8843861 1.0283109
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.8113164 0.7859279 0.8282923
## [2,] 0.8377271 0.6526376 1.0903276
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.5206320 0.4271731 0.8146884
## [2,] 0.8944162 0.5379819 0.7069661
##
## ...
##
## ,,8
## [,1] [,2] [,3]
## [1,] 0.7242228 1.0884784 0.8051575
## [2,] 1.4125899 1.4748228 1.7648922
##
## ,,9
## [,1] [,2] [,3]
## [1,] 0.3382288 0.6036124 0.6058082
## [2,] 1.1493713 1.0035942 0.9392856
##
## ,,10
## [,1] [,2] [,3]
## [1,] 0.9471110 0.9967047 1.1605950
## [2,] 1.3795150 0.9823599 1.2965601
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.3101738 0.2184266 0.2733889 . 0.39240450 0.02822743
## [2,] 1.3629619 0.9681013 1.2090081 . 1.71419100 0.12334772
## ... . . . . . .
## [9,] 1.6541811 1.2162410 1.5055943 . 2.03013448 0.14627328
## [10,] 0.3277028 0.2210784 0.2798575 . 0.42639323 0.03062776
##
## ...
##
## ,,4
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] 0.3584848 0.2445960 0.3089876 . 0.46093830 0.03306275
## [2,] 1.4559980 1.0026426 1.2565926 . 1.91166174 0.13855039
## ... . . . . . .
## [9,] 1.1734263 0.8576498 1.0214949 . 1.75365090 0.13463242
## [10,] 0.5181027 0.3427435 0.4446627 . 0.61996101 0.04280012
vec
collapses a DelayedArray
into a 1D DelayedArray
(vector).
## <24> HDF5Array object of type "double":
## [1] [2] [3] . [23] [24]
## 0.8212771 0.4959988 0.7648480 . 0.9210920 0.6248816
fnorm
calculates the Frobenius norm of a DelayedArray.
## [1] 3.075155
innerProd
calculates the inner product value of two
DelayedArray.
## [1] 6.487197
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.7044639 0.6560609 0.5911752
## [2,] 0.4254512 0.1488051 0.4342923
##
## ,,2,1
## [,1] [,2] [,3]
## [1,] 0.7871812 0.7330948 0.6605904
## [2,] 0.4754071 0.1662776 0.4852864
##
## ,,1,2
## [,1] [,2] [,3]
## [1,] 0.13040764 0.12144747 0.10943608
## [2,] 0.07875788 0.02754622 0.08039451
##
## ,,2,2
## [,1] [,2] [,3]
## [1,] 0.7021960 0.6539488 0.5892720
## [2,] 0.4240815 0.1483260 0.4328942
##
## ,,1,3
## [,1] [,2] [,3]
## [1,] 0.046779861 0.043565666 0.039256937
## [2,] 0.028252045 0.009881388 0.028839140
##
## ,,2,3
## [,1] [,2] [,3]
## [1,] 0.6481991 0.6036621 0.5439587
## [2,] 0.3914708 0.1369202 0.3996059
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,] 1.3172759 0.9383277 1.1955090
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.7727303 1.2818189 1.1348938
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.9721926 1.3629345 1.0035221
##
## ,,4
## [,1] [,2] [,3]
## [1,] 1.354847 0.624063 1.545974
## <2 x 1 x 4> DelayedArray object of type "double":
## ,,1
## [,1]
## [1,] 2.275328
## [2,] 1.175785
##
## ,,2
## [,1]
## [1,] 1.407457
## [2,] 1.781986
##
## ,,3
## [,1]
## [1,] 0.6740355
## [2,] 2.6646138
##
## ,,4
## [,1]
## [1,] 1.679152
## [2,] 1.845732
## <2 x 3 x 1> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 1.871939 2.122105 2.041929
## [2,] 2.545107 2.085039 2.837969
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.6586379 0.4691638 0.5977545
##
## ,,2
## [,1] [,2] [,3]
## [1,] 0.3863651 0.6409095 0.5674469
##
## ,,3
## [,1] [,2] [,3]
## [1,] 0.4860963 0.6814672 0.5017611
##
## ,,4
## [,1] [,2] [,3]
## [1,] 0.6774237 0.3120315 0.7729868
## <2 x 1 x 4> DelayedArray object of type "double":
## ,,1
## [,1]
## [1,] 0.7584427
## [2,] 0.3919282
##
## ,,2
## [,1]
## [1,] 0.4691524
## [2,] 0.5939952
##
## ,,3
## [,1]
## [1,] 0.2246785
## [2,] 0.8882046
##
## ,,4
## [,1]
## [1,] 0.5597174
## [2,] 0.6152440
## <2 x 3 x 1> DelayedArray object of type "double":
## ,,1
## [,1] [,2] [,3]
## [1,] 0.4679847 0.5305262 0.5104823
## [2,] 0.6362768 0.5212598 0.7094924
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] 267559541 -562783346
## .. .. .. ..@ 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] 267559541 -562783346
## .. .. .. ..@ 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] 267559541 -562783346
## .. .. .. ..@ 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.2 (2024-10-31)
## 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.15.0 HDF5Array_1.35.1
## [3] rhdf5_2.51.0 DelayedArray_0.33.2
## [5] SparseArray_1.7.2 S4Arrays_1.7.1
## [7] abind_1.4-8 IRanges_2.41.1
## [9] S4Vectors_0.45.2 MatrixGenerics_1.19.0
## [11] matrixStats_1.4.1 BiocGenerics_0.53.3
## [13] generics_0.1.3 Matrix_1.7-1
## [15] DelayedTensor_1.13.0 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.2
## [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.47.0
## [16] cachem_1.1.0 yaml_2.3.10 tools_4.4.2
## [19] beachmat_2.23.2 parallel_4.4.2 BiocParallel_1.41.0
## [22] einsum_0.1.2 Rhdf5lib_1.29.0 rsvd_1.0.5
## [25] buildtools_1.0.0 R6_2.5.1 lifecycle_1.0.4
## [28] zlibbioc_1.52.0 BiocSingular_1.23.0 irlba_2.3.5.1
## [31] ScaledMatrix_1.15.0 rTensor_1.4.8 bslib_0.8.0
## [34] Rcpp_1.0.13-1 xfun_0.49 sys_3.4.3
## [37] knitr_1.49 rhdf5filters_1.19.0 htmltools_0.5.8.1
## [40] rmarkdown_2.29 maketools_1.3.1 compiler_4.4.2