apply {GPUmatrix} | R Documentation |
Apply Functions over 'gpu.matrix-class' margins
Description
This function mimics the 'base' function 'apply'
to operate on gpu.matrix-class objects: It returns a vector or a list of values obtained by applying a function to margins of a GPUmatrix.
Usage
## S4 method for signature 'gpu.matrix.tensorflow'
apply(X, MARGIN, FUN, ..., simplify)
## S4 method for signature 'gpu.matrix.torch'
apply(X, MARGIN, FUN, ..., simplify)
Arguments
X |
a |
MARGIN |
1 for rows and 2 for columns. |
FUN |
function to be applied in the operation. |
... |
general additional parameters. Optional arguments to FUN. |
simplify |
a logical indicating whether results should be simplified if possible. Note that some methods that can be simplified when working with 'matrix' objects may not always be simplified for gpu.matrix objects. See details. |
Details
FUN
is found by a call to match.fun
as done in the base function apply
. Internally, apply will use the functions implemented to work with objects from the GPUmatrix library. If the input gpu.matrix-class object(s) are stored on the GPU, then the operations will be performed on the GPU. See gpu.matrix
.
As in apply
, the arguments in ...
cannot have the same name as any of the other arguments to ensure possible errors.
The parameter simplify
indicates wheter the result should be simplified if possible. If the called FUN
returns a gpu.matrix-class object, the result cannot be simplified. In these cases, the parameter simplify
will work as if it was set to FALSE
and the following warning message will be returned: "If the function applied to the GPU matrix returns a tensor or another GPU matrix, then the 'simplify' argument will always be FALSE."
Value
The results of mimics the base function apply
.
Each call to FUN
will return a vector of length n. If simplify
is TRUE and the result can be simplified, then apply will return a numeric vector of dimension c(n,dim(x)[MARGIN])
if n > 1
. If n = 1
, apply
will return a numeric vector of length dim(x)[MARGIN]
.
If simplify is FALSE, apply will return a list of length dim(x)[MARGIN]
.
Note that if simplify
is TRUE and the result of FUN
is an object of class gpu.matrix, then the result cannot be simplified, so it will return a list of length dim(x)[MARGIN]
and each element of this list will be of class gpu.matrix.
For more details see apply
See Also
For more information see:
apply
Examples
if(installTorch()){
a <- gpu.matrix(rnorm(9),3,3)
apply(a, 1, mean) #computes the mean of each row
apply(a, 2, mean) #computes the mean of each column
}