MCPcopy Index your code
hub / github.com/mlverse/torch

github.com/mlverse/torch @v0.17.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.17.0 ↗ · + Follow
8,878 symbols 25,527 edges 102 files 3,959 documented · 45%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

torch

Lifecycle:
experimental Test CRAN
status cuda Discord

Installation

torch can be installed from CRAN with:

install.packages("torch")

You can also install the development version with:

remotes::install_github("mlverse/torch")

At the first package load additional software will be installed. See also the full installation guide here.

Examples

You can create torch tensors from R objects with the torch_tensor function and convert them back to R objects with as_array.

library(torch)
x <- array(runif(8), dim = c(2, 2, 2))
y <- torch_tensor(x, dtype = torch_float64())
y
#> torch_tensor
#> (1,.,.) = 
#>   0.6192  0.5800
#>   0.2488  0.3681
#> 
#> (2,.,.) = 
#>   0.0042  0.9206
#>   0.4388  0.5664
#> [ CPUDoubleType{2,2,2} ]
identical(x, as_array(y))
#> [1] TRUE

Simple Autograd Example

In the following snippet we let torch, using the autograd feature, calculate the derivatives:

x <- torch_tensor(1, requires_grad = TRUE)
w <- torch_tensor(2, requires_grad = TRUE)
b <- torch_tensor(3, requires_grad = TRUE)
y <- w * x + b
y$backward()
x$grad
#> torch_tensor
#>  2
#> [ CPUFloatType{1} ]
w$grad
#> torch_tensor
#>  1
#> [ CPUFloatType{1} ]
b$grad
#> torch_tensor
#>  1
#> [ CPUFloatType{1} ]

Contributing

No matter your current skills it’s possible to contribute to torch development. See the contributing guide for more information.

Core symbols most depended-on inside this repo

get
called by 14355
src/torch_api.cpp
Tensor
called by 7012
src/lantern/include/lantern/types.h
int64_t
called by 1319
src/lantern/include/lantern/types.h
bool_t
called by 1096
src/lantern/include/lantern/types.h
IntArrayRef
called by 1039
src/lantern/include/lantern/types.h
Scalar
called by 758
src/lantern/include/lantern/types.h
TensorList
called by 654
src/lantern/include/lantern/types.h
double_t
called by 533
src/lantern/include/lantern/types.h

Shape

Function 8,465
Method 286
Class 125
Enum 2

Languages

C++100%
Python1%

Modules by API surface

src/lantern/src/lantern.cpp3,695 symbols
src/gen-namespace.cpp3,654 symbols
src/torch_api.cpp193 symbols
inst/include/torch_types.h175 symbols
src/lantern/src/Delete.cpp77 symbols
src/lantern/src/Tensor.cpp67 symbols
inst/include/torch_impl.h60 symbols
src/lantern/include/lantern/types.h54 symbols
inst/include/lantern/types.h54 symbols
src/lantern/src/Ignite.cpp41 symbols
src/lantern/src/utils.cpp39 symbols
src/autograd.cpp39 symbols

For agents

$ claude mcp add torch \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact