Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/krzjoa/torchts

Time series with torch
https://github.com/krzjoa/torchts

deep-learning deep-neural-networks forecasting neural-networks r time-series time-series-forecasting torch

Last synced: about 1 month ago
JSON representation

Time series with torch

Awesome Lists containing this project

README

        

---
output: github_document
always_allow_html: true
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# torchts

[![CRAN status](https://www.r-pkg.org/badges/version/torchts)](https://CRAN.R-project.org/package=torchts)
[![R build status](https://github.com/krzjoa/torchts/workflows/R-CMD-check/badge.svg)](https://github.com/krzjoa/torchts/actions)
[![Codecov test coverage](https://codecov.io/gh/krzjoa/torchts/branch/master/graph/badge.svg)](https://codecov.io/gh/krzjoa/torchts?branch=master)
[![Buy hex
stciker](https://img.shields.io/badge/buy%20hex-torchts-green)](https://www.redbubble.com/i/sticker/torchts-R-package-hex-sticker-by-krzjoa/93537989.EJUG5)

> Time series models with torch

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/kjoachimiak)

## Installation

You can install the released version of torchts from [CRAN](https://CRAN.R-project.org) with:

The development version from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("krzjoa/torchts")
```

## parsnip models

```{r parsnip.api}
library(torchts)
library(torch)
library(rsample)
library(dplyr, warn.conflicts = FALSE)
library(parsnip)
library(timetk)
library(ggplot2)

tarnow_temp <-
weather_pl %>%
filter(station == "TRN") %>%
select(date, tmax_daily)

# Params
EPOCHS <- 3
HORIZON <- 1
TIMESTEPS <- 28

# Splitting on training and test
data_split <-
time_series_split(
tarnow_temp, date,
initial = "18 years",
assess = "2 years",
lag = TIMESTEPS
)

# Training
rnn_model <-
rnn(
timesteps = TIMESTEPS,
horizon = HORIZON,
epochs = EPOCHS,
learn_rate = 0.01,
hidden_units = 20,
batch_size = 32,
scale = TRUE
) %>%
set_device('cpu') %>%
fit(tmax_daily ~ date,
data = training(data_split))

prediction <-
rnn_model %>%
predict(new_data = testing(data_split))

plot_forecast(
data = testing(data_split),
forecast = prediction,
outcome = tmax_daily
)
```

## Transforming data.frames to tensors

In `as_tensor` function we can specify columns, that are used to
create a tensor out of the input `data.frame`. Listed column names
are only used to determine dimension sizes - they are removed after that
and are not present in the final tensor.

```{r example}
temperature_pl <-
weather_pl %>%
select(station, date, tmax_daily)

# Expected shape
c(
n_distinct(temperature_pl$station),
n_distinct(temperature_pl$date),
1
)

temperature_tensor <-
temperature_pl %>%
as_tensor(station, date)

dim(temperature_tensor)
temperature_tensor[1, 1:10]

temperature_pl %>%
filter(station == "SWK") %>%
arrange(date) %>%
head(10)
```

## Similar projects in Python

* [PyTorch Forecasting](https://pytorch-forecasting.readthedocs.io/en/stable/)
* [PyTorchTS](https://github.com/zalandoresearch/pytorch-ts)
* [TorchTS](https://rose-stl-lab.github.io/torchTS/)
* [GluonTS ](https://ts.gluon.ai/)
* [sktime-dl](https://github.com/sktime/sktime-dl)