The function converts an an object to torch_tensor
instance.
Possible arguments differ a little bit depending on the input object class.
torch_tensor
Returns identical torch_tensor
, but changes dtype or device if specified.
Three dots arguments are ignored for now.
data.frame
If no column names are specified (as "three dots"), the function
simply transforms the input data to matrix
and then to torch_tensor
.
The second scenario assumes that we need a torch_tensor
,
which has more than two dimensions and some columns contains
indicators, how this wrapping should be performed.
It's especially useful when transforming a data.frame containing multiple (and, possibly, multivariate) time series. When passing optional column names, function:
arranges a data.frame by the given columns
removes these columns from the data.frame
creates a n-dimensional tensor with the following shape (n_distinct(column_name_1), n_distinct(column_name_2), ..., number of other columns)
ts
If by
is not specified, it returns a tensor of shape (1, length(object), 1)
.
If we use any by
, the output shape is (length(data)/by, by, 1)
as_tensor(
data,
...,
dtype = NULL,
device = NULL,
requires_grad = FALSE,
pin_memory = FALSE
)
# S3 method for default
as_tensor(
data,
dtype = NULL,
device = NULL,
requires_grad = FALSE,
pin_memory = FALSE
)
# S3 method for torch_tensor
as_tensor(data, ..., dtype = NULL, device = NULL, requires_grad = FALSE)
# S3 method for data.frame
as_tensor(
data,
...,
dtype = NULL,
device = NULL,
requires_grad = FALSE,
pin_memory = FALSE
)
# S3 method for ts
as_tensor(
data,
by = NULL,
dtype = NULL,
device = NULL,
requires_grad = FALSE,
pin_memory = FALSE
)
A data.frame-like object, array
, ts
or torch_tensor
Column names to reshape the data.frame-like object into a n-dimensional tensor
A torch_dtype instance
A device created with torch_device()
If autograd should record operations on the returned tensor.
If set, returned tensor would be allocated in the pinned memory.
For ts
objects only. The length of time series to reshape the time series into tensor.
An object of torch_tensor
class
library(dplyr, warn.conflicts = FALSE)
library(torchts)
# Simple data.frame-to-torch_tensor transformation
as_tensor(head(mtcars))
#> torch_tensor
#> Columns 1 to 8 21.0000 6.0000 160.0000 110.0000 3.9000 2.6200 16.4600 0.0000
#> 21.0000 6.0000 160.0000 110.0000 3.9000 2.8750 17.0200 0.0000
#> 22.8000 4.0000 108.0000 93.0000 3.8500 2.3200 18.6100 1.0000
#> 21.4000 6.0000 258.0000 110.0000 3.0800 3.2150 19.4400 1.0000
#> 18.7000 8.0000 360.0000 175.0000 3.1500 3.4400 17.0200 0.0000
#> 18.1000 6.0000 225.0000 105.0000 2.7600 3.4600 20.2200 1.0000
#>
#> Columns 9 to 11 1.0000 4.0000 4.0000
#> 1.0000 4.0000 4.0000
#> 1.0000 4.0000 1.0000
#> 0.0000 3.0000 1.0000
#> 0.0000 3.0000 2.0000
#> 0.0000 3.0000 1.0000
#> [ CPUFloatType{6,11} ]
# Transformation with column-wise data wrapping
weather_tensor <-
weather_pl %>%
select(-rr_type) %>%
as_tensor(station, date)
dim(weather_tensor)
#> [1] 2 7305 7
# ts class - default
air_passengers <- as_tensor(AirPassengers)
class(air_passengers)
#> [1] "torch_tensor" "R7"
dim(air_passengers)
#> [1] 1 144 1
# ts class using data frequency
air_passengers <-
AirPassengers %>%
as_tensor(frequency(.))
class(air_passengers)
#> [1] "torch_tensor" "R7"
dim(air_passengers)
#> [1] 12 12 1
# ts class using arbitrary frequency
air_passengers <-
as_tensor(AirPassengers, 6)
class(air_passengers)
#> [1] "torch_tensor" "R7"
dim(air_passengers)
#> [1] 24 6 1